replacement for __file__ in compiled exe

T

TechieInsights

__file__ command does not work when compiled to exe. It makes since
because the file is now in a compressed library. Is there a
replacement or something else you can do? The real problem is that
when you create an exe of your program with python embedded, you can't
always guarantee that your current directory is the directory of your
program. I guess when you could just set a registry entry on
windows... but it would be nice to have a quick fix for this (like
os.path.dirname(__file__)).

Thanks,

Greg
 
J

John Machin

__file__ command does not work when compiled to exe.  It makes since
because the file is now in a compressed library.  Is there a
replacement or something else you can do?  The real problem is that
when you create an exe of your program with python embedded, you can't
always guarantee that your current directory is the directory of your
program.

How can you *ever* guarantee that the current directory is the same as
the directory in which the program resides? This lack of guarantee is
quite independent of whether the "program" is .py, .exe, .bat, .com,
etc. Isn't the real problem how to find out which directory the
program is in?
 I guess when you could just set a registry entry on
windows... but it would be nice to have a quick fix for this (like
os.path.dirname(__file__)).

if hasattr(sys, 'frozen'):
answer = os.path.split(sys.executable)[0]
else:
answer = os.path.split(sys.argv[0])[0]

(maybe) ... your question is a little unclear.

HTH,
John
 
T

TechieInsights

Yes, that is my exact question. How do you get the file path of your
script/program. Normally in python you can use __file__ and it will
return the file path of the script you are in, however, when you
compile the script to exe py2exe (or whatever util you are using)
compresses them into a zip folder... not usually a problem, except
when you want the access the file path of the exe.

Thanks for the answer... I should have thought of that all along...
just look on the system path. Sometimes the answer was right in front
of your face the whole time...

Greg



__file__ command does not work when compiled to exe.  It makes since
because the file is now in a compressed library.  Is there a
replacement or something else you can do?  The real problem is that
when you create an exe of your program with python embedded, you can't
always guarantee that your current directory is the directory of your
program.

How can you *ever* guarantee that the current directory is the same as
the directory in which the program resides? This lack of guarantee is
quite independent of whether the "program" is .py, .exe, .bat, .com,
etc. Isn't the real problem how to find out which directory the
program is in?
 I guess when you could just set a registry entry on
windows... but it would be nice to have a quick fix for this (like
os.path.dirname(__file__)).

    if hasattr(sys, 'frozen'):
        answer = os.path.split(sys.executable)[0]
    else:
        answer = os.path.split(sys.argv[0])[0]

(maybe) ... your question is a little unclear.

HTH,
John
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top