0

There are many similar posts I have found. However, those post did not provide solution of my problem. So that I thought to ask the question regarding my problem. My problem is in unit test and import module. My project has following hierarchy.

app/
   __init__.py
   src/
       __init__.py
       person.py
   tests/
       __init__.py
       test_person.py 

I use pytest for unit test. Inside tests/test_person.py

## tests/test_person.py
from ..src import person

Inside src/person.py

## src/person.py
from Bio import PDB

From app/, I run py.test and got the error.

from Bio import PDB
E   ImportError: No module named Bio

I further tested in command line to check whether from Bio import PDB can be imported or not. It can be imported without any error.

Python 2.7.4 (default, May 14 2013, 09:41:12) 
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from Bio import PDB
>>> 

I don't know what is the error. Someone has any guess?

Exchhattu
  • 197
  • 3
  • 15

2 Answers2

0

When running python, make sure your current working directory is the app directory, otherwise from Bio import PDB might have worked for other reasons. You can use os.chdir to move to the app directory, and then try importing and see if it still works. Also - where is Bio located?

EDIT: According to your comment the issue is caused by using different versions of Python.
In order to find out what version you're using, you can simply print sys.version. Make sure to do this BEFORE the failed import line:

import sys
print sys.version
from Bio import PDB

Anyway, the issue of changing the Python version that is used, has been answered here: Two versions of python on linux. how to make 2.7 the default.

Community
  • 1
  • 1
Hetzroni
  • 2,109
  • 1
  • 14
  • 29
  • What causes `Bio` to be successfully imported when you use the command line? It's not in the folder listing you mentioned above. – Hetzroni Sep 12 '14 at 12:47
  • Update: in my system because of program dependency, I have put to version of python - python 2.6 and python 2.7. I installed python2.6 in /usr/bin/ and python2.7 in /usr/local/bin. Now, when I used python2.6, No module error appear - from Bio import PDB. Since py.test is also installed at /user/bin, when py.test is used by default I think it uses python from /usr/bin instead of my default path /usr/local/bin. Could you please tell me how can I know what (which python) py.test is using? How can I customise the library path in py.test? – Exchhattu Sep 13 '14 at 04:02
0

I found the answer. The major problem is py.test by default uses python2.6 and it also raises the error while executing "from Bio import PDB". However Python2.7 does not raise any error on executing same command. To make choice in choosing different version python. Standalone pytest script was created with command and then run

py.test --genscript=curpytest
python curpytest

It solves the error of importing Bio.

Exchhattu
  • 197
  • 3
  • 15