[Pythonmac-SIG] PYTHONPATH in Mac OS X App

Sarwat Khan sarwat at sarwat.net
Wed Sep 17 15:48:55 EDT 2003


On Wednesday, September 17, 2003, at 02:41  PM, Drew McCormack wrote:

> My question is, what is the best way to load pplot.py from a directory 
> in my app bundle? I would rather not hard code a path, because I want 
> to be able to install my app anywhere. So I would prefer a solution 
> that allows me to set the path to pplot.py at run time.

If you want an example of embedding Python in a Cocoa app, take a look 
at the BitTorrent port. The Macintosh source is included (I think) in 
the source available through http://bitconjurer.org/BitTorrent

The approach they took was including the entire Python library 
distribution files that they required (so (a) they don't depend on the 
user having Python installed and (b) they trim the 'fat' of Python they 
don't use) and put the files in the application wrapper's Resources 
directory. From their main.m,

   Py_SetPythonHome((char*)[[[NSBundle mainBundle] resourcePath] 
cString]);
   Py_Initialize();
   PySys_SetArgv(argc, (char **)argv);
   PyEval_InitThreads();

   // add our resource path to sys.path so we can find the BT modules
   mm = PyImport_ImportModule("sys");
   md = PyModule_GetDict(mm);
   path = PyDict_GetItemString(md, "path");
   PyList_Append(path, PyString_FromString([[[NSBundle mainBundle] 
resourcePath] cString]));

I wrote my own port of BitTorrent that takes a completely different 
approach. It uses the user's installation of Python, but the Cocoa 
application itself doesn't embed (use the Python C API) at all. It does 
keep .py[o] files in the application's Resources directory and launches 
/usr/bin/python to interpret them.

   http://sarwat.net/opensource

What my version does is uses the 'exec' and 'fork' system calls to 
launch another python process, and they  communicate with each other 
over standard IO. This way the Cocoa and Python (BitTorrent) programs 
can be updated independently of each other, and be coded to the fullest 
extent of their respective programming environments. This was useful 
for BitTorrent since BitTorrent is cross platform and the Mac version 
needs constant updating to keep up.

When my version needs to launch Python, it sets the current working 
directory to where the 'main' BitTorrent python script is located, and 
then forks and execs it. However, if you're going to use the user's 
installation of Python, you will want to override their site.py and 
provide your own in order to make sure that you don't get unwanted 
side-effects when running your program. My version of BitTorrent 
includes a site.py that disables loading of site-packages, sets the 
default string encoding to utf-8, and also doesn't load 
sitecustomize.py. In order for my site.py to override the site.py in 
the user's Python installation, I had to set PYTHONPATH to "." (which 
is also the current working directory when I launch Python, which is 
also where my site.py is located). See my source code for details; I 
exec python in btagent_posix.c, and BitTorrentAgent.m uses 
btagent_posix.c.

I hope this information wasn't obtuse,

Sarwat.

{sarwat khan : http://sarwat.net}




More information about the Pythonmac-SIG mailing list