[Tutor] Python 2.0 newbie questions

alan.gauld@bt.com alan.gauld@bt.com
Tue, 31 Oct 2000 11:21:40 -0000


> > >In which case you should also add to your AUTOEXEC:
> > >SET PYTHONPATH=C:\PYTHON20\MYPYTHON
> 
>    > Then you don't need to do this:
>    > import os
>    > os.chdir('C:\PYTHON20\MYPYTHON')    #make sure to 
> 
>     Is this in anyway wrong? meaning is it dangerous or flatout wrong

No, its not wrong but its not very portable to have that hard 
path in your program. If anyone else trires to use it they will 
be forced to keep their modules in that exact same folder...

PYTHONPATH means you can create a single place for all your 
Python modules and then use them in all new projects.

Thus I have a folder:

E:\PROJECTS\PYTHON

Under that I create a LIB folder which I add to my PYTHONPATH:

SET PYTHONPATH=E:\PROJECTS\PYTHON\LIB

Now for each froject, spam say, I create a new project 
folder:

 E:\PROJECTS\PYTHON\SPAM

And place the top level and experimental code there. But once 
I get a module working I move it to the LIB folder where 
Python can see it, not just for the current project but for 
every project, thus enabling reuse.

Doing it your way you have to 
a) hard code the folder name into any program thats going 
to import the module and

b) can only import modules from one folder at a time, which 
reduces(eliminates!) reuse across projects.

>     to import modules this way?. I just find that it's a 
> little quicker to use
>     "import os/os.chdir(' ')" and include the path in the IDLE path
> browser
>     than to add to my already bloated AUTOEXEC and reboot windows
>     everytime I create a new project folder.

Try this then:

Create a batch file containing the set PYTHONPATH=....
called setpypath.bat

Call that from AUTOEXEC.BAT.

Then while programming update that file then just execute it:

C:\PROJDIR> C:\SETPYPATH.BAT

That will reset the environment and next time python runs 
it will correctly find your modules. No need for a reboot.
Of course the next time you reboot all the changes will 
automatically be picked up too.

HTH,

Alan G.

PS It might be necessary in AUTOEXEC to use CALL to exec 
the SETPYPATH.BAT, I can't remember if thats still needed 
in Win9x DOS.