473,287 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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

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
Jul 18 '05 #1
9 2412
Amir Dekel <ad****@ort.org.il> writes:
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
<le***@telus.net>
Jul 18 '05 #2

Maybe, set environment variable PYTHONPATH. More details at

<http://www.python.org/doc/current/tut/node8.html>

/Jean Brouwers

In article <fz**********@telus.net>, Lenard Lindstrom <le***@telus.net>
wrote:
Amir Dekel <ad****@ort.org.il> writes:
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
<le***@telus.net>

Jul 18 '05 #3
Lenard Lindstrom wrote:
Amir Dekel <ad****@ort.org.il> writes:
Hi everyone,

I have two problems:

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


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

Jul 18 '05 #4
Jeff Shannon wrote:

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
Jul 18 '05 #5
Amir Dekel wrote:
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):
for key, val in os.environ.items(): .... 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

Jul 18 '05 #6
Jeff Shannon wrote:
Amir Dekel wrote:
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...
Jul 18 '05 #7
Amir Dekel wrote:
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\Explore r\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

Jul 18 '05 #8
Jeff Shannon said unto the world upon 2004-12-16 17:54:
Amir Dekel wrote:
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):


<SNIP>
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

Jul 18 '05 #9
Jeff Shannon wrote:

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
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Timothy Madden | last post by:
Hello all I'm trying to include some files in an included file. Think of some scripts like this /index.php /scripts/logging.php /scripts/config.php /scripts/db_access.php
1
by: Holger Joukl | last post by:
Hi there, 2 questions regarding build/installation issues: 1. In the python 2.3.3 setup.py script, the detect_modules method of class PyBuildExt contains the following code: 253 if...
5
by: jason | last post by:
Can anyone help me find a solution to quickly working out relative paths to a folder in the root of my server... Although it easy when you go - say - two levels down: .../includes it...
24
by: Lovely Angel | last post by:
Dear Friends Hope you doing great. I have recently shifted to a webhost which is Using Windows 2003 and IIS 6. Now my application was working fine earlier but now I am facing this problem ...
11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
5
by: David Mathog | last post by:
One thing that can make porting C code from one platform to another miserable is #include. In particular, the need to either place the path to an included file within the #include statement or to...
8
by: nick | last post by:
I have a problem and I've been using a cheezy work around and was wondering if anyone else out there has a better solution. The problem: Let's say I have a web application appA. Locally, I set...
3
by: Ankit Aneja | last post by:
I have a strange situation and I have no idea how to solve this. Its a Recruitment Search Page,in the Admin Page, for every button click event the Admin Person has to create a checkbox on the users...
3
by: JeffDotNet | last post by:
I wrote a small data processing application that writes a summary of several hundred files. I use drag and drop on a panel (Panel1) to grab the absolute path to each of these files. Then I begin...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.