The docs suggest that I can either add it to the PYTHONPATH environment
variable or to the PythonPath key in the registry. However, PYTHONPATH
doesn't exist, and updating the registry key has no effect (and in any case
the contents aren't the same as sys.path).
So where does sys.path get its value from, and how do I change it?
--
Michael
sys.path is just a list. So in your 'main' module where you do most of
your imports, just append or prepend the path you desire (the search
order is left to right). Although, I believe under Windows creating a
system level or user level PYTHONPATH environment variable will enable
Windows to pick it up. Not 100% sure as I don't have a Windows machine
handy.
>MMM> I'm running Python 3.1 on Vista and I can't figure out how to add my own
>MMM> directory to sys.path.
>MMM> The docs suggest that I can either add it to the PYTHONPATH environment
>MMM> variable or to the PythonPath key in the registry. However, PYTHONPATH
>MMM> doesn't exist,
Then create it.
--
Piet van Oostrum <pi...@cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: pi...@vanoostrum.org
current directory (which uses "" rather than the expected ".")
directories listed in PythonPath environment variable
Windows-system directory
relative to the executable (python.exe or pythonw.exe) that's
actually running
relative to the user directory (docs&settings/username/Application
Data ....
If there's no PythonPath variable, it just uses those other items. I
have no idea what it gets from the registry entries.
Anyway, I'd suggest adding it to PythonPath, and if it's empty, just
create it with the directory you need.
I'm hoping you know you can also add to sys.path directly during script
initialization. It's just a list, and is writeable.
The simplest hack (worst - but most direct) is that sys.path is a list
and you can use it like any other list. (add, delete, change items in it)
It gets loaded from site.py (in the standardard library) at startup.
Anything else you'll have to ask somebody else.
David
Thanks to Jon, Piet, David and Dave for the responses.
> sys.path gets its values from several places.
Ah, I'd misunderstood the docs: I thought it came from just one place (which
I couldn't find).
> Anyway, I'd suggest adding it to PythonPath, and if it's empty, just
> create it with the directory you need.
Thanks--that worked!
> I'm hoping you know you can also add to sys.path directly during script
> initialization. It's just a list, and is writeable.
Yes, but I'm mainly playing in IDLE and I was getting a bit fed up of
repeatedly typing
import sys
sys.path.append('C:/Users/Michael/Code/Python')
import mystuff
--
Michael
Mark Lawrence.
Thanks. Sure enough, I've already got my own test.py but I hadn't
discovered it was a problem yet...
--
Michael
You can use my PEP 370 (http://python.org/dev/peps/pep-0370/) and a .pth
file to extend the search path for modules.
>>> import os
>>> import site
>>> site.USER_SITE
'/home/heimes/.local/lib/python2.6/site-packages'
>>> if not os.path.isdir(site.USER_SITE):
... os.makedirs(site.USER_SITE)
...
>>> pth = open(os.path.join(site.USER_SITE, "michal.pth"), "w")
>>> pth.write("C:/Users/Michael/Code/Python\n")
>>> pth.close()
Restart Python, your custom search path should be in sys.path.
Christian