In Python 2.5, I import modules by changing environment variables. It works, but using site-packages does not. Is there another way to import modules in directories other than C:\Python25 ?
-
What does "using site-packages does not" mean? What code are you using? What error are you getting? What -- specifically -- is happening? – S.Lott Mar 30 '09 at 14:00
-
@S.Lott: 39 questions and you haven't get used to him yet? – SilentGhost Mar 30 '09 at 14:01
-
@SilentGhost: My default assumption is that people -- eventually -- learn something. Statistically, this appears to be a case where learning does not appear to be happening. – S.Lott Mar 30 '09 at 14:20
-
@S.Lott: if you haven't noticed, there is a group of users that have 0 answers, but dozens of poorly-worded questions. My feeling is that they use this community to do their jobs. Dishonesty is always more probable than limited mental abilities, that's what Heinlein taught me ;) – SilentGhost Mar 30 '09 at 14:26
-
@SilentGhost: Just responding to the dishonesty comment. The stuff is hard if you don't know what you are doing. I often find that authors will leave out very basic things in tutorials assuming that the base knowledge is a given. I think that leads to a lot of overly simplified poorly represented questions. Also, I highly recommend Heinlein to anyone who can read. – JoeM05 Dec 16 '10 at 16:47
-
@SilentGhost http://blog.stackoverflow.com/2012/07/the-hunting-of-the-snark/ – praxmon Feb 10 '14 at 13:07
4 Answers
On way is with PYTHONPATH
environment variable. Other one is to add path to sys.path
either directly by sys.path.append(path)
or by defining .pth
files and add them to with site.addsitedir(dirWithPths)
. Path files (.pth
) are simple text files with a path in each line. Every .pth
file in dirWithPths
will be read.

- 131,205
- 36
- 218
- 244
-
its not working . site.addsitedir(dirWithPths) is returning None Can you help me? – user46646 Mar 31 '09 at 06:56
-
-
Looks a .pth file is good enough to let python search for the module. No need to use `site.addsitedir(dirWithPths)`, which is only necessary if you want to put the .pth file in some non-default importing directory that python is looking into. – Qiang Xu Nov 28 '12 at 16:51
Append the location to the module to sys.path
.
Edit: (to counter the post below ;-) )
os.path
does something completely different. You need to use sys.path
.
sys.path.append("/home/me/local/modules")

- 307,395
- 66
- 306
- 293

- 610
- 4
- 13
-
os.path.append doesn't do anything completely different except raising AttributeError – SilentGhost Mar 30 '09 at 13:56
-
-
probably the part where you said "So os.path.append might not generate an error" which is untrue since os.path does not have an append method or attribute, resulting in an AttributeError as SilentGhost said – Mar 30 '09 at 20:56
-
omg. I was so wondering about being offensive, I missed the 'being stupid' part. Sorry about that. It was a very poor formulated comment, so I'll just remove it so it'll cause no further harm :-) – wzzrd Mar 31 '09 at 06:10
Directories added to the PYTHONPATH
environment variable are searched after site-packages
, so if you have a module in site-packages
with the same name as the module you want from your PYTHONPATH
, the site-packages
version will win. Also, you may need to restart your interpreter and the shell that launched it for the change to the environment variable to take effect.
If you want to add a directory to the search path at run time, without restarting your program, add the directory to sys.path
. For example:
import sys
sys.path.append(newpath)
If you want your new directory to be searched before site-packages
, put the directory at the front of the list, like this:
import sys
sys.path.insert(0, newpath)

- 21,597
- 12
- 64
- 64
sys.path
is a list to which you can append custom paths to search like this:
sys.path.append("/home/foo")

- 344,730
- 71
- 640
- 635
-
Fixed - the amazing thing was that I typed "os.path" twice and didn't notice. – Andrew Hare Mar 30 '09 at 15:36