简体   繁体   English

Python Unittest和导入错误

[英]Python Unittest and import error

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. 我使用pytest进行单元测试。 Inside tests/test_person.py 内部测试/ test_person.py

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

Inside src/person.py 内部src / person.py

## src/person.py
from Bio import PDB

From app/, I run py.test and got the error. 在app /中,我运行py.test并收到错误消息。

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. 我在命令行中进行了进一步测试,以检查是否可以从Bio import PDB中导入。 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? 有人有猜测吗?

When running python, make sure your current working directory is the app directory, otherwise from Bio import PDB might have worked for other reasons. 在运行python时,请确保您当前的工作目录是app目录,否则from Bio import PDB可能由于其他原因起作用了。 You can use os.chdir to move to the app directory, and then try importing and see if it still works. 您可以使用os.chdir移至app目录,然后尝试导入并查看它是否仍然有效。 Also - where is Bio located? 此外-其中Bio在什么位置?

EDIT: According to your comment the issue is caused by using different versions of Python. 编辑:根据您的评论,问题是由使用不同版本的Python引起的。
In order to find out what version you're using, you can simply print sys.version . 为了找出您使用的版本,您可以简单地print sys.version Make sure to do this BEFORE the failed import line: 确保在失败的import行之前执行此操作:

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. 无论如何,已在此处回答了更改所使用的Python版本的问题: Linux上的两个python版本。 how to make 2.7 the default . 如何使2.7默认

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". 主要问题是py.test默认情况下使用python2.6,并且在执行“从生物导入PDB”时也会引发错误。 However Python2.7 does not raise any error on executing same command. 但是,Python2.7在执行相同命令时不会引发任何错误。 To make choice in choosing different version python. 在选择不同版本的python中做出选择。 Standalone pytest script was created with command and then run 使用命令创建独立的pytest脚本,然后运行

py.test --genscript=curpytest
python curpytest

It solves the error of importing Bio. 解决了导入Bio的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM