Adding paths to sys.path permanently, and another problem...

A

Amir Dekel

Hi everyone,

I have two problems:

1. How can I keep my changes in sys.path after closing the interpreter?

2. os.path.expanduser("~") always gives me "C:\\" instead of my
homepath. I have tried to change my homepath in WinXP using set
homepath(in command line), and alsaw using the "Environment Variables"
in WinXP system properties, non helped. I really have to fix this somehow.

Thanks,
Amir
 
L

Lenard Lindstrom

Amir Dekel said:
Hi everyone,

I have two problems:

1. How can I keep my changes in sys.path after closing the interpreter?
Just how flexible does this need to be? sys.path can easily be altered
at startup from within a sitecustomize module (section 3.28 -- site --
Python Runtime Services -- Python Library Reference) or a module specified
by the PYTHONSTARTUP environment variable (section 3.29 -- user -- Python
Runtime Services). Just have one of these modules append to sys.path any
additional paths you need.

Saving sys.path changes between interpreter runs would be more involved.
On Windows Python loads the initial module search path from the registry.
Python's registry entries are made during installation and are left alone
afterwards. Changing these entries is probably not a good idea. But
if you need sys.path persistence I can try writing an example that does
it automatically using the atexit module and a history file.

Lenard Lindstrom
<[email protected]>
 
J

Jeff Shannon

Lenard said:
Saving sys.path changes between interpreter runs would be more involved.
On Windows Python loads the initial module search path from the registry.
Python's registry entries are made during installation and are left alone
afterwards. Changing these entries is probably not a good idea. But
if you need sys.path persistence I can try writing an example that does
it automatically using the atexit module and a history file.

Not only can one modify the environment variable PYTHONPATH, but one can
also use a .pth file. Under windows, when the interpreter starts up it
will search its default sys.path for any files with extension .pth; if
it finds any, then it will use each line of those files as a directory
to add to sys.path. Thus, if you drop a mymodule.pth file in
site-packages, which contains a list of the directories you're
interested in, sys.path will automatically be amended for you every time
Python starts.

Jeff Shannon
Technician/Programmer
Credit International
 
A

Amir Dekel

Jeff said:
Not only can one modify the environment variable PYTHONPATH, but one can
also use a .pth file. Under windows, when the interpreter starts up it
will search its default sys.path for any files with extension .pth; if
it finds any, then it will use each line of those files as a directory
to add to sys.path. Thus, if you drop a mymodule.pth file in
site-packages, which contains a list of the directories you're
interested in, sys.path will automatically be amended for you every time
Python starts.

Works great! So simple, yet people get so confused...
Do you have a solution for my other problem?

Thanks,
Amir
 
J

Jeff Shannon

Amir said:
2. os.path.expanduser("~") always gives me "C:\\" instead of my
homepath. I have tried to change my homepath in WinXP using set
homepath(in command line), and alsaw using the "Environment Variables"
in WinXP system properties, non helped. I really have to fix this
somehow.


Well, according to os.path.expanduser()'s docstring, it uses the $HOME
environment variable to determine how to expand ~. I don't know what's
standard on Windows, but I tried checking for a $HOME and found none.
Here's (a slightly redacted copy of) what I *do* find (Win2K):
.... print '%15s %s' % (key, val)
....
TMP C:\DOCUME~1\Jeff\LOCALS~1\Temp
USERNAME jeff
COMPUTERNAME ######
LOGONSERVER ######
COMSPEC C:\WINNT\system32\cmd.exe
USERDOMAIN #######
COMMONPROGRAMFILES C:\Program Files\Common Files
PROCESSOR_IDENTIFIER x86 Family #...
PROGRAMFILES C:\Program Files
PROCESSOR_REVISION 0806
PATHEXT .COM;.EXE;.BAT;.CMD;#....
SYSTEMROOT C:\WINNT
PATH C:\Python22\.;C:\WINNT\system32;C:\WINNT;##....
APPDATA C:\Documents and Settings\Jeff\Application Data
TEMP C:\DOCUME~1\Jeff\LOCALS~1\Temp
HOMEDRIVE C:
SYSTEMDRIVE C:
PROCESSOR_ARCHITECTURE x86
NUMBER_OF_PROCESSORS 1
ALLUSERSPROFILE C:\Documents and Settings\All Users.WINNT
PROCESSOR_LEVEL 6
HOMEPATH \
OS2LIBPATH C:\WINNT\system32\os2\dll;
USERPROFILE C:\Documents and Settings\Jeff
OS Windows_NT
WINDIR C:\WINNT
Judging from this, I think that os.environ['USERPROFILE'] seems like it
may do what you want, though os.environ['APPDATA'] might be useful as
well. Of course, if you're trying to get something to work
cross-platform, things may be more difficult -- but that's because
Windows doesn't normally use ~ so its use is not supported very well.
You may be able to create a $HOME that's equivalent to $USERPROFILE...

Jeff Shannon
Technician/Programmer
Credit International
 
A

Amir Dekel

Jeff said:
Amir said:
2. os.path.expanduser("~") always gives me "C:\\" instead of my
homepath. I have tried to change my homepath in WinXP using set
homepath(in command line), and alsaw using the "Environment Variables"
in WinXP system properties, non helped. I really have to fix this
somehow.


Judging from this, I think that os.environ['USERPROFILE'] seems like it
may do what you want, though os.environ['APPDATA'] might be useful as
well. Of course, if you're trying to get something to work
cross-platform, things may be more difficult -- but that's because
Windows doesn't normally use ~ so its use is not supported very well.
You may be able to create a $HOME that's equivalent to $USERPROFILE...

Yet another reason not to use Windows and all it's silly system...
Unfotunely, I am stuck with a USB ADSL modem on a family computer, which
led to terrible headaches with my (quiet successful, actually) attempts
to install Linux.

Well, so be it...
 
F

Fernando Perez

Amir said:
Hi everyone,

I have two problems:

1. How can I keep my changes in sys.path after closing the interpreter?

As others said, use $PYTHONPATH
2. os.path.expanduser("~") always gives me "C:\\" instead of my
homepath. I have tried to change my homepath in WinXP using set
homepath(in command line), and alsaw using the "Environment Variables"
in WinXP system properties, non helped. I really have to fix this somehow.

This is what ipython uses to try and get that information in a portable manner.
Note that it uses _winreg, which is part of the win32 extensions.

In [1]: import IPython.genutils

In [2]: psource IPython.genutils.get_home_dir
def get_home_dir():
"""Return the closest possible equivalent to a 'home' directory.

For Posix systems, this is $HOME, and on NT it's $HOMEDRIVE\$HOMEPATH.

Currently only Posix and NT are implemented, a HomeDirError exception is
raised for all other OSes. """ #'

if os.name == 'posix':
return os.environ['HOME']
elif os.name == 'nt':
# For some strange reason, win9x returns 'nt' for os.name.
try:
return os.path.join(os.environ['HOMEDRIVE'],os.environ['HOMEPATH'])
except:
try:
# Use the registry to get the 'My Documents' folder.
import _winreg as wreg
key = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell
Folders")
homedir = wreg.QueryValueEx(key,'Personal')[0]
key.Close()
return homedir
except:
return 'C:\\'
elif os.name == 'dos':
# Desperate, may do absurd things in classic MacOS. May work under DOS.
return 'C:\\'
else:
raise HomeDirError,'support for your operating system not implemented.'

HTH,

f
 
B

Brian van den Broek

Jeff Shannon said unto the world upon 2004-12-16 17:54:
Well, according to os.path.expanduser()'s docstring, it uses the $HOME
environment variable to determine how to expand ~. I don't know what's
standard on Windows, but I tried checking for a $HOME and found none.
Here's (a slightly redacted copy of) what I *do* find (Win2K):

Judging from this, I think that os.environ['USERPROFILE'] seems like it
may do what you want, though os.environ['APPDATA'] might be useful as
well. Of course, if you're trying to get something to work
cross-platform, things may be more difficult -- but that's because
Windows doesn't normally use ~ so its use is not supported very well.
You may be able to create a $HOME that's equivalent to $USERPROFILE...

Jeff Shannon
Technician/Programmer
Credit International

Hi all,

some 'nix-world tool (probably xemacs) that I put on my WinMe box didn't
like the lack of a HOME environment variable, either. This was easily
solved by adding the line

SET HOME=D:\HOME

to my autoexec.bat file. (I don't know if this works in more recent
versions of Windows, but I'd imagine so.) There to, I have added a line

SET PYTHONPATH=D:\List_Of_Dirs;D:\Use_a_semi_colon_to_sep_items_on_Win

Which puts those dirs into sys.path (at the front, I believe). I also
used the .pth file trick because they two means differ as to where in
the sys.path they add their entries.

Best to all,

Brian vdB
 
A

Amir Dekel

Jeff said:
Judging from this, I think that os.environ['USERPROFILE'] seems like it
may do what you want, though os.environ['APPDATA'] might be useful as
well. Of course, if you're trying to get something to work
cross-platform, things may be more difficult -- but that's because
Windows doesn't normally use ~ so its use is not supported very well.
You may be able to create a $HOME that's equivalent to $USERPROFILE...

Both problems solved! The .pth seems like the best solution for the
first one, and for the expanduser("~") I just added a $HOME variable to
the Environment variables of WinXP. Simple solutions. Thanks Jeff

Amir
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top