[Tutor] Windows questions

Jeff Shannon jeff at ccvcorp.com
Tue Sep 2 11:37:42 EDT 2003


Robin Hood wrote:

> 1. How do you set the PATH environment variable for python under windows 98
> (So that I can import modules which aren't located in the same folder as
> python itself)

The simplest way to do this is to include a file named python.pth in 
your Python directory -- the filename itself doesn't matter, as long 
as the extension is '.pth'.  Each line of this file should contain a 
directory name; every directory that's specified in that file will be 
added to your sys.path, and thus searched for modules.

You can also create (in your autoexec.bat or the like) an environment 
variable named PYTHONPATH, formatted like the PATH variable; those 
directories will also be searched for modules.

> 2. Currently, to execute a python script I have to right click it, choose
> "edit with IDLE" then choose "run script" from one of the drop down menus.
> If I just double click on a python script. It automatically executes under
> the DOS interpreter, and then closes itself too quickly to see what the
> results were. Is it possible to change this? (That is, make it so that the
> program doesn't automatically close the DOS Prompt after its finished
> executing, or get it to automatically execute under the Windows IDLE)

One option is to simply add a final line to the script -- 
'raw_input("Press Enter to close this program")'.  You could also just 
always start scripts from a command prompt, but that's a bit of a 
pain.  For a more permanent solution, you need to convince Windows to 
run your scripts with the -i option to Python -- that option will 
leave Python in interactive mode once the script finishes running.  To 
do this, you need to go to Windows Explorer's Tools menu, select 
'Folder Options', and go to the File Types tab.  Find the entry for PY 
files, click on 'Advanced', then select the 'open' entry and hit the 
Edit button.  You'll see the command line that Windows uses when you 
open files of this type.  Now, insert '-i' just after 'python.exe'. 
You'll probably want to do the same thing for compiled python files 
(PYC/PYO).  Instead of altering the existing open command, you could 
create a new command that would use the interactive mode, which would 
let you have the standard behavior for most uses but allow you to 
right-click and select your new command when you need it.

Jeff Shannon
Technician/Programmer
Credit International




More information about the Tutor mailing list