Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can modules with a common package hierarchy mentioned multiple times in my PYTHONPATH?

I have two separate projects that share a package name. They run OK as long as they are not both on the PYTHONPATH, but as soon as they both appear one of them cannot find imports in its own project.

Example, two projects like this:

Project 1:

x/
  __init__.py
  test.py
  foo.py

test.py contains the line:

import x.foo

Project 2:

x/
  __init__.py
  bar.py

If I run

PYTHONPATH=. python x/y/test.py

there is no error. But if I run

PYTHONPATH='pathtoproject2:.' python x/test.py

I get the error:

Traceback (most recent call last):
  File "x/test.py", line 1, in <module>
    import x.foo
ImportError: No module named foo

Is there a way to have different Python projects with a common package share the PYTHONPATH? Or will Python always use only the first path where a package is found?

Note: I know if you modify the import from x.foo to import foo then it will work. But I want to know if it is possible to do it without modifying either package.

like image 466
Simonz Avatar asked Aug 25 '11 07:08

Simonz


People also ask

What is difference between module and package in Python?

A Python Module can be a simple python File (. py extension file), i.e., a combination of numerous Functions and Global variables. A Python Package is a collection of different Python modules with an __init__.py File. __init__.py Python File works as a Constructor for the Python Package.

How do you access a module imported from a package?

Importing module from a package We can import modules from packages using the dot (.) operator. Now, if this module contains a function named select_difficulty() , we must use the full name to reference it. Now we can directly call this function.

How do I know if a Python module is imported?

Use: if "sys" not in dir(): print("sys not imported!")

How modules and packages work in Python?

In Python, Modules are simply files with the “. py” extension containing Python code that can be imported inside another Python Program. In simple terms, we can consider a module to be the same as a code library or a file that contains a set of functions that you want to include in your application.


1 Answers

Although not supported natively by the import mechanism, there is a workaround solution to create namespaced packages in python. You just have to put the following code on both __init__.py files.

try:
    import pkg_resources
    pkg_resources.declare_namespace(__name__)
except ImportError:
    import pkgutil
    __path__ = pkgutil.extend_path(__path__, __name__)

pkg_resources is provided by setuptools python package and has the advantage that also handles packages contained within egg zip files.

pkgutil is contained in python's standard library so we rely on it to handle the namespace extension if setuptools is not installed in the system.

for more information about python namespace packages can be found here:

http://packages.python.org/distribute/setuptools.html#namespace-packages

http://www.python.org/dev/peps/pep-0382/

like image 180
vinilios Avatar answered Sep 28 '22 02:09

vinilios