Why can't python find some modules when I'm running CGI scripts from the web?

Solution 1:

Here are a couple of possibilities:

  • Apache (on Unix) generally runs as a different user, and with a different environment, to python from the command line. Try making a small script that just prints out sys.version and sys.prefix, and compare the result through apache and via the command line, to make sure that you're running from the same installation of python in both environments.
  • Is Biopython installed under your home directory, or only readable just for your normal user? Again, because apache generally runs as a different user, perhaps you don't have access to that location, so can't import it.
  • Can you try doing import site before trying to import Biopython? Perhaps something is preventing site packages from being imported when you run through apache.

Solution 2:

In the cgi script, you could try to add the path to this package before any import.

sys.path.insert(0, 'path to biopython package')

If you are using Apache, you should be able to set the PYTHONPATH in conf file with directive SetEnv

SetEnv PYTHONPATH "path to biopython package"

Solution 3:

I had same problem. I solved this problem by changing user of Apache in Linux Ubuntu by command in terminal:

sudo gedit /etc/apache2/envvars

Please change www-data on export APACHE_RUN_USER and export APACHE_RUN_GROUP to your current user or user that can run python script. Have a good time ;)