Finding Script Directory

F

Fuzzyman

What's the best, cross platform, way of finding out the directory a
script is run from ?

I've googled a bit, but can't get a clear answer.

On sys.argv[0] the docs say :
argv[0] is the script name (it is operating system dependent whether
this is a full pathname or not).

So this doesn't seem to be the answer.

The script directory is always *somewhere* in sys.path - but not
always in position 0. If you use py2exe then sys.path[0] is the
zipfile it does the imports from !!

Regards,

Fuzzy

http://www.voidspace.org.uk/atlantibots/pythonutils.html
 
P

Peter Hansen

Fuzzyman said:
What's the best, cross platform, way of finding out the directory a
script is run from ?

I've googled a bit, but can't get a clear answer.

I've seen twenty threads on this and I still don't know/recall
whether there's a clear answer. :-(
On sys.argv[0] the docs say :
argv[0] is the script name (it is operating system dependent whether
this is a full pathname or not).

So this doesn't seem to be the answer.

Actually, it's all you've got to work with. On Linux and Windows,
at least, it's got either just the script name, if you're running
from the current directory, or a path (relative or absolute) to
the directory where the script is run from. It's like this whether
py2exe'd or not, too.

__file__ could be of assistance, but it doesn't exist when
you've py2exe the thing...
The script directory is always *somewhere* in sys.path - but not
always in position 0. If you use py2exe then sys.path[0] is the
zipfile it does the imports from !!

Yeah, sys.path isn't much good for this sort of thing.

-Peter
 
L

Larry Bates

Would os.curdir() help? It as least tells you the
current directory. My solution to what I think is
your problem is to put an installdirectory= statement
in my configuration file that gets updated when the
application is installed. That way I always know
where the script is installed. I use Inno Installer
and it is very easy to update configuration files
during installation.

HTH,
Larry Bates
Syscon, Inc.
 
S

Steve Holden

Peter said:
Fuzzyman said:
What's the best, cross platform, way of finding out the directory a
script is run from ?

I've googled a bit, but can't get a clear answer.


I've seen twenty threads on this and I still don't know/recall
whether there's a clear answer. :-(
On sys.argv[0] the docs say :
argv[0] is the script name (it is operating system dependent whether
this is a full pathname or not).

So this doesn't seem to be the answer.


Actually, it's all you've got to work with. On Linux and Windows,
at least, it's got either just the script name, if you're running
from the current directory, or a path (relative or absolute) to
the directory where the script is run from. It's like this whether
py2exe'd or not, too.

__file__ could be of assistance, but it doesn't exist when
you've py2exe the thing...
The script directory is always *somewhere* in sys.path - but not
always in position 0. If you use py2exe then sys.path[0] is the
zipfile it does the imports from !!


Yeah, sys.path isn't much good for this sort of thing.

-Peter

Oh, come now, Peter, you've seen this problem solved more times than you
can shake a stick at. The required code is

import os.path, sys
print os.path.abspath(sys.argv[0])

How hard can that be to remember? Didn't realise you were getting so OLD
;-) ...

regards
STeve
 
S

Steve

Isn't gmail really cool ?? here's one of the text links in the
"Related Pages" column at the right of the page displaying this
thread:

7.2. Finding the path
a free Python book for experienced programmers
www.faqs.org

The link points to: http://www.faqs.org/docs/diveintopython/regression_path.html

I'm glad google's reading my mail :)

HTH
Steve
- Hide quoted text -

Fuzzyman said:
What's the best, cross platform, way of finding out the directory a
script is run from ?

I've googled a bit, but can't get a clear answer.

I've seen twenty threads on this and I still don't know/recall
whether there's a clear answer. :-(
On sys.argv[0] the docs say :
argv[0] is the script name (it is operating system dependent whether
this is a full pathname or not).

So this doesn't seem to be the answer.

Actually, it's all you've got to work with. On Linux and Windows,
at least, it's got either just the script name, if you're running
from the current directory, or a path (relative or absolute) to
the directory where the script is run from. It's like this whether
py2exe'd or not, too.

__file__ could be of assistance, but it doesn't exist when
you've py2exe the thing...
The script directory is always *somewhere* in sys.path - but not
always in position 0. If you use py2exe then sys.path[0] is the
zipfile it does the imports from !!

Yeah, sys.path isn't much good for this sort of thing.
 
P

Peter Hansen

Steve said:
Peter said:
I've seen twenty threads on this and I still don't know/recall
whether there's a clear answer. :-(
Oh, come now, Peter, you've seen this problem solved more times than you
can shake a stick at. The required code is

import os.path, sys
print os.path.abspath(sys.argv[0])

How hard can that be to remember? Didn't realise you were getting so OLD
;-) ...

Well, I just put that in a nice little .exe created by py2exe,
and running from a directory in my PATH.

Amazing, but when I run it with the current directory set to C:\
it prints only "C:\testpath", and yet the script (well, the EXE)
is actually in a directory called c:\dev\prj189~1\dist ...

Fuzzyman's right: there isn't a clear, conclusive answer, I believe.

(But the above recipe, which I'm sure he found via google,
should work in almost all cases, py2exe and perhaps certain OSes
excluded.)

-but-that-doesn't-mean-i'm-not-getting-old-ly y'rs,
Peter
 
T

Thomas Heller

Peter Hansen said:
Fuzzyman said:
What's the best, cross platform, way of finding out the directory a
script is run from ?
I've googled a bit, but can't get a clear answer.

I've seen twenty threads on this and I still don't know/recall
whether there's a clear answer. :-(
On sys.argv[0] the docs say :
argv[0] is the script name (it is operating system dependent whether
this is a full pathname or not).
So this doesn't seem to be the answer.

Actually, it's all you've got to work with. On Linux and Windows,
at least, it's got either just the script name, if you're running
from the current directory, or a path (relative or absolute) to
the directory where the script is run from. It's like this whether
py2exe'd or not, too.

__file__ could be of assistance, but it doesn't exist when
you've py2exe the thing...
The script directory is always *somewhere* in sys.path - but not
always in position 0. If you use py2exe then sys.path[0] is the
zipfile it does the imports from !!

Yeah, sys.path isn't much good for this sort of thing.

The samples\simple\hello.py script can be used for experimentation, and
it prints all the items that may be useful. Here is the output when
the exe-file's directory is on the PATH:

c:\>hello
Hello from py2exe
frozen 'console_exe'
sys.path ['c:\\sf\\py2exe\\py2exe\\samples\\simple\\dist\\library.zip']
sys.executable c:\sf\py2exe\py2exe\samples\simple\dist\hello.exe
sys.prefix c:\sf\py2exe\py2exe\samples\simple\dist
sys.argv ['hello']

The 'sys.frozen' attribute should be used to determine if the
script runs from py2exe or from a Python module.

Thomas
 
P

Peter Hansen

Thomas said:
The 'sys.frozen' attribute should be used to determine if the
script runs from py2exe or from a Python module.

Okay, so here's my first attempt (without checking whether there
is already one in the Cookbook or somewhere) at 'the' canonical
way to find the directory in which the main script or .exe (for frozen
apps) is found. Note that this uses sys.frozen and therefore
works for frozen apps only if they follow the py2exe mechanism.
(Do any of the others?)

import sys
import os
def getRunDir():
try:
sys.frozen
except AttributeError:
path = sys.argv[0]
else:
path = sys.executable

return os.path.dirname(os.path.abspath(path))

Comments? Anyone have a better name?

-Peter
 
T

Thomas Heller

Peter Hansen said:
Thomas said:
The 'sys.frozen' attribute should be used to determine if the
script runs from py2exe or from a Python module.

Okay, so here's my first attempt (without checking whether there
is already one in the Cookbook or somewhere) at 'the' canonical
way to find the directory in which the main script or .exe (for frozen
apps) is found. Note that this uses sys.frozen and therefore
works for frozen apps only if they follow the py2exe mechanism.
(Do any of the others?)

import sys
import os
def getRunDir():
try:
sys.frozen
except AttributeError:
path = sys.argv[0]
else:
path = sys.executable

return os.path.dirname(os.path.abspath(path))

Comments? Anyone have a better name?

Very similar to what I have. Here's my version (os.path.abspath should
probably be added). I hope it also works with Installer, freeze,
cx_Freeze, and earlier versions of py2exe (although I never tested that):

import imp, os, sys

def main_is_frozen():
return (hasattr(sys, "frozen") or # new py2exe
hasattr(sys, "importers") # old py2exe
or imp.is_frozen("__main__")) # tools/freeze

def get_main_dir():
if main_is_frozen():
return os.path.dirname(sys.executable)
return os.path.dirname(sys.argv[0])
 
S

Steve Holden

Peter Hansen wrote:
[...]
Well, I just put that in a nice little .exe created by py2exe,
and running from a directory in my PATH.

Amazing, but when I run it with the current directory set to C:\
it prints only "C:\testpath", and yet the script (well, the EXE)
is actually in a directory called c:\dev\prj189~1\dist ...

Fuzzyman's right: there isn't a clear, conclusive answer, I believe.

(But the above recipe, which I'm sure he found via google,
should work in almost all cases, py2exe and perhaps certain OSes
excluded.)

-but-that-doesn't-mean-i'm-not-getting-old-ly y'rs,
Peter

Happily it's still true that the fastest way to get the right answer on
a newsgroup is to suggest the wrong one!

regards
Steve
 
F

Fuzzyman

Larry Bates said:
Would os.curdir() help? It as least tells you the
current directory. My solution to what I think is
your problem is to put an installdirectory= statement
in my configuration file that gets updated when the
application is installed. That way I always know
where the script is installed. I use Inno Installer
and it is very easy to update configuration files
during installation.

HTH,
Larry Bates
Syscon, Inc.
This might be the answer to my problem, part of the problem I'm trying
to solve is an install of a py2exe'd program that is installed with
innosetup.

I'll have to look into this.
Otherwise the magic with os.path.abspath(sys.argv[0]) should do the
trick...

Regards,

Fuzzy
Fuzzyman said:
What's the best, cross platform, way of finding out the directory a
script is run from ?

I've googled a bit, but can't get a clear answer.

On sys.argv[0] the docs say :
argv[0] is the script name (it is operating system dependent whether
this is a full pathname or not).

So this doesn't seem to be the answer.

The script directory is always *somewhere* in sys.path - but not
always in position 0. If you use py2exe then sys.path[0] is the
zipfile it does the imports from !!

Regards,

Fuzzy

http://www.voidspace.org.uk/atlantibots/pythonutils.html
 
F

Fuzzyman

Thomas Heller said:
Peter Hansen said:
Thomas said:
The 'sys.frozen' attribute should be used to determine if the
script runs from py2exe or from a Python module.

Okay, so here's my first attempt (without checking whether there
is already one in the Cookbook or somewhere) at 'the' canonical
way to find the directory in which the main script or .exe (for frozen
apps) is found. Note that this uses sys.frozen and therefore
works for frozen apps only if they follow the py2exe mechanism.
(Do any of the others?)

import sys
import os
def getRunDir():
try:
sys.frozen
except AttributeError:
path = sys.argv[0]
else:
path = sys.executable

return os.path.dirname(os.path.abspath(path))

Comments? Anyone have a better name?

Very similar to what I have. Here's my version (os.path.abspath should
probably be added). I hope it also works with Installer, freeze,
cx_Freeze, and earlier versions of py2exe (although I never tested that):

import imp, os, sys

def main_is_frozen():
return (hasattr(sys, "frozen") or # new py2exe
hasattr(sys, "importers") # old py2exe
or imp.is_frozen("__main__")) # tools/freeze

def get_main_dir():
if main_is_frozen():
return os.path.dirname(sys.executable)
return os.path.dirname(sys.argv[0])


Thanks - that should more than do the trick !
Now just to go back and change all my scripts that probably only work
if run from the right directory.... (assuming files they need will be
in the current directory).. *sigh*

Regards,


Fuzzy

http://www.voidspace.org.uk/atlantibots/pythonutils.html
Fuzzy

http://
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top