[Tutor] New

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri Dec 27 03:37:01 2002


On Thu, 26 Dec 2002, selevin wrote:

> I am very new to this but I want to learn.

Hi Selevin,

Very cool; we'll do what we can to help.


> How do I cause the directory containing the file named python.exe to be
> listed in my system environment variable named path.

Before I say anything about this: are you sure you need to touch PATH? You
should not need to touch your environmental PATH unless you're doing
something slightly unusual.  Can you tell us more why you want to do this?

Setting PATH is very specific to the operating system of your system ---
it has very little to do with Python, which is why most Python
documentation will ignore the issue.  On Windows 95/98, the file
AUTOEXEC.BAT defines this environmental variable, and changes to it
require a reboot to get things to take effect.  On Windows 2k, it's
somewhere deep in the Control Panel/System applet, in the "Advanced" tab.

Setting path is not a uniform thing to explain, so we need more
information about your system.



> How do I set a path

Python has a function called 'os.chdir()' that will allow us to change the
current working directory of a program.  We can see more information about
it here:

    http://www.python.org/doc/lib/os-file-dir.html



> and make the directory containing my new script file become the current
> directory.

Hmmm... this might be slightly trickier --- your script may be
symbolically linked from several different places.

Can you tell us why you're trying to do this, though?


The name of your script should live in the list variable 'sys.argv', as
the first element of that list.  Here is an example that shows what
sys.argv[0] might look like:

###
dyoo@hkn:~$ cat bin/foo.py
#!/usr/bin/env python
import sys
print sys.argv[0]


dyoo@hkn:~$ bin/foo.py
bin/foo.py
###


So it might just be enough to pull out the 'path' portion of sys.argv[0],
and do a os.chdir() into that directory.  The 'os.path.dirname()' function
is good at extracting directory paths from a filename:

    http://www.python.org/doc/lib/module-os.path.html


If you have more questions, please feel free to ask on Tutor.  Good luck!