views:

844

answers:

4

I'm trying to setup Django on an internal company server. (No external connection to the Internet.)

Looking over the server setup documentation it appears that the "Running Django on a shared-hosting provider with Apache" method seems to be the most-likely to work in this situation.

Here's the server information:

  • Can't install mod_python
  • no root access
  • Server is SunOs 5.6
  • Python 2.5
  • Apache/2.0.46
  • I've installed Django (and flup) using the --prefix option (reading again I probably should've used --home, but at the moment it doesn't seem to matter)

I've added the .htaccess file and mysite.fcgi file to my root web directory as mentioned here. When I run the mysite.fcgi script from the server I get my expected output (the correct site HTML output). But, it won't when trying to access it from a browser.

It seems that it may be a problem with the PYTHONPATH setting since I'm using the prefix option.

I've noticed that if I run mysite.fcgi from the command-line without setting the PYTHONPATH enviornment variable it throws the following error:

prompt$ python2.5 mysite.fcgi 
 ERROR:
 No module named flup   Unable to load
 the flup package.  In order to run
 django   as a FastCGI application, you
 will need to get flup from  
 http://www.saddi.com/software/flup/  
 If you've already   installed flup,
 then make sure you have it in your
 PYTHONPATH.

I've added sys.path.append(prefixpath) and os.environ['PYTHONPATH'] = prefixpath to mysite.fcgi, but if I set the enviornment variable to be empty on the command-line then run mysite.fcgi, I still get the above error.

Here are some command-line results:

>>> os.environ['PYTHONPATH'] = 'Null'
>>>
>>> os.system('echo $PYTHONPATH')
Null
>>> os.environ['PYTHONPATH'] = '/prefix/path'
>>>
>>> os.system('echo $PYTHONPATH')
/prefix/path
>>> exit()
prompt$ echo $PYTHONPATH
Null

It looks like Python is setting the variable OK, but the variable is only applicable inside of the script. Flup appears to be distributed as an .egg file, and my guess is that the egg implementation doesn't take into account variables added by os.environ['key'] = value (?) at least when installing via the --prefix option.

I'm not that familiar with .pth files, but it seems that the easy-install.pth file is the one that points to flup:

import sys; sys.__plen = len(sys.path)
./setuptools-0.6c6-py2.5.egg
./flup-1.0.1-py2.5.egg
import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sy
s.path[p:p]=new; sys.__egginsert = p+len(new)

It looks like it's doing something funky, anyway to edit this or add something to my code so it will find flup?

+2  A: 

Try using a utility called virtualenv. According to the official package page, "virtualenv is a tool to create isolated Python environments."

It'll take care of the PYTHONPATH stuff for you and make it easy to correctly install Django and flup.

Justin Voss
A: 

To modify the PYTHONPATH from a python script you should use:

sys.path.append("prefixpath")

Try this instead of modifying with os.environ().

And I would recommend to run Django with mod_python instead of using FastCGI...

Anders Westrup
I'm also adding sys.path.append("prefixpath"), but I still get the flup PYTHONPATH error if not adding the ENV option on the command line. Unfortunatly, I don't have the permissions needed to install mod_python :P
monkut
+3  A: 

In your settings you have to point go actual egg file, not directory where egg file is located. It should look something like:

sys.path.append('/path/to/flup/egg/flup-1.0.1-py2.5.egg')
Łukasz
why .pth files do not work in fastcgi?
Stefano Borini
+1  A: 

Use site.addsitedir() not os.environ['PYTHONPATH'] or sys.path.append().

site.addsitedir interprets the .pth files. Modifying os.environ or sys.path does not. Not in a FastCGI environment anyway.

#!/user/bin/python2.6

import site

# adds a directory to sys.path and processes its .pth files
site.addsitedir('/path/to/local/prefix/site-packages/')

# avoids permissions error writing to system egg-cache
os.environ['PYTHON_EGG_CACHE'] = '/path/to/local/prefix/egg-cache'
mhanney