[Pythonmac-SIG] Re: cwd and PythonLauncher problem -- a bit more info

Bob Ippolito bob at redivi.com
Thu Sep 30 21:35:26 CEST 2004


On Sep 30, 2004, at 2:54 PM, Jim Harrison wrote:

>> Sorry if this has been previously discussed. It appears that when a 
>> source
>> file is double-clicked and starts with PythonLauncher (2.3), the 
>> directory
>> of the file is not added to the Python module search paths. Thus other
>> modules in that directory cannot be imported without a full path and 
>> files
>> cannot be read or written using paths relative to the source file.
>
> Sorry, I was inaccurate--importing is not affected. This problem 
> appears to
> be specific to opening files. If a script is launched with 
> PythonLauncher
> and it attempts to open a file in its own directory using only the 
> name of
> the file (no path), it will fail with 'No such file or directory.' If 
> the
> program writes a file using a file name only, it appears to write to 
> one of
> two places: if it is running within a user directory tree it will 
> write the
> file to the root of the user directory; if it is running outside a user
> directory (eg., the Applications directory) it will write to the root 
> of the
> disk.
>
> My understanding is that relative file paths should be based on the
> directory containing the executing script. Programs executed on the 
> Mac from
> within IDLE or MacPythonIDE appear to handle the script's directory
> correctly under these conditions. Is this a known or fixable issue with
> PythonLauncher, or should I be looking for a problem in my 
> installation?
>
> Environment: OSX 10.3.5, Jack's MacPython 2.3 Panther addons

Relative file paths are relative to the current directory.  GUI apps in 
OS X always have a current directory of / (not sure why you ever saw it 
have a path of the user directory, maybe PythonLauncher does that on 
purpose?).  It would make sense for PythonLauncher to behave otherwise 
though.

In the meantime, you could do:

import os
os.chdir(os.path.dirname(__file__))

which will change to the directory that your script is in.

-bob


More information about the Pythonmac-SIG mailing list