Add a directory to Python sys.path so that it's included each time I use Python
Simply add this path to your PYTHONPATH environment variable. To do this, go to Control Panel / System / Advanced / Environment variable, and in the "User variables" sections, check if you already have PYTHONPATH. If yes, select it and click "Edit", if not, click "New" to add it.
Paths in PYTHONPATH should be separated with ";".
If this is a library that you use throughout your code, you should install it as such. Package it up properly, and either install it in your site-packages
directory - or, if it's specific to certain projects, use virtualenv
and install it just within the relevant virtualenvs.
- You should use
os.path.join
to make your code more reliable. - You have already used my-library in the path. So don't user it the second time in import. If you have a directory structure like this C:\code\my-library\lib.py and function in there, e.g.
print("Hello, world")
then your resulting code should be
import sys
sys.path.append(os.path.join('C:/', 'code', 'my-library')
from lib import main