简体   繁体   中英

pip installed namespace turns into a builtin

I'm trying to debug a complex situation in which a *nspkg.pth file creates a builtin package that brakes the import in a uwsgi process.

In this case I'm still working with Python2.7.

To be sure the package is correct I started with an "almost empty" one (whose content is shown below) and I have this strage behaviour: if I install using python setup.py install everithing is ok, if I install with pip the namespace seems a builtin:

setup(
    name='jmb.vega',
    namespace_packages=['jmb'],
    version="0.1",
    description='Test package',
    author='Alessandro Dentella',
    packages=find_packages(exclude=['tests', 'tests.*']),
    platforms='any',
    zip_safe=False,
    install_requires=[
        'setuptools',
    ],
)

While init in jmb is:

sandro@bluff:/tmp/jmb.vega$ cat jmb/__init__.py
__import__('pkg_resources').declare_namespace(__name__)

What's wrong with the configuration? Why pip makes it a builtin package?

   root@argo-stretch:/tmp/jmb.vega# python setup.py install
   ...
   root@argo-stretch:/tmp/jmb.vega# python -c 'import jmb; print(jmb)'
   <module 'jmb' from '/usr/local/lib/python2.7/dist-packages/jmb.vega-0.1-py2.7.egg/jmb/__init__.pyc'>

In this case the file 'jmb.vega-0.1-nspkg.pth' is NOT created and the egg is added to 'easy-install.pth'

When doing the installation with pip

   root@argo-stretch:/tmp/jmb.vega# pip install .
   Processing /tmp/jmb.vega
   Requirement already satisfied: setuptools in /usr/lib/python2.7/dist-packages (from jmb.vega==0.1)
   Installing collected packages: jmb.vega
     Running setup.py install for jmb.vega ... done
   Successfully installed jmb.vega-0.1

the file 'jmb.tools-0.7-py2.7-nspkg.pth' is created and the modules seems a built-in

root@argo-stretch:/tmp/jmb.vega# (cd ; python -c 'import jmb; print(jmb)')
   <module 'jmb' (built-in)>

In the real case this is enought to break the import system of any call to namespace 'jmb'.

the test package is

  jmb.vega/
   ├── jmb
   │   ├── __init__.py
   │   └── vega
   │       └── __init__.py
   └── setup.py

sandro@bluff:/tmp/jmb.vega$ cat setup.py from setuptools import setup, find_packages

It's not built-in. What you're seeing is normal. The module type's __repr__ just thinks any module object without a __file__ is built-in , on Python 2:

filename = PyModule_GetFilename((PyObject *)m);
if (filename == NULL) {
    PyErr_Clear();
    return PyString_FromFormat("<module '%s' (built-in)>", name);
}

Namespace packages don't have a __file__ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM