Reputation: 14233
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.
Upvotes: 16
Views: 43000
Reputation: 11
I don't know how general it is, but I have a "usercustomize" file lying around which is read when starting my shell. Maybe it's just because I am a newbie for who "environment variable" sounds scary... Anyway, that's how I permanently modify my sys.path
But as said, I don't know how general it is. I have python 2.7.3, installed with python(x,y) on windows 7. And this file is at
C:>Users>Me>Appdata>Roaming>Python>Python27>sitepackages> (Careful, Appdata is hidden folder)
and the file, as said, is "usercustomize.py" nothing special in that file. In my case, just my 3 imported paths:
import sys
sys.path.append('C:\\Users\\blablabla\\LPlot')
sys.path.append('C:\\Users\\bliblibli\\MTSim')
sys.path.append('C:\\Users\\blobloblo\\XP')
hope it helps too... And if not, don't hit me, I'm 100% newb. Or let's say 99.99%
Upvotes: 1
Reputation: 536339
Another possibility is to alter the sys.path
in your sitecustomize.py
, a script that is loaded as Python startup time. (It can be put anywhere on your existing path, and can do any setup tasks you like; I use it to set up tab completion with readline too.)
The site
module offers a method that takes care of adding to sys.path
without duplicates and with .pth
files:
import site
site.addsitedir(r'C:\MyFolder\MySubFolder')
Upvotes: 5
Reputation: 42040
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
Upvotes: 6