10

I have no idea what could be the problem here:

I have some modules from Biopython which I can import easily when using the interactive prompt or executing python scripts via the command-line.

The problem is, when I try and import the same biopython modules in a web-executable cgi script, I get a "Import Error"

: No module named Bio

Any ideas here?

Dave
  • 2,396
  • 2
  • 22
  • 25
  • 1
    Does `import Bio` work in a Python interpreter run from the command line? – Cristian Ciupitu Sep 24 '10 at 02:44
  • I never had this kind of problem myself. So I can just make guesses. Does your CGI Script call the same python version as you, when you use the python shell? And is your biopython module installed in a way to be used globally, or can it just be that in your shell tests you had a local access to biopython, maybe because you tried it in the biopython folder or something? Hope it is a little bit helpful. – erikbstack Sep 24 '10 at 02:51
  • To Cristian - import Bio does work as executed from the command line or via the interactive interpreter – Dave Sep 24 '10 at 02:59
  • Are you using the mod_python module, or real CGI? – Doug Sep 24 '10 at 03:00
  • To erkib- biopython is installed along with other modules such as numpy in a site-packages folder, which is not the same as my developing directory. – Dave Sep 24 '10 at 03:01
  • To Doug - I am think my answer would be the real CGI . . . I give an "import cgi" at the top of my code, the import error was provided via the cgitb module – Dave Sep 24 '10 at 03:04

3 Answers3

7

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.
Doug
  • 8,780
  • 2
  • 27
  • 37
  • By running sys.version on the interactive prompt, it looks like Apache was running a different version of python than the once accessible from bash. Since I don't have root access, I'm going to try and install biopython locally in my home. Thanks! – Dave Sep 24 '10 at 12:57
  • I have apache2 running same python as commandline and this still does not work. I am using a LAMP VM from GCP. Wondering what I might have to add to `/etc/apache2/sites-enabled/lamp-server.conf` to get it to work – Brian Wiley Dec 19 '22 at 08:15
4

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"
pyfunc
  • 65,343
  • 15
  • 148
  • 136
  • 1
    I've tried adding the path by way of "sys.path.insert", the thing is, the containing folder already shows up when I enter "sys.path" into the interactive prompt. I am using apache. – Dave Sep 24 '10 at 02:57
1

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 ;)