1๐
โ
You can add the home directory to PYTHONPATH
and then it would be possible to do from a import add
.
To programmatically add a directory to PYTHONPATH
, you can use sys.path
.
Example โ
import sys
sys.path.append('/path/to/home/directory')
After doing the above you would be able to directly import any scripts that exist in the home directory. Example if in home directory there is an a.py
with add function, after above lines you would be able to do โ
from a import add
Outside python, you can also set the PYTHONPATH
environment variable to contain your home directory, and then you would be able to import any python scripts in the home directory directly (from anywhere) โ
Example of setting the PYTHONPATH in bash โ
export PYTHONPATH=$PYTHONPATH;/path/to/home/directory
๐คAnand S Kumar
Source:stackexchange.com