How are Python modules (which are shared libraries) imported without a .py file?

I run the Python interactive shell and try to see the path from which the module is imported using the validation module. The modules I'm trying to import have Python wrappers around the C ++ API using SWIG.

The following snippet shows my steps:

>>> import os >>> import inspect >>> >>> import db >>> inspect.getfile(db) 'mypath1/lib/db/db.pyc' >>> >>> import dart >>> inspect.getfile(dart) 'mypath2/lib/dart.so' >>> 

My PYTHONPATH contains both mypath1/lib/db and mypath2/lib .

I got the impression that in order to load modules, the interpreter needs access to the .py file, which then calls imp.load_module to load the necessary shared library ( .so file). This is what I see in the case of the db module, which has a db.py file under mypath1/lib/db . However, dart does not have a .py file in the mypath2/lib section.

Is it possible to import a module without a .py file, as is the case with the dart module?

+6
source share
1 answer

Python looks for several different files for any given import , including a directory by that name and containing the __init__.py , .so file for clean Python source modules and .pyc files that can be used even if .py is deleted.

Run strace -o trace_output.txt python to see how it works. Partial example for import md5 :

 stat("/usr/lib/python2.7/md5", 0x7fff81ff16d0) = -1 ENOENT (No such file or directory) open("/usr/lib/python2.7/md5.x86_64-linux-gnu.so", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/lib/python2.7/md5.so", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/lib/python2.7/md5module.so", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/lib/python2.7/md5.py", O_RDONLY) = 3 

In my setup, it is actually looking for:

  • ~/.local/lib/python2.7/
  • /usr/local/lib/python2.7/dist-packages
  • /usr/lib/python2.7/dist-packages
  • /usr/lib/python2.7/

Within each directory, a stat invocation pattern to search for a directory that searches for .so files, then .py .

For more information on writing a purely native python module, see here: https://docs.python.org/2/extending/index.html

+2
source

All Articles