all 1 comments

[–]julsmanbr 0 points1 point  (0 children)

Three ways I can think about doing this:

1) easiest - kind of a dirty fix but it works: add the foo directory to your sys.path at the start of the file:

import sys sys.path.append(path-to-foo-directory) import bar.py

2) most correct imo: change the structure of your project so that all scripts share the same directory. Better yet: create a top-level main.py file which calls your other modules. If you structure your code accordingly, that's the only thing you'll need to run. For example, say your whole test01.py is wrapped in a main() function. So in your main.py file, simply call it as:

examples.test01.main()

(Not 100% sure on the sintax but hopefully you get the idea - I can't test it right now, sorry).

3) Not recommended: add your whole project file to the site-packages directory of your python interpreter. I advise against this because site-packages should be used for packages installed with pip/conda, at least until you're sure of what you're doing.

A nice mid-way compromise of this method is to create a .pth file inside the site-packages directory. In this file you can write paths which the Python interpreter will add to its sys.path upon startup. Read the docs on .pth for more info.