471,601 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,601 software developers and data experts.

In search of idiom in py2exe

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","someimag e.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.
Jul 18 '05 #1
3 1823
David wrote:
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/groups?threa...ing.google.com

-Peter
Jul 18 '05 #2
On Tue, 07 Sep 2004 22:08:21 -0400, Peter Hansen <pe***@engcorp.com>
wrote:

Thanks! That's the ticket.
David wrote:
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/groups?threa...ing.google.com

-Peter


Jul 18 '05 #3
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.
<David Morgenthaler> wrote in message
news:dq********************************@4ax.com...
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","someimag e.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.

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by RJS | last post: by
1 post views Thread by | last post: by
2 posts views Thread by Stefan Behrens | last post: by
8 posts views Thread by Kathleen Kudzma | last post: by
reply views Thread by Steven Bell | last post: by
6 posts views Thread by Luc Saffre | last post: by
2 posts views Thread by Ola K | last post: by
reply views Thread by Larry Bates | last post: by
reply views Thread by Jimmy Retzlaff | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.