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/atlantib...thonutils.html 11 2170
Fuzzyman wrote: 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
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.
"Fuzzyman" <mi*****@foord.net> wrote in message
news:80**************************@posting.google.c om... 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/atlantib...thonutils.html
Peter Hansen wrote: Fuzzyman wrote:
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
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/diveintopyt...sion_path.html
I'm glad google's reading my mail :)
HTH
Steve
- Hide quoted text -
On Wed, 07 Jul 2004 09:58:20 -0400, Peter Hansen <pe***@engcorp.com> wrote: Fuzzyman wrote:
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.
Steve Holden wrote: Peter Hansen wrote: Fuzzyman wrote: 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. :-( 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
Peter Hansen <pe***@engcorp.com> writes: Fuzzyman wrote:
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\\li brary.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
Thomas Heller wrote: 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
Peter Hansen <pe***@engcorp.com> writes: Thomas Heller wrote:
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])
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
"Larry Bates" <lb****@swamisoft.com> wrote in message news:<MK********************@comcast.com>... 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" <mi*****@foord.net> wrote in message news:80**************************@posting.google.c om... 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/atlantib...thonutils.html
Thomas Heller <th*****@python.net> wrote in message news:<7j**********@python.net>... Peter Hansen <pe***@engcorp.com> writes:
Thomas Heller wrote:
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/atlantib...thonutils.html
Fuzzy
http:// This discussion thread is closed Replies have been disabled for this discussion. Similar topics
4 posts
views
Thread by Kevin Thorpe |
last post: by
|
1 post
views
Thread by Todd Johnson |
last post: by
|
reply
views
Thread by Gabriel Cooper |
last post: by
|
3 posts
views
Thread by runes |
last post: by
|
12 posts
views
Thread by jeff elkins |
last post: by
|
2 posts
views
Thread by Robb Meade |
last post: by
|
22 posts
views
Thread by Tony Houghton |
last post: by
|
4 posts
views
Thread by Larry |
last post: by
|
12 posts
views
Thread by Jeff |
last post: by
| | | | | | | | | | |