[Pythonmac-SIG] python module search path in Apache on OSX

Rhett Sutphin rhett-sutphin@uiowa.edu
Mon, 17 Feb 2003 13:32:55 -0600


Hi,

On Monday, February 17, 2003, at 11:00  AM, 
pythonmac-sig-request@python.org wrote:
> From: Jack Jansen <Jack.Jansen@cwi.nl>
> Date: Mon Feb 17, 2003  3:32:41  AM America/Chicago
> To: "Samuel M.Smith" <smithsm@samuelsmith.org>
> Cc: Jack Jansen <Jack.Jansen@oratrix.com>, pythonmac-sig@python.org
> Subject: Re: [Pythonmac-SIG] python module search path in Apache on OSX
>
> On Monday, Feb 17, 2003, at 03:57 Europe/Amsterdam, Samuel M.Smith 
> wrote:
>
>>>
>>> You can fix it by creating a symlink /usr/local/lib/python2.2 
>>> pointing to /sw/lib/python2.2. Or, even easier, you could simply put 
>>> #!/sw/bin/python into your scripts.
>>
>> Neither of these fixes changed the path at all. =(
>>
>> One other item I forgot to mention is that the PYTHONPATH environment 
>> variable has a couple more directories in that get prepended to 
>> normal python but are also not in the Apachi CGI invocation of >> python.
>>
>> I noticed that the python library ref for cgi states. "In particular, 
>> don't count on ...the Ptyhon module search path (PYTHONPATH) to be 
>> set to anything interesting."
>
> Ah, then I guess I was barking up the wrong tree, and this is an 
> Apache problem.
>
> If you do find out what the problem is (or the solution) pleas let us 
> know,

I had the same problem when trying to use the pyPgSQL module in CGI 
scripts.  I had installed it in my copy of fink python with no trouble, 
but the cgi scripts were using the wrong sys.path and therefore not 
finding it.  The solution is to set PYTHONPATH in your apache 
configuration.  Here's how I did it:

* First, in /etc/httpd/httpd.conf, enable mod_env by uncommenting the 
following two lines:

LoadModule env_module         libexec/httpd/mod_env.so [at around line 
200]
AddModule mod_env.c [around line 250]

(You may note when you do this that the module mod_setenvif is already 
enabled.  I don't know why it is while mod_env is not.  Perhaps someone 
who knows more about apache would have some insight.)

* The second step is more variable.  The meat of it is that, in an 
appropriate place, put this directive:

SetEnv PYTHONPATH 
"/sw/lib/python2.2:/sw/lib/python-2.2/plat-darwin:/sw/lib/
python2.2/lib-dynload:/sw/lib/python2.2/site-packages"

(All on one line.)  What "an appropriate place" means depends on your 
particular situation.  In my case, I only needed python+CGI+PgSQL to 
work for one user account, so I put it in the Directory block in 
/etc/httpd/users/username.conf .  I imagine you could also put it in 
the Directory block for the web root (in httpd.conf) or in a .htaccess 
file in your CGI directory.

* Restart the apache service for these changes to take effect.

That worked for me.  Feel free to ask if anything's unclear.

Rhett

--
Rhett Sutphin
Research Assistant (Software)
Coordinated Laboratory for Computational Genomics
   and the Center for Macular Degeneration
University of Iowa - Iowa City, IA 52242 - USA
4111 MEBRF - email: rhett-sutphin@uiowa.edu