question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Namespace package breaks module imports

See original GitHub issue

Here’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:closed
  • Created 7 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ncoghlancommented, Dec 22, 2016

For this kind of question, it’s important to separate two different concepts:

  1. Optional submodules, where a given component installs into a shared package module hierarchy. This doesn’t require namespace packages at all, it just requires what @matham describes in https://github.com/pypa/setuptools/issues/895#issuecomment-268722067

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 require farm or farm.deps as a dependency that provides the relevant __init__.py files.

  1. Namespace packages, which allow a package to be split across multiple directories by having more than one entry in __path__.

Python 3.3+ treats any directory without an __init__.py file as a potential part of a namespace package automatically, while the declare_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 of sys.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_path

The 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.

0reactions
jaracocommented, Dec 22, 2016

@matham: That may work in some scenarios, but will probably not work in cases where farm and farm.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 that import farm.deps (or anything below it) works at all on Python prior to 3.3, as there’s no module for that package.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found