Why can't python find some modules when I run CGI scripts from the Internet?

I have no idea what could be the problem here:

I have some modules from Biopython that I can easily import when using the interactive prompt or running python scripts through the command line.

The problem is that when I try to import the same biopython modules into the cgi script executable, I get an "Import Error"

: No module named Bio

Any ideas here?

+6
source share
3 answers

Here are a few possibilities:

  • Apache (on Unix) usually works as a different user and with a different environment, in python from the command line. Try making a small script that simply prints sys.version and sys.prefix , and compares the result via apache and the command line to make sure that you are working with the same python installation in both environments.
  • Is Biopython installed in your home directory or is it accessible only to the regular user? Again, since apache usually works as a different user, you may not have access to this location, so it cannot be imported.
  • Can you try to make an import site before trying to import Biopython? Perhaps something is stopping you from importing site packages when you start apache.
+5
source

In a cgi script, you can try adding the path to this package before any import.

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

If you are using Apache, you must install PYTHONPATH in the conf file with the SetEnv directive

 SetEnv PYTHONPATH "path to biopython package" 
+4
source

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

 sudo gedit /etc/apache2/envvars 

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

0
source

All Articles