Connecting Tech Pros Worldwide Forums | Help | Site Map

More elegant way to cwd?

Kamilche
Guest
 
Posts: n/a
#1: Jul 18 '05
Is there a more elegant way to change the working directory of Python
to the directory of the currently executing script, and add a folder
called 'Shared' to the Python search path?

This is what I have. It seems like it could be shorter, somehow.

# Switch Python to the current directory
import os, sys
pathname, scriptname = os.path.split(sys.argv[0])
pathname = os.path.abspath(pathname)
os.chdir(pathname)
s = os.path.realpath('..')
s = os.path.join(s, 'Shared')
sys.path.append(s)

Thanks for any enhancements you can suggest!

M.E.Farmer
Guest
 
Posts: n/a
#2: Jul 18 '05

re: More elegant way to cwd?


Hello,
Seems you found os.chdir(), but you missed os.getcwd()
;)
M.E.Farmer

M.E.Farmer
Guest
 
Posts: n/a
#3: Jul 18 '05

re: More elegant way to cwd?


Ignore my last post to much eggnog I guess. I must have parsed it to
quickly.

M.E.Farmer

Peter Hansen
Guest
 
Posts: n/a
#4: Jul 18 '05

re: More elegant way to cwd?


Kamilche wrote:[color=blue]
> Is there a more elegant way to change the working directory of Python[/color]

That depends on how you define "elegant", I guess.
[color=blue]
> to the directory of the currently executing script, and add a folder
> called 'Shared' to the Python search path?
>
> This is what I have. It seems like it could be shorter, somehow.[/color]

It could be shorter if you were willing to combine several
function calls on the same line, but I get the impression
you wouldn't consider that more elegant...
[color=blue]
> # Switch Python to the current directory
> import os, sys
> pathname, scriptname = os.path.split(sys.argv[0])
> pathname = os.path.abspath(pathname)
> os.chdir(pathname)
> s = os.path.realpath('..')
> s = os.path.join(s, 'Shared')
> sys.path.append(s)
>
> Thanks for any enhancements you can suggest![/color]

Other than using os.pardir instead of '..', and possibly adding
an "os.path.abspath()" call to the last bit (or does realpath
already do that? It's unclear from the docs), I can't see
anything fundamental I'd do differently... except package these
functions up as nice clean subroutines, possibly in a library
package, that I could then use in code that would magically
become "elegant" (IMHO) by avoiding the repetition of all
that non-elegant stuff above...

-Peter
F. Petitjean
Guest
 
Posts: n/a
#5: Jul 18 '05

re: More elegant way to cwd?


On Mon, 27 Dec 2004 11:53:57 -0500, Peter Hansen <peter@engcorp.com> wrote:[color=blue]
> Kamilche wrote:[color=green]
>> Is there a more elegant way to change the working directory of Python[/color]
>
> That depends on how you define "elegant", I guess.
>[color=green]
>> to the directory of the currently executing script, and add a folder
>> called 'Shared' to the Python search path?
>>
>> This is what I have. It seems like it could be shorter, somehow.[/color]
>
> It could be shorter if you were willing to combine several
> function calls on the same line, but I get the impression
> you wouldn't consider that more elegant...
>[color=green]
>> # Switch Python to the current directory
>> import os, sys
>> pathname, scriptname = os.path.split(sys.argv[0])
>> pathname = os.path.abspath(pathname)
>> os.chdir(pathname)
>> s = os.path.realpath('..')
>> s = os.path.join(s, 'Shared')
>> sys.path.append(s)
>>
>> Thanks for any enhancements you can suggest![/color]
>[/color]
How about ?
import os, sys
pathname, scriptname = os.path.split(sys.argv[0])
pathname = os.path.abspath(pathname)
os.chdir(pathname) # only if you really need to cwd
parent = os.path.dirname(pathname)
parent = os.path.join(parent, 'Shared')
if parent not in sys.path:
sys.path.append(parent)


Or even :
pathname, scriptname = os.path.split(__file__)
# pathname should be an absolute path
Kamilche
Guest
 
Posts: n/a
#6: Jul 18 '05

re: More elegant way to cwd?


> For "library" modules there are already other and more elegant means.[color=blue]
>
> -Peter[/color]

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.

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?
Peter Hansen
Guest
 
Posts: n/a
#7: Jul 18 '05

re: More elegant way to cwd?


Kamilche wrote:[color=blue][color=green]
>> For "library" modules there are already other and more elegant means.[/color]
>
> 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.[/color]

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.
[color=blue]
> 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?[/color]

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
Closed Thread