Packages in same namespace: can't import module in setup script

陌路散爱 提交于 2019-12-06 09:50:29

As mentioned in the comments: The command

$ python setup.py sdist

for mymeta.project_bravo downloads an egg of mymeta.project_alpha from a private pypi repo.

Namespace packages are very delicate concerning their imports.

e.g. I found out that when I install ns.a and ns.b regularly in my environment and supply the import path of ns.d in a .pth file in the site-packages directory, it will not work, no matter what. However when I symlink to ns.d in site-packeges/ns directory it will work.

If I install none of the components and supply all paths in a .pth in the site-packages directory, everything works fine.

If I install all of the components regularly also everything works fine.

Just when I mix concepts it will not work.

Therefore I suspect that this might also be the issue here: Different strategies for the import paths.

You might want to try to modify your __init__.py file to:

from pkgutil import extend_path

__path__ = extend_path(__path__, __name__)
__import__('pkg_resources').declare_namespace(__name__)

I regret my decision to do namespace packages. I would never do it again.

Almost one year later, I once again faced this issue and the solution for python>=3.3 is to use implicit namespace packages as specified in PEP 420. The project structure is barely changed, just both the __init__.pys for pkgutil-style namespace package mymeta are gone:

project_alpha/
   -> mymeta/
        -> project_alpha/
             -> __init__.py
             -> version.py
   -> setup.py

project_bravo/
   -> mymeta/
        -> project_bravo/
             -> __init__.py
             -> version.py
   -> setup.py

To make setuptools happy, both setup scripts need to be adjusted as well:

...
setup(
    ...
    packages=['mymeta.' + pkg for pkg in find_packages('mymeta')],
)

Now the imports will be resolved correctly when building project-bravo.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!