Skip to content Skip to sidebar Skip to footer

How To Have Multi-directory Or Multi-package Python Project?

I have project structure like this: package1/__init__.py package1/file1.py package1/file2.py package2/__init__.py package2/file1.py package2/file2.py __init__.py script1.py scrip

Solution 1:

You either need both package1 and package2 to be inside a package, in which case they can both import from each other:

root_package/
    __init__.py
    package1/
    package2/

Or add the packages to your PYTHONPATH, in which case any python script on your system can import from them:

export PYTHONPATH="$PYTHONPATH:/path/to/package1:/path/to/package2"

Update: you cannot import as part of a package if you are running the scripts directly. What you should do is define classes and functions in your packages as desired, then import them from another script:

root_package/
    __init__.py
    my_script.py
    package1/
    package2/

script.py:

from package1 import ...
from package2 import ...

Post a Comment for "How To Have Multi-directory Or Multi-package Python Project?"