Skip to content

Instantly share code, notes, and snippets.

@ftorto
Created January 30, 2017 20:31
Show Gist options
  • Save ftorto/e565f33565f7843d2a82d44bb603a184 to your computer and use it in GitHub Desktop.
Save ftorto/e565f33565f7843d2a82d44bb603a184 to your computer and use it in GitHub Desktop.
Python same namespace in different folders
Package-1/namespace/__init__.py
Package-1/namespace/module1/__init__.py
Package-2/namespace/__init__.py
Package-2/namespace/module2/__init__.py

with

  • Package-1
  • Package-2 in PYTHONPATH

If you want to import

import namespace.module1
import namespace.module2

module2 won't be found

Explanation

  • Python look for namespace.module2.
  • It parses PYTHONPATH in the declared order
  • Package-1 found
  • Parsing stopped
  • moule2 not present inside --> ImportError

In __init__.py, write:

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

sources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment