How do native namespaces work?

Peter Otten __peter__ at web.de
Thu Oct 5 13:41:45 EDT 2017


Thomas Nyberg wrote:

> Hello,
> 
> I'm trying to understand native namespaces. I'm currently using python
> 3.5 as packaged in debian 9. I've been following the instructions here:
> 
> https://packaging.python.org/guides/packaging-namespace-packages/#native-namespace-packages
> 
> Those instructions link to the following example:
> 
> https://github.com/pypa/sample-namespace-packages/tree/master/native
> 
> I presume I'm doing something silly, but I can't get that to work. I've
> tried creating a virtual environment as:
> 
> --------------------------------------------------
> $ python3 -m venv venv
> $ source venv/bin/activate
> --------------------------------------------------
> 
> And then I've gone into those repo folders and run their setup.py files
> (e.g. for namespace a):
> 
> --------------------------------------------------
> $ cd sample-namespace-packages/native/pkg_a/
> $ python3 setup.py install
> --------------------------------------------------
> 
> Then if I try to run their sample verification file, things fail:
> 
> --------------------------------------------------
> $ cat sample-namespace-packages/verify_packages.py
> from example_pkg import a
> from example_pkg import b
> 
> print(a.name)
> print(a.__path__)
> print(b.name)
> print(b.__path__)
> $ python3 sample-namespace-packages/verify_packages.py
> Traceback (most recent call last):
>   File "sample-namespace-packages/verify_packages.py", line 1, in <module>
>     from example_pkg import a
> ImportError: No module named 'example_pkg'
> --------------------------------------------------
> 
> Things seem to be installing:
> 
> --------------------------------------------------
> $ ls venv/lib/python3.5/site-packages/
> easy-install.pth           pkg_resources
> easy_install.py            pkg_resources-0.0.0.dist-info
> example_pkg_a-1-py3.5.egg  __pycache__
> example_pkg_b-1-py3.5.egg  setuptools
> pip                        setuptools-32.3.1.dist-info
> pip-9.0.1.dist-info
> --------------------------------------------------
> 
> Am I missing something totally obvious here? Does anyone here see what
> I'm doing wrong? Thanks for any help!

For me it's failing in the same way. I then tried 

pip install path/to/pkg_a

which works. Comparing the site-packages directories gave

added example_pkg/a/__init__.py
added example_pkg/a/__pycache__/__init__.cpython-34.pyc
added example_pkg_a-1.egg-info/PKG-INFO
added example_pkg_a-1.egg-info/SOURCES.txt
added example_pkg_a-1.egg-info/dependency_links.txt
added example_pkg_a-1.egg-info/installed-files.txt
added example_pkg_a-1.egg-info/top_level.txt
removed easy-install.pth
removed example_pkg_a-1-py3.4.egg

i. e. it looks like the modules have to be extracted from the egg and cannot 
be imported directly even though that is what I thought the pth file was 
supposed to achieve. This is all a bit of black magic to me -- I've no idea 
if this is some kind of misconfiguration or a version problem or whatever.




More information about the Python-list mailing list