Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a directory to Python sys.path so that it's included each time I use Python

Currently, when trying to reference some library code, I'm doing this at the top of my python file:

import sys
sys.path.append('''C:\code\my-library''')
from my-library import my-library

Then, my-library will be part of sys.path for as long as the session is active. If I start a new file, I have to remember to include sys.path.append again.

I feel like there must be a much better way of doing this. How can I make my-library available to every python script on my windows machine without having to use sys.path.append each time?

like image 835
Ben McCormack Avatar asked Sep 19 '11 14:09

Ben McCormack


People also ask

Should I add Python scripts to path?

Adding Python to PATH makes it possible for you to run (use) Python from your command prompt (also known as command-line or cmd). This lets you access the Python shell from your command prompt. In simpler terms, you can run your code from the Python shell by just typing “python” in the command prompt, as shown below.

What does Sys path append do in Python?

The sys. path. append() is a built-in Python function that can be used with path variables to add a specific path for an interpreter to search.


1 Answers

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 ";".

like image 187
tiho Avatar answered Sep 25 '22 13:09

tiho