pytest:从pytest运行时不导入模块,但在普通python上运行

问题描述 投票:0回答:1

此代码在交互式控制台上运行时有效:

>>> 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文件 - 我确实在每个级别都有它们。

python pytest
1个回答
0
投票

你不应该在sys.path.append("/path/to/mypkg")做相同的pytest吗?提示:定义PYTHONPATH!

cd /path/to/mypkg
PYTHONPATH=/path/to/mypkg python py -m pytest
© www.soinside.com 2019 - 2024. All rights reserved.