In search of idiom in py2exe

  • Thread starter David Morgenthaler
  • Start date
D

David Morgenthaler

In many of my scripts I've used the following idiom for accessing data
files placed nearby:

BASEDIR = os.path.dirname(__file__)
..
..
..
fp = file(os.path.join(BASEDIR,"somefile.txt"))
..
..
..
img = image.open(os.path.join(BASEDIR,"images","someimage.jpg"))


This works well with my cohorts who place their work on different
drives, in different directories, etc.

However, when I use py2exe to make an executable, the name '__file__'
is not defined.

Can anyone recommend an idiom that will work with py2exe?

Thanks in advance,
Dave M.

P.S. -- We've also tried the idiom

BASEDIR = os.path.dirname(sys.argv[0])

but the result can be misleading, for example, if the script is
launched from a desktop shortcut.
 
P

Peter Hansen

David said:
In many of my scripts I've used the following idiom for accessing data
files placed nearby: ....
Can anyone recommend an idiom that will work with py2exe?

Do the last few posts to the following thread help you? It
was an attempt to find "the" idiomatic way to find the
directory of the main script, whether running normally or
with py2exe.

http://groups.google.ca/[email protected]

-Peter
 
L

Larry Bates

After struggling with this for a while I came up with
what method that ALWAYS works for me. I use some sort
of .INI configuration file in almost 100% of my programs
(particularly the ones that I "freeze" with py2exe). I
routinely put a parameter in that .INI file that is
something like:

[defaults]
installpath=<installer will update this line>
..
.. Followed by other .INI configuration parameters
..

Then I put a lines in my Inno Installer script that are
something like:

[INI]
;
; Set the paths in progfile.INI to point to where the
; program was installed
;
Filename: "{app}\progfile.ini"; Section: "defaults";
Key: "installpath"; String: "{app}" (all on 1 line)

This allows the user to install the application anywhere
they wish AND I have a foolproof way of finding my way
back to the program file's installation directory. May
not be as elegant as some other approaches but it is
easily understandable and seems to work 100% of the time.

Larry Bates
Syscon, Inc.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top