Python's import
is going to look for the modules (.py
files) in the python path. You can see what is in python path in the sys.path
variable.
By default, sys.path
will include directory where the script you ran is located. It will also include everything defined in the PYTHONPATH
environment variable.
So, if you have two directories with .py
files, you can either put both in python path, or you can make sure everything all of your source is under the same path entry.
Option 1
(this syntax depends on your shell, here it is for windows)
set PYTHONPATH=%PYTHONPATH%;\path\to\second_folder
python \path\to\first_folder\a.py
Then, you can simply import b
.
Option 2
Create an empty __init__.py
in both directories and a run.py
in the directory above them, so you have:
root_dir
run.py
first_folder
__init__.py
a.py
sedond_folder
__init__.py
b.py
Make run.py
your entry point (run python run.py
) and then you can import any of the modules from any other module using their full module names:
import first_folder.a
import second_folder.b