This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Why can't import Biopython module when I run my scripts located in /usr/lib/cgi-bin?

Hello,

I am user of ubuntu and I run many scripts written with python3 which was installed through anaconda. All modules that I need have been installed there previously i.e. biopython. However, I can't import biopython in one of my script when I try to run my script from /usr/lib/cgi-bin

from Bio import SeqIO #it doesn't work

The ERROR is:

A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
ImportError: No module named 'Bio'
      args = ("No module named 'Bio'",)
      msg = "No module named 'Bio'"
      name = 'Bio'
      path = None
      with_traceback = <built-in method with_traceback of ImportError object>

I have tried the advices proposed on Why can't python find some modules when I'm running CGI scripts from the web? or https://www.raspberrypi.org/forums/viewtopic.php?t=50225

In the first case, sudo permissions to www-data create a warning of security because I am using the server from a public institution. The second case is for raspberrypi

Can someone help me with that problem?

biopython

Are you sure the cgi-bin python binary is 'pointing at' the conda python binary? I suspect youre packages are set up for the 'wrong' Python.

I am not sure, could you help me. Do you know how can I check that?

A quick way to check would be make a script inside the cgi-bin, which prints the system executable:

import sys
sys.executable

If that prints a path with anaconda, then it will be another issue.

Webservers are not an area of expertise for me though, so someone else will probably also need to weigh in.

I guess that my problem is: I don't know how to share anaconda packages with the user of HTTP server because the HTTP server executes my scripts as user “nobody”.

I suspect that is more likely the case.

This question is probably better directed at StackOverflow. I won’t go so far as to close it just yet, in case there is some behaviour specific to BioPython, but I doubt thats likely to be so.

This is more a question about web server config/package management than bioinformatics at this point.

Ok, I agree. I sent the same question to StackOverflow but nobody answered me with a real solution.

Did you made the python file executable and if yes are you not using a wrong shebang line?

Some extra info to do some easy checks:

Execute from the command line:

which python3

If it says that you are using the one from conda your are good, if not you now know your problem.(tip: check your $PATH)

Next start python3 from the commandline by simply executing:

python3

If you are inside the python3 program you can type and press enter:

import Bio

If you get an import error biopython is not installed. If you don't get an error biopython works and your script should work if you execute:

python3 yourscript.py

And the last thing but is unlikely, make sure there is no python3 symlink in your /usr/lib/cgi-bin folder.

1 answer

SOLUTION: Removing anaconda from home directory and install it in usr/local. Thus, Apache will have permission to access the environment.

a) Remove anaconda with $ rm -rf anaconda3/ b) Re-install anaconda3 in directory /usr/local c) Add the line export PATH='/usr/local/anaconda3/bin:$PATH' to the end of your /root/.bashrc file d) Verify that the root user is using the right version of Python: $ which python

That's all

Log in to answer this question.