Namespace package breaks module imports
See original GitHub issueHere’s a simple example to reproduce the problem. In https://github.com/matham/pip-nspace-broken there’s a farm
package as well as a farm.deps.glew
meant to share a namespace with the farm
package.
Also included are the wheels built with it. After installing the wheels with pip, I get the following:
$ python
Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from farm import my_name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'my_name'
If I remove farm.deps.glew
I get the following:
$ python
Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from farm import my_name
Imported farm
This is with pip 9.0.1
.
So basically, having a namespace package seems to break the main package.
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Packaging namespace packages
Namespace packages allow you to split the sub-packages and modules within a ... Then you can break these sub-packages into two separate distributions:....
Read more >pyximport breaks on namespace packages - Google Groups
pyximporting the cython module fails, which I guess is acceptable if we just say "namespace modules are not supported". $ python -c 'from...
Read more >Traps for the Unwary in Python's Import System
The submodules are added to the package namespace trap If the submodule is loaded by any module at any point after the import...
Read more >Importing everything from a python namespace / package
Generally using import * is discouraged, since it clutters up the namespace. And if a project has 50 modules in it, and you...
Read more >Importing Modules - | notebook.community
In effect, a package import turns a directory on your computer into another Python namespace, with attributes corresponding to the subdirectories and module...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For this kind of question, it’s important to separate two different concepts:
In 3.3+ and when using
importlib2
that will work standalone (since__init__.py
files become optional), while in 3.2 and earlier it will requirefarm
orfarm.deps
as a dependency that provides the relevant__init__.py
files.__path__
.Python 3.3+ treats any directory without an
__init__.py
file as a potential part of a namespace package automatically, while thedeclare_namespace
functionality in setuptools lets you request the generation of a boilerplate__init__.py
that takes care of setting__path__
appropriately based on a search ofsys.path
.The fact that these are separate questions means that a non-namespace package may have namespace packages as children, as long as it includes the necessary
__path__
expansion code in its__init__.py
: https://docs.python.org/3/library/pkgutil.html#pkgutil.extend_pathThe trick with doing that is that you don’t tell setuptools that it is a namespace package, you instead publish it as a normal self-contained package that happens to manipulate its own
__path__
attribute.However, if all you want is optional submodules, then you don’t need to use namespace packages at all - just make the submodules depend on the parent package that they install into, and let the parent package take care of installing the necessary
__init__.py
files.@matham: That may work in some scenarios, but will probably not work in cases where
farm
andfarm.deps.glew
are not installed to the same location (i.e. one is installed in system site packages and the other in user site packages, or one under develop and the other not, or one or more of the packages are installed as an egg). Also, I’m sort-of surprised thatimport farm.deps
(or anything below it) works at all on Python prior to 3.3, as there’s no module for that package.