More elegant way to cwd?

Peter Hansen peter at engcorp.com
Tue Dec 28 21:52:42 EST 2004


Kamilche wrote:
>> For "library" modules there are already other and more elegant means.
> 
> Well, I want to make a script execute without error regardless of where 
> the current working directory is, and without having to make the user 
> modify their PYTHONPATH variable or install something in site-packages.

If you are talking about just "a script", then this of course
works out of the box without doing anything special.  You just
run the script and it works.

If, on the other hand, the script requires access to certain
shared *other* modules, then no, if you don't tell the script
where to find those other modules, and they are not in the same
folder as the script itself, then you can't do this.  You do
have to tell the Python where to find things... I'm unclear
what your situation is, that you have shared modules that you
want to use in various other scripts, and yet you have some
strange restriction preventing you from telling Python where
those are to be found except with custom code written in each
script that will use the modules.

Even if that really describes your (very bizarre, IMHO) situation,
the least you could do is write the aforementioned code in a
nice subroutine that you just cut and paste into each module,
then call "elegantly".  It's code reuse, even if it's the most
rudimentary sort of reuse.

> Is there a way to let the user just 'drag the folder' anywhere on their 
> hard drive, and have it work, without having to modify any special 
> system variables?

Not sure exactly what you are looking for.  I certain have folders
of Python scripts that I can just drag here or there and have them
work.  When they need to use common library files, however, I take
a moment to set that up ahead of time, using .pth files if I can't
or won't install them in site-packages.

-Peter



More information about the Python-list mailing list