Thanks to everyone who has helped me see through this stuff -- even if all I see is that it's more of a mess than I thought.
I'm slightly leery of sys.argv[0] because the docs say "argv[0] is the script name (it is operating system dependent whether this is a full pathname or not)." I know there are also reasons to be leery of os.getcwd(). For the moment, I'm settling on this code:
if sys.platform == 'darwin': # Mac oddity
basedir = os.getcwd()
if string.find(basedir, '/Contents/Resources') != -1:
os.chdir('../../..')
basedir = os.getcwd()
dfilename = basedir + '/PROSE.DIC'
gfilename = basedir + '/PROSE.GRA'
It works on the Mac in either Terminal mode or py2app mode; I'll test it on Windows when I get to a Windows machine again -- I'll see if I'm right that the straight getcwd() is *likely* to return the directory from which the Windows executable was launched. (Obviously not guaranteed.)
Charles Hartman
Professor of English, Poet in Residence
http://cherry.conncoll.edu/cohar
http://villex.blogspot.com
Charles Hartman wrote:
I'm slightly leery of sys.argv[0] because the docs say "argv[0] is the script name (it is operating system dependent whether this is a full pathname or not)."
I'd give it try. It seems to work for others. However, I just tested it on linux, and if the app you start is actually a soft link to the real script, you get the relative path to the link, not the full path to anything.
I know there are also reasons to be leery of
os.getcwd(). For the moment, I'm settling on this code:
if sys.platform == 'darwin': # Mac oddity
basedir = os.getcwd()
if string.find(basedir, '/Contents/Resources') != -1:
os.chdir('../../..')
basedir = os.getcwd()
dfilename = basedir + '/PROSE.DIC'
gfilename = basedir + '/PROSE.GRA'
It works on the Mac in either Terminal mode or py2app mode;
what if you start the app from the command line, in a different dir than the app?
For completeness, on Linux, you get the dir you started the app from, if started from a command line. If started from a GUI, you get whatever that GUI does, usually $HOME
-Chris
···
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov
Charles Hartman <charles.hartman@conncoll.edu> writes:
I'm slightly leery of sys.argv[0] because the docs say "argv[0] is the
script name (it is operating system dependent whether this is a full
pathname or not)." I know there are also reasons to be leery of
os.getcwd(). For the moment, I'm settling on this code:
I'm going with Chris. I'd trust more on getting the path name with
sys.argv[0] than on having the program ran from the correct directory
100% of the time.
if sys.platform == 'darwin': # Mac oddity
basedir = os.getcwd()
if string.find(basedir, '/Contents/Resources')
!= -1:
I like more the idiom: "if '/Contents/Resources' in basedir:" than using
string.find(). You should also use
"basedir.find('/Contents/Resources')" and avoid having to import the old
string handling module. Both read much better to me.
os.chdir('../../..')
basedir = os.getcwd()
dfilename = basedir + '/PROSE.DIC'
gfilename = basedir + '/PROSE.GRA'
It works on the Mac in either Terminal mode or py2app mode; I'll test
it on Windows when I get to a Windows machine again -- I'll see if I'm
right that the straight getcwd() is *likely* to return the directory
from which the Windows executable was launched. (Obviously not
guaranteed.)
I think it won't, but I also don't have a Windows machine around here
(none of the three computers here at the office/home have Windows, all
run Linux and FreeBSD :-))
Don't forget testing with an icon on the Desktop.
Be seeing you,
···
--
Godoy. <godoy@ieee.org>
I'm going with Chris. I'd trust more on getting the path name with
sys.argv[0] than on having the program ran from the correct directory
100% of the time.
If you are just trying to get the directory you are executed from, then
the following works for me (and is used in both BitPim and dotamatic)
and also works when the app is frozen. It is used on Windows, Linux and Mac.
import os
mydir=os.path.abspath(sys.path[0])
if os.path.isfile(mydir): # typical when app is frozen
mydir=os.path.dirname(p)
resourcedir=os.path.join(mydir, "resources")
(I store the resources in the resources subdirectory). I have seen
theoretical arguments that sys.path[0] may not necessarily be right, but it always has been for me.
Roger
"Roger Binns" <rogerb@rogerbinns.com> writes:
mydir=os.path.dirname(p)
I knew those existed! I just couldn't find them yesterday
And thanks for your recipe. I have never tried mine with a frozen
package...
···
--
Godoy. <godoy@ieee.org>