3

I have installed python-biopython and successfully used it in my python scripts with something like:

import Bio

But it eventually stopped working and now even after uninstallation and reinstallation I cannot successfully import with python 2.7x. I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named Bio

Requested Outputs:

Output of apt-cache policy python-biopython:

python-biopython:
  Installed: 1.63-1
  Candidate: 1.63-1
  Version table:
 *** 1.63-1 0
        500 http://us.archive.ubuntu.com/ubuntu/ trusty/universe amd64 Packages
        100 /var/lib/dpkg/status

Output of find $(python -c "import sys; print '\n'.join(sys.path)") -type d -name 'Bio':

find: `/home/alex/anaconda/lib/python27.zip': No such file or directory
find: `/home/alex/anaconda/lib/python2.7/lib-old': No such file or directory

Output of python -c "import sys; print '\n'.join(sys.path)" Does not appear that biopython is listed:

/home/alex/anaconda/lib/python27.zip
/home/alex/anaconda/lib/python2.7
/home/alex/anaconda/lib/python2.7/plat-linux2
/home/alex/anaconda/lib/python2.7/lib-tk
/home/alex/anaconda/lib/python2.7/lib-old
/home/alex/anaconda/lib/python2.7/lib-dynload
/home/alex/anaconda/lib/python2.7/site-packages
/home/alex/anaconda/lib/python2.7/site-packages/Sphinx-1.3.1-py2.7.egg
/home/alex/anaconda/lib/python2.7/site-packages/cryptography-0.9.1-py2.7-linux-x86_64.egg
/home/alex/anaconda/lib/python2.7/site-packages/setuptools-18.1-py2.7.egg

Output of apt-cache policy python:

python:
  Installed: 2.7.5-5ubuntu3
  Candidate: 2.7.5-5ubuntu3
  Version table:
 *** 2.7.5-5ubuntu3 0
        500 http://us.archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages
        100 /var/lib/dpkg/status
3
  • 1
    Edit your question and add the output of apt-cache policy python-biopython – A.B. Sep 23 '15 at 17:49
  • 1
    And the output of find $(python -c "import sys; print '\n'.join(sys.path)") -type d -name 'Bio' – A.B. Sep 23 '15 at 17:52
  • Please the output of apt-cache policy python – A.B. Sep 23 '15 at 19:09
1

I guess it's a problem with the Python PATH. Probably your installation does not look for packages in the directory where Biopython installs its files.

Step 1 - Checking the Python PATH and Biopython installation directory:

Please check your Python 2's PATH setting with the following command:

python -c "import sys; print('\n'.join(sys.path))"

According to the package content of python-biopython version 1.64, it will install the Bio package directory in /usr/lib/python2.7/dist-packages/, so if this directory is missing in your Python PATH, we have spotted the error cause.

Step 2 - Trying a temporary fix:

You can add a directory to the Python PATH variable by setting the shell's environment variable PYTHONPATH before starting the interpreter:

PYTHONPATH="/usr/lib/python2.7/dist-packages" python

In this Python session you should now be able to use the Biopython package. After you verified that it works, we can continue with...

Step 3 - Making the fix permanent:

Python has one directory where it looks for path configuration files (*.pth). We find out which directory this is in your installation with the command:

PythonSiteDir=$(python -c "import site; site._script()" --user-site)
echo $PythonSiteDir

This first stores the path in the variable $PythonSiteDir and then outputs it to the terminal. In your case, it will probably show the directory /home/alex/anaconda/lib/python2.7/site-packages, but if not, please use your respective directory instead.

We have to place our custom .pth file into this directory, so first we make sure that we don't accidentally overwrite an existing file by checking which path configuration files already exist there. To avoid much typing, we use the variable we created earlier:

ls ${PythonSiteDir}/*.pth

All file names displayed by this command do already exist and may not be used. Assuming that biopython_directory.pth was not in the list, we will now create this file and let it contain the path where Biopython is installed:

echo "/usr/lib/python2.7/dist-packages" > ${PythonSiteDir}/biopython_directory.pth

That was all. The only thing left by now is to test whether it worked. You can either just start using Biopython again or you can first check the current Python Path with the command from above:

python -c "import sys; print('\n'.join(sys.path))"

Source/inspiration for step 3: https://stackoverflow.com/a/12311321/4464570

2
  • Using your PYTHONPATH does work and I can import Bio properly now. But is there a way to make this permanent so I don't need to do this everytime I need to use biopython? – The Nightman Sep 23 '15 at 19:03
  • This is probably described best in this answer from StackOverflow. I will edit my answer to include the steps. – Byte Commander Sep 24 '15 at 9:06
0

I'd like to answer as a comment, but I need some reputation to write comments. So, I write here.

I use ready-made virtualbox qiime system. As I know, in that system, QIIME uses different path set up method. In the ".bashrc" file it uses activate.sh like

source /home/qiime/qiime_software/activate.sh

In the activate.sh file, you can find the lines

export PYTHONPATH=/home/qiime/.../:/home/qiime/.../:
/usr/local/lib/python2.7/dist-packages/`

You should edit the file by adding ":/usr/local/lib/python2.7/dist-packages/" after the paths and execute the file again using following command

source /home/iime/qiime_software/activate.sh

I got a solution here from your question and answer. thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.