[Tutor] PYTHONPATH: where to set on Win9x

Patrick K. O'Brien pobrien@orbtech.com
Wed, 13 Jun 2001 17:22:22 -0500


No problem, Christian. I will do my best to answer your questions. And if I
mispeak, hopefully someone will correct me. Look for comments below that
begin with [POB]. Those would be mine. <g>

---
Patrick K. O'Brien
Orbtech
"I am, therefore I think."

-----Original Message-----
From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
Christian Mascher
Sent: Wednesday, June 13, 2001 3:45 PM
To: tutor@python.org
Subject: [Tutor] PYTHONPATH: where to set on Win9x

Hello everyone,

I've been following the recent postings on implementing a
startup-configuration of some sorts, perhaps loading some modules
(pydoc) or setting up the path to arbitrary extra module-directories (my
private modules). As far as I understood the matter, the
module-search-path (shown with 'sys.path') where Python looks for and is
able to find modules must (?) be set via the environment variable
PYTHONPATH.
---
[POB] Yes, with a few exceptions and subtleties. For example, python has
some builtin places that it automatically adds to sys.path. Then the IDE
that you use (IDLE, PythonWin, VPython and Boa, just to name a few) might
make some changes to sys.path. There is also a way for 3rd party packages to
specify a directory that always gets added. And since sys.path is just a
list, you can always add on to it in your code. (In addition, python's
concept of packages can extend its reach into directories that aren't
explicitly listed in sys.path.) But the *easiest* way to have one of your
own directories *always* visible to python *no matter what* IDE you are
using is to add that directory to a SET PYTHONPATH statement in your
autoexec.bat file.
---
But that doesn't seem to be the whole story on windows, because I can
run python and import all standard modules with no explicit PYTHONPATH
set in the environment. (During installation the necessary paths must
have been hardwired into the registry.
---
[POB] Yes, but not necessarily through the registry.
---
For the same reason I can start
idle by "executing" idle.pyw from the shell (explorer) right?).
---
[POB] No. That has to do with the fact that the python installer has
associated the .py and .pyw file extensions with python.exe. So if you
double click on a file with that extension, Windows knows what to do with
that file. This is no different than what happens when you double click on a
.doc file and Word pops up. Gnome and KDE do similar things under Linux. In
Windows Explorer select the Tools | Folder Options | File Types tab to see
the associations: C:\Python21\python.exe "%1" %* (for .py files) and
C:\Python21\pythonw.exe "%1" %* (for .pyw files). Notice that the path to
python.exe is included, so it doesn't matter whether python is on your PATH
for this bit of magic to work.

If you want to be able to open a DOS box and go to any directory and type
"python" or "python myfile.py" then you need to add your python directory to
your PATH either through a batch file or as part of your autoexec.bat, which
again is the *easiest* way to go.
---

So I guess I only have to use PYTHONPATH for having my personal
directories turn up in sys.path, is that right? Meaning I only need to
put
set PYTHONPATH=d:\mypython
in autoexec.bat to include that directory. It works, but is it "the
right way" or did I miss a point?
---
[POB] Yes, that is all you need. No, I don't think you missed the point. I
think this is the right way to go. And if d:\mypython is a python package
(see
http://www.python.org/doc/current/tut/node8.html#SECTION00840000000000000000
0) you can get to subdirectories of mypython using dot notation (from
mypython.mycode.mygame.3d import specialeffects), which is pretty cool,
imho.
---

By the way, is the global autoexec.bat really the best place for putting
this? Normally, when I use a console-started compiler (like javac for
instance), I have an extra .bat-script setting the appropriate
environment-variables only when opening the shell (DOS-box) dedicated to
Java or whatever. So it is possible to add environment variables later.
Could/should this be done for the python shell as well? One could of
course append to sys.path in a standard startupscript, but then I have
to set PYTHONSTARUP first, yes?
---
[POB] Again, putting it in autoexec.bat is just easy and global. If you keep
it simple, you only need to do it once. Otherwise you need to make sure you
set up every desktop shortcut/menu choice/toolbar shortcut, etc. to go
through your batch files for every tool you use (and I actively use IDLE,
Boa, PythonWin and VPython). I would not want to have to hassle with that
myself. I have a *ton* of software loaded on this machine and my
autoexec.bat file consists of only these three lines:
SET PATH=C:\Python21
SET PYTHONPATH=C:\My Documents\pobrien\Code
SET PYTHONSTARTUP=C:\My Documents\pobrien\.pythonrc.py
---

Hope I'm not asking the same questions Patrick O'Brien and Daniel Yoo
just sorted out, but I'm really still confused about the issue.

---
[POB] Don't feel bad. I still think these environmental issues are one of
the weakest areas. I don't know if it's a lack of documentation or what. But
it seems like everyone (myself included) gets completely baffled when they
come to the environment and startup issues. I hope this helped.
---
Thanks in advance,
Christian

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor