Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding folder to Python's path permanently

Tags:

python

I've written a library in python and I want it to reside in a common location on the file system.

From my script, I just want to do:

>>> import mylib

Now I understand that in order to do this, I can do this:

>>> import sys
>>> sys.path.append(r'C:\MyFolder\MySubFolder')
>>> import mylib

But I don't want to have to do that every time.

The question is: How do I add a folder to python's sys.path permanently? I would imagine that it would be an environment variable, but I can't find it.

It seems like it should be easy, but I can't find out how to do it.

like image 510
riwalk Avatar asked Sep 15 '10 22:09

riwalk


People also ask

How do I permanently add a path to SYS path?

On each line of the file you put one directory name, so you can put a line in there with /path/to/the/ and it will add that directory to the path. You could also use the PYTHONPATH environment variable, which is like the system PATH variable but contains directories that will be added to sys. path .

How do you add to a path in Python?

Python3. Output: APPENDING PATH- append() is a built-in function of sys module that can be used with path variable to add a specific path for interpreter to search.

Does Python automatically add to path?

Now, you should be able to call python from the command line directly. The next time you log in, Python should automatically be added to PATH .


2 Answers

The PYTHONPATH environment variable will do it.

like image 133
nmichaels Avatar answered Oct 16 '22 18:10

nmichaels


Deducing from the path you provided in your example here's a tutorial for setting the PYTHONPATH variable in Windows: http://docs.python.org/using/windows.html#excursus-setting-environment-variables

like image 6
Uku Loskit Avatar answered Oct 16 '22 18:10

Uku Loskit