2017-12-27 258 views
0

当交互式控制台上运行此代码:pytest:从pytest运行,但适用于普通的Python,当模块没有导入

>>> import sys 
>>> sys.path.append("/path/to/mypkg") 
>>> import mypkg.src.dbwrapper as dbw 

但不喜欢这样:

$ cd /path/to/mypkg 
$ python py -m pytest 
mypkg/src/dbwrapper_queries.py:3: in <module> 
    import mypkg.src.dbwrapper as ec 
E ImportError: No module named mypkg.src 

我上运行Python 2.7.14

我可能会丢失什么?当通过pytest调用时导入失败,但在运行python时运行,然后在控制台上导入。

埃德:我不会错过任何__init__.py文件 - 我确实在每个级别都有。

+0

无处不在。让我更新我的问题。 – ghostrider

回答

0

难道你不应该在pytest做一个相当于sys.path.append("/path/to/mypkg")?提示:定义PYTHONPATH!

cd /path/to/mypkg 
PYTHONPATH=/path/to/mypkg python py -m pytest