Python 将in _uinit__uuu.py导入到unittest包
我有一个包和一个测试包。根据来自的建议,测试应该位于不同的目录中。项目的目录树如下所示:Python 将in _uinit__uuu.py导入到unittest包,python,unit-testing,testing,Python,Unit Testing,Testing,我有一个包和一个测试包。根据来自的建议,测试应该位于不同的目录中。项目的目录树如下所示: project\ kernel\ __init__.py file1.py file2.py tests\ __init__.py test1.py test2.py test3.py 我想将内核包导入测试包,因为这是测试file1.py和file2.py的地方。另外,我想在\u
project\
kernel\
__init__.py
file1.py
file2.py
tests\
__init__.py
test1.py
test2.py
test3.py
我想将内核
包导入测试
包,因为这是测试file1.py
和file2.py
的地方。另外,我想在\uuuu init\uuuuuu.py
中使用一个import
语句,而不是在每个测试中一次又一次地导入内核。
我尝试将以下内容添加到tests
中的\uuuu init\uuuuuuuuuuuuuuuuuuuuupy
文件和test2.py
,test2.py
(一起和分别添加),但没有成功(第一个没有造成任何伤害,第二个给出语法错误):
我用的是python2.6。从命令行执行上述所有操作。当我使用EclipsePydev时,一切都会神奇地工作。只有在“project”目录是python包的情况下(即,它有一个\uu init\uuuuuuuuuy.py
文件),您使用的相对导入才会工作。先试试,看看这对你是否有效
如果内核
目录充当要分发的“包”,那么您可以将测试
目录放在其中,并以这种方式进行相对导入。所以看起来是这样的:
project/
kernel/
__init__.py
file1.py
file2.py
tests/
__init__.py
test1.py ...
您可以从tests目录导入内核模块,如下所示:
from kernel import file1 # if it's installed in the python path/environment
或:
from kernel import file1 # if it's installed in the python path/environment
from .. import file1
# 'import ..file1' might work, but I'm not sure that's syntactically correct