What's best for importing python path for whole project

I have a project with quite a few packages and modules, and many modules have an entry point.

my_project/
    package1/
        subpackage1/
            __init__.py
            module1.py
        subpackage2/
            ...
        __init__.py
    package2/
        subpackage3/
            ...
__init__.py

      

As module1.py

I have done in the beginning

path = os.path.abspath(os.path.dirname(__file__)
for i in range(2):
    path = os.path.split(path)[0]
sys.path.append(path + '/package1')
sys.path.append(path + '/package2')

      

This way I can import all modules from two folders up. I saw in StackOverflow question that I could create a setup file that would import. Then I still have to do load()

at every entry point. If I put this download function in myproject/load.py

, I still have to add my project to the python path at each entry point.

So my question is, is there a way to use __init__.py

the project folder to automatically import all files if I run any module in any package / subfolder?

PS: Sorry I didn't comment on the other question, but I don't have enough reputation so far.

+3
python path packages


source to share


No one has answered this question yet

See similar questions:

22
Are import paths the right way?

or similar:

5231
What are metaclasses in Python?
1028
What is the Python 3 equivalent "python -m SimpleHTTPServer"
1028
Which IDE should I use for Python?
989
If Python is interpreted, what are .pyc files?
985
How do I import a module with a full path?
845
How to get filename without extension from path in Python?
706
Import module from relative path
589
How to get the absolute path to a file in Python
537
What is the best project structure for a Python application?
369
Best practice for Python



All Articles
Loading...
X
Show
Funny
Dev
Pics