[Baypiggies] Locating the directory you are executing from
Kelly Yancey
kelly at nttmcl.com
Thu May 8 04:36:10 CEST 2008
Shannon -jj Behrens wrote:
> Furthermore, if you run your script like "$ script.py", and it runs
> because "." is in $PATH, I'm guessing that argv[0] won't have the full
> path, but __file__ will. (By the way, "." is not in my path.)
>
> -jj
>
Maybe on linux and un*x-like systems.
Unfortunately, on Windows this is not the case: argv[0] == __file__
for the main script *unless* you compile your script into an executable
using py2exe...in which case __file__ is not defined for the main script.
> type test.py
import sys
import os
print "sys.executable :", os.path.dirname(sys.executable)
print "sys.argv[0] :", os.path.dirname(sys.argv[0])
print "__file__ :", os.path.dirname(__file__)
> python test.py
sys.executable : c:\python25
sys.argv[0] :
__file__ :
> python "c:\Documents and Settings\Kelly"\test.py"
sys.executable : c:\python25
sys.argv[0] : c:\Documents and Settings\Kelly
__file__ : c:\Documents and Settings\Kelly
Using py2exe to build a standalone executable...
> type setup.py
from distutils.core import setup
import py2exe
setup(console=['test.py'])
> test.exe
sys.executable : c:\Documents and Settings\Kelly\dist
sys.argv[0] :
__file__ :
Traceback (most recent call last):
File "test.py", line 5, in ?
NameError: name '__file__' is not defined
> "c:\Documents and Settings\Kelly\dist\test.exe"
sys.executable : c:\Documents and Settings\Kelly\dist
sys.argv[0] : c:\Documents and Settings\Kelly\dist
__file__ :
Traceback (most recent call last):
File "test.py", line 5, in ?
NameError: name '__file__' is not defined
For Windows, sys.argv[0] would appear to be the best bet. If this is
different on un*x-like systems, though, then people just need to beware
when trying to write portable code.
Kelly
> On Tue, May 6, 2008 at 11:04 PM, Atul Varma <varmaa at gmail.com> wrote:
>> I think one nice thing about using __file__ is that it doesn't necessarily
>> require your Python file to be the main script that's being executed, as
>> sys.argv[0] does. For instance, I could have my own script called "foo",
>> and then import "bar", which loads something relative to its (i.e., bar's)
>> location in the filesystem. If "bar" uses sys.argv[0], it would find the
>> path to foo, but if it uses __file__, it will always find the correct path
>> regardless of whether it's imported as a module or run as a script.
>>
>> - Atul
>>
>>
>>
>> On Tue, May 6, 2008 at 10:22 PM, Kelly Yancey <kelly at nttmcl.com> wrote:
>>>
>>>
>>>
>>> Shannon -jj Behrens wrote:
>>>
>>>> On Sun, May 4, 2008 at 1:15 PM, Aahz <aahz at pythoncraft.com> wrote:
>>>>
>>>>> On Sun, May 04, 2008, Jeff Younker wrote:
>>>>> >
>>>>> > I have a set of development scripts. The scripts can potentially
>>>>> > be executed from many places. They reference information which is
>>>>> > relative to their installation paths. (These are build scripts.)
>>>>> >
>>>>> > The CI system can supply a fixed root, but when run by the
>> developers
>>>>> > I'd like them to be runnable from anywhere in the project, and
>> still
>>>>> > have them work.
>>>>>
>>>>> If you're willing to require that the script directory NOT be on the
>>>>> path, just do some manipulation of sys.argv[0]. Otherwise, __file__
>> is
>>>>> the right approach.
>>>>>
>>>> Heh, I have this same problem ;)
>>>>
>>>> -jj
>>>>
>>>>
>>> Here is the snippet I've been using. It supports running both as a
>> python script and as a binary created by py2exe. It also works on systems
>> that return paths encoded with with non-ASCII character sets (e.g. non-U.S.
>> version of Windows).
>>> import locale
>>> import os
>>> import sys
>>>
>>> if hasattr(sys, 'frozen'):
>>> # Running as executable generated by py2exe.
>>> runpath = sys.executable
>>> else:
>>> # Running as python script.
>>> runpath = sys.argv[0]
>>> rundir = os.path.dirname(unicode(runpath,
>>> locale.getpreferredencoding()))
>>>
>>> I am curious though: how is extracting the path from __file__ better than
>> extracting it from sys.argv[0]? The best I can tell (from simple test
>> scripts) is that both yield the same results.
>>> Kelly
>>>
>>> --
>>> Kelly Yancey
>>> http://kbyanc.blogspot.com/
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> Baypiggies mailing list
>>> Baypiggies at python.org
>>> To change your subscription options or unsubscribe:
>>> http://mail.python.org/mailman/listinfo/baypiggies
>>>
>>
>
>
>
More information about the Baypiggies
mailing list