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.
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.
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.
Use: if "sys" not in dir(): print("sys not imported!")
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With