py.test:ImportError:没有名为mysql的模块

mem*_*ecs 8 python mysql virtualenv pytest

我在导入mysql的包上运行py.test时遇到问题.包mysql是用virtualenv中的pip安装的.

# test_mysql.py

import mysql

def foo():
  pass
Run Code Online (Sandbox Code Playgroud)

我可以毫无问题地运行python test_mysql.py,但是当我执行时,py.test test_mysql.py我得到:

>   import mysql
E   ImportError: No module named mysql
Run Code Online (Sandbox Code Playgroud)

知道问题是什么?

fal*_*tru 15

看起来你正在使用系统范围的版本py.test,使用非virtualenv版本的python.(其中mysql未安装包).

py.test在virtualenv中安装,然后你的问题就会解决.


边注

要确保使用新安装的py.test,请清除shell使用的命令路径缓存.

例如,如果使用bash,请发出以下命令:

hash -r
Run Code Online (Sandbox Code Playgroud)