3

I have a project with quite some 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

In in module1.py I would do at 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')

Thus I can import all modules from two folder upwards. I saw at https://stackoverflow.com/a/6466248/1449104 that I could create a setup file which does the imports. Then I still have to do load() at every entry point. If I place this load function in myproject/load.py I still have to add my project to the python path at every entry point.

So my question is: Is there a way to use the __init__.py in the project folder to do all the imports automatically if I run any module in any package/subpackage?

PS: Sorry that I did not comment in the other question but I do not have enough reputation till now.

Community
  • 1
  • 1
Meppel
  • 106
  • 6
  • 1
    You don't have to do any of that. The best practice is to use `setuptools` and let it manage it. – Keith Aug 19 '14 at 09:34
  • @Meppel While getting familiar with using `setup.py` takes some effort, it is definitely worth. It will make all your packages easy to use anywhere you need. In general, anytime you feel a need to manipulate pythonpath, you shall take it as a sign, somemething is wrong and shall be imporoved. – Jan Vlcinsky Aug 19 '14 at 10:05
  • setuptools? I don't see how setuptools could be useful in this case – mguijarr Aug 19 '14 at 12:23
  • 1
    write a `.pth` file insead of fidling with `sys.path` (https://docs.python.org/2/library/site.html) – Paulo Scardine Aug 19 '14 at 19:00

0 Answers0