Search code examples
pythonnamespacespackagecythonnamespace-package

Strange file being created automatically after installing a Python library


I have a Python library that depends on a C library, so I'm using Cython to deal with it. While I've managed to wrap the library and it's ready for installation, I've been facing a strange problem (note: for the sake of non-advertising I'm not using the name of the library).

I have the following directory structure:

package/
       setup.py
       library/
              __init__.py
              module/
                    lib.py
                    _lib.pyx

The setup.py is supposed to convert _lib.pyx into _lib.so, which can be easily imported by Python. Also, library.module is supposed to be installed as a namespace package, so lib.__init__.py contains the single line of code required by PEP420.

__import__('pkg_resources').declare_namespace(__name__)

But when I do:

python setup.py install

and after checking the .egg created I find a _lib.py created inside module with the following lines in it

def __bootstrap__():
   global __bootstrap__, __loader__, __file__
   import sys, pkg_resources, imp
   __file__ = pkg_resources.resource_filename(__name__,'_lib.so')
   __loader__ = None; del __bootstrap__, __loader__
   imp.load_dynamic(__name__,__file__)
__bootstrap__()

While _lib.py is present along with _lib.so, as soon as lib.py imports _lib, it imports the _lib.py file rather than importing _lib.so which is the actual Python wrapper of the C library.

I'd like to know why _lib.py is being created and how I can avoid it.


Solution

  • It's now clear that the strange file being created with the same name as .so file, is not creating any problem. Infact that's necessary for the tighter integration with the .so module. So, It is automatically created while installing the file.