[Pythonmac-SIG] Re: Newbie path problem

Bob Ippolito bob at redivi.com
Thu Feb 26 12:03:29 EST 2004


On Feb 26, 2004, at 10:12 AM, Christer Fernstrom wrote:

> On 26 févr. 04, at 14:38, Bob Ippolito wrote:
>
>> On Feb 26, 2004, at 8:16 AM, Christer Fernstrom wrote:
>>
>>>> On 25-feb-04, at 23:39, Christer Fernstrom wrote:
>>>>
>>>> > I have just ported some of my own python code from os x 10.1 to 
>>>> Python
>>>> > 2.3 on OSX 10.3 and come upon a problem with the path which would
>>>> > surprise me if not several others have. One module imports another
>>>> > module residing in the same directory, which is not on the 
>>>> PYTHONPATH.
>>>> > This works fine when running from the terminal, and it used to 
>>>> work
>>>> > fine in my earlier installation also after using BuildApplet to 
>>>> create
>>>> > an application applet. This does not work in Python 2.3. Instead 
>>>> it
>>>> > generates an import error.
>>>> >
>>>> > Tracing sys.path, I notice that its first two elements are equal:
>>>> >       <scripts-directory>/<scripts-name>.app/Contents/Resources
>>>>
>>>> You have to explain this a bit more. You say "One module imports
>>>> another module residing in the same directory, which is not on the
>>>> PYTHONPATH". How do you import the first module then, as it is in a
>>>> directory that also isn't on sys.path?
>>>>
>>>> --
>>>> Jack Jansen, <Jack.Jansen at cwi.nl>, http://www.cwi.nl/~jack
>>>> If I can't dance I don't want to be part of your revolution -- Emma
>>>> Goldman
>>>
>>> Sorry, I'll try to make it clearer. The first module, Module-A, is 
>>> the main program module. It imports Module-B, which resides in the 
>>> same directory as Module-A. This directory, Directory-D, is not on 
>>> the PYTHONPATH. Using terminal, and positioned in any directory, I 
>>> can successfully invoke Module-A:
>>>  % python Directory-D/Module-A.py <paramters>
>>> Printing out the value of sys.path tells me that its first element 
>>> is "Directory-D", which explains why the import of Module-B works 
>>> fine.
>>>
>>> The problem occurs when I transform Module-A to an applet, using 
>>> BuildApplet. The import now fails, and looking at the Console window 
>>> I can see that sys.path now prints with a different value, and in 
>>> particular its first two elements are identical. They contain 
>>> "Directory-D/Module-A.app/Contents/Resources". It does not contain 
>>> Directory-D, which explains why the import fails.
>>>
>>> I could of course work around this in my code, but it doesn't seem 
>>> very clean to do it that way.
>>
>> Have you tried copying Module-B into Module-A.app/Contents/Resources 
>> ? Everything you need either needs to be findable from sys.path, or 
>> you will have yourself a location-dependent app (i.e. if you moved 
>> Module-A.app to any other location then it would no longer work).
>>
> I assume it would work, but then this would depend on the fact that I 
> run Module-A as an applet and not a script. In my humble opinion, it 
> is the AppletBuilder's responsibility to initialise sys.path to 
> contain Module-A's source directory.

I think you're confused as to what an applet is.  An applet is *not* an 
alias to a python script, but a self-contained application that must 
*embed* anything it needs.  I'm sorry if this disappoints you, but what 
you want is simply not what applets are on OS X.  Perhaps the sys.path 
rules were a little different (including the location of the applet 
itself) with OS9/Carbon based applets, but you will have to emulate 
that on your own.  It's possible to write an application similar to 
BuildApplet that uses an alias to a script instead (the behavior you 
seem to want), but it would no longer be BuildApplet :)

-bob
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2357 bytes
Desc: not available
Url : http://mail.python.org/pipermail/pythonmac-sig/attachments/20040226/aace6bc4/smime.bin


More information about the Pythonmac-SIG mailing list