where is sys.path initialized?

A

Alex

Rossum's tutorial states
"Actually, modules are searched in the list of directories given by the
variable sys.path which is initialized from the directory containing
the input script (or the current directory)"

I'm running Python standard installation (ver 2.4) on Windows XP. Where
can I find the "input script" where the sys.path is defined?

Alex
 
A

Alex

No one is answering this question. Why, is it a bit unclear or what?
All I wanted to do was to add the path C:\Python24\mypython to the
sys.path so that I could put all my stuff there instead of mixing it
with the stuff that comes with the installation. And I wanted to do it
permanently som that I wouldn't have to use sys.path.append every time
I started Python interpreter.

Here is what I did, and it worked, but I still don't know why. If
somebody has an explanation please tell me.

1. My computer -> Properties->Advanced->Environment variables->System
variables ->New

2. In Variable name field enter PYTHONPATH and in Variable value field
enter C:\Python24\mypython

3. OK, OK, OK to close all the dialog boxes.

4. Restart the computer (it's essential to restart the computer and not
just the Python shell).

5. Open the Python shell and enterIf you've done exactly as I've described here you should see
C:\Python24\mypython in the list.

Alex
 
P

Peter Hansen

Alex said:
No one is answering this question. Why, is it a bit unclear or what?

No, but if you really expected a response in only four hours you need a
reality check. Many people don't receive Usenet articles for a day or
two, possibly longer, and in any case many people don't *check* their
news more often than once every day or two.

As Robert suggests, have patience. Generally one should wait several
days before giving up on a reply. Reposting is generally less than
useless (basically it's rude), though reposting with more detail or
another way of describing your problem is perfectly acceptable. After a
day or two...

-Peter
 
S

Steve M

The PYTHONPATH environment variable is good for that. For general
customizing beyond the path, you can make a file called:

C:\Python24\Lib\site-packages\sitecustomze.py

and it will be executed every time python runs. It might look like
this:

import sys
sys.path.insert(0, r'C:\Python24\mypython')
sys.setdefaultencoding('latin-1)



Where the tutorial says the sys.path is "initialized from the directory
containing the input script" I think the input script is the file whose
name is the argument to the Python interpreter. E.g., if you ran
"python c:\stuff\myscript.py" then the path is initialized relative to
c:\stuff. If you just run the interpreter in interactive mode, it's
current working directory when you run 'python'. To say that the path
is initialized "from the directory" I guess only matters if any of the
items in PYTHONPATH or the sys.path are not given as absolute pathnames
but rather relative. If my PYTHONPATH has 'mypython' in it with no
absolute qualifier then in the above scenario where I'm in, say, the
c:\windows directory and I run "python c:\stuff\myscript.py" it will be
c:\stuff\mypython that is added to the path. I think...
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top