Adding folder to Python's path permanently
Asked Answered
K

4

16

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.

Karylkarylin answered 15/9, 2010 at 22:12 Comment(0)
B
17

The PYTHONPATH environment variable will do it.

Branle answered 15/9, 2010 at 22:16 Comment(4)
Perfect. That's what I was looking for.Karylkarylin
Does it work for linux? I add "PYTHONPATH=~/pythons/ " to my .zshrc (echo $PYTHONPATH return it also). Then I print sys.path and it's not there. :/Guevara
Ok, It works in linux. I didn't include command 'export' in my .zshrc. It's strange becasue i'm changing the $PATH and there is no need to 'export' it. Any idea?Guevara
You might need to set it as a system or a user environment variable. In Windows 10 you can type environment in the start menu, choose Environment Variables, then push on the button of the same name. You might need to create the variable.Tophet
R
6

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

Rama answered 15/9, 2010 at 22:17 Comment(0)
A
5

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')
Augustine answered 15/9, 2010 at 22:40 Comment(5)
If adding a file to the current path were an option, why would I even bother trying to change the path at all? And even worse, what could possibly make me want to add that line to every single python script that I write? Sorry bud...not very elegant.Karylkarylin
@Stargazer712: Er... sitecustomize.py is run automatically by Python. You don't have to add a line to every Python script. That's the whole point.Augustine
perhaps it is from an unspoken requirement that this library installed using a typical installer, and modifying an environment variable is far easier (and more transaction-like) than editing a file.Karylkarylin
Ah, if that's the case: drop a .pth file in the Python installation's site-packages folder containing the path to the outside folder. This is more reliable than frobbing the PYTHONPATH or sitecustomize. (If you need an outside directory at all... for a ‘typical’ library, normally distutils/setuptools would take care of dropping it in the appropriate place.)Augustine
@Augustine could You paste the site.py file? I'm courios about completition and the other.Guevara
P
1

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%

Pirtle answered 19/7, 2013 at 3:22 Comment(4)
hmmm actually it's not simply "read" when starting my console, as sys is actually not imported yet when I start it... Anyway, my 3 folders are permanently in my sys.path... So somehow it's working...Pirtle
Welcome to StackOverflow! Your desire to help is commendable, but you shouldn't post answers to a question when you're not certain if they're correct. If you have to lead off your answer with "I don't know how general it is," it's a sign that you aren't quite experienced enough yet to be answering this question. And besides, this question was asked (and answered) nearly three years ago; adding another answer to it is not likely to help the original questioner, who has long since moved on to other topics by now. :-)Trouper
Sorry... But actually I had been looking for an answer just like the one I put for one day; because I knew this file had to exist somewhere, until I find it again (Ialready used it in the past put couldn't find it anymore). So I thought maybe it would be useful to someone else. And I didn't want to open a new topic for it, when this one was so similar...Pirtle
In order for usercustomize.py to be imported, site.ENABLE_USER_SITE must be set to 'True'. See the two paragraphs above 'Readline Configuration' section: https://docs.python.org/3/library/site.html#module-siteShelburne

© 2022 - 2024 — McMap. All rights reserved.