PYTHONPATH on Mac 10.5

Philip Semanchuk philip at semanchuk.com
Wed Feb 25 19:48:04 EST 2009


On Feb 25, 2009, at 3:20 PM, Vincent Davis wrote:

> I have looked around for a good howto setup PYTHONPATH on Mac os x
> 10.5 Although I get many results I am not sure which is correct. I  
> am not
> sure if it is different for 10.5 over previous versions. Does anyone  
> know of
> a well documented set of instructions.
>
> Is there a way to specify a module location or working directory?  
> Which is
> best? Or should I just add location to PYTHONPATH?


Hi Vincent,
There are different instructions on how to set PYTHONPATH because it  
solves a problem (how to organize one's Python modules) that has more  
than one solution. It's sort of like asking "How should I organize my  
email?" Different strokes for different folks.

Me, I don't use PYTHONPATH at all. Most of the stuff I want to import  
is already available in site-packages. If not, I can add a .pth file  
to site-packages that tells Python where to find the source. You can  
read about .pth files here:
http://docs.python.org/library/site.html


> In my python scripts I specify which python I want to use like this
> #!/Library/Frameworks/Python.framework/Versions/4.1.30101/bin/python

Yikes! Your scripts won't be too portable then, will they? I'm on OS X  
10.5 and I don't have a directory like that. A common, more portable  
alternative is this:

#!/usr/bin/env python

That relies (obviously) on /usr/bin/env being present which means that  
your scripts won't work on Windows machines, for instance. But it's a  
whole lot more portable than what you've got now. You don't need a  
shebang line at all if you're willing to launch your scripts by typing  
`python foo.py` at the command line. That will merely execute  
whichever python appears first in your path. I used to always use the / 
usr/bin/env shebang line that I described above, but I do so less  
often now. It's one less dependency to deal with.

So, in short, PYTHONPATH doesn't need to be set at all, and you can  
switch the shebang line to this:

#!/usr/bin/env python

Or do away with it entirely.

This isn't a complete answer but has it been at least somewhat helpful?

Cheers
Philip









More information about the Python-list mailing list