【问题标题】:ImportError: No module named requests in 'pytest'ImportError:“pytest”中没有名为请求的模块
【发布时间】:2018-10-09 17:05:46
【问题描述】:
root
| +-- demo
      |-->__init__.py
      |-->conftest.py
      |-->test.py

conftest.py

import pytest
def tear_down():
    print "\nTEARDOWN after all tests"

@pytest.fixture(autouse=True)
def set_up(request):
    print "\nSETUP before all tests"
    if request.cls.__name__ == 'TestClassA':
        return ["username", "password"]
    request.addfinalizer(tear_down)

test.py

#import requests # this is commented

class TestClassA:
    def test_1(self,set_up):
        print "test A1 called"
        print("username :-- %s and password is %s" % (set_up[0], set_up[1]))
    def test_2(self):
        print "test A2 called"

class TestClassB:
    def test_1(self):
        print "test B1 called"

pytest -s -v demo/test.py::TestClassA

此代码运行良好。观察test.py 的第一行,它被评论了。现在,如果我在取消注释 import requests 的情况下运行相同的脚本,则会出现以下错误

ImportError while importing test module 'some_path/../demo/test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/lib/python2.7/site-packages/six-1.11.0-py2.7.egg/six.py:709: in exec_
    exec("""exec _code_ in _globs_, _locs_""")
demo/test.py:1: in <module>
    import requests
E   ImportError: No module named requests

在没有 pytest 的情况下执行,工作正常(没有导入错误) 而且,如果test.py 调用其他模块(which has import requests)的函数也会抛出同样的错误。是request of pytest 的冲突吗?这个我真的不明白,你能帮帮我吗?

which python:/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonpytest --version:'imported from /usr/local/lib/python2.7/site-packages/pytest-3.8.2-py2.7.egg/pytest.pyc',这是失败的原因吗?

pytest --version

This is pytest version 3.8.2, imported from /usr/local/lib/python2.7/site-packages/pytest-3.8.2-py2.7.egg/pytest.pyc
setuptools registered plugins:
  celery-4.0.2 at /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celery/contrib/pytest.py
  hypothesis-3.8.2 at /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/hypothesis/extra/pytestplugin.pyc
  pytest-cov-2.4.0 at /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pytest_cov/plugin.py

【问题讨论】:

  • requests 没有为您使用的解释器/venv 安装。
  • @KlausD。这对这里有什么影响?因为,即使从 python 交互,我也可以成功导入请求。
  • 不同的Python解释器(版本)还是venv?
  • 不,它是同一个 /Library/Frameworks/Python.framework/Versions/2.7/bin/python
  • 那么当你运行/Library/Frameworks/Python.framework/Versions/2.7/bin/python -c "import requests"时,你会得到任何输出吗?

标签: python macos pytest


【解决方案1】:

将以下代码行添加到您的脚本并运行它:

from distutils.sysconfig import get_python_lib
print(get_python_lib())

现在检查输出,你会得到一些打印出来的路径,它指向 python 解释器当前使用的包的确切位置

e.g. "/usr/lib/python2.7/dist-packages"

cd(change directory) 到上述路径,ls(list directory) 检查包是否存在; 如果不是

sudo pip3 install requests -t . # dot indicates current directory 

否则,如果您有 requirements.txt 文件,那么您可以尝试:

sudo pip3 install -r requirements.txt -t "/usr/lib/python2.7/dist-packages" 
#try this from the directory where  "requirements.txt" file exists

现在运行你的脚本,如果它有效,请告诉我

【讨论】:

  • 之前我已经验证过,从 /usr/local/bin/pip install requests 安装 requests 模块。这行得通。但我猜不是根本原因。因为,当我从 pytest 再次运行脚本时,它会为不同的包引发不同的错误。而没有 pytest 的相同脚本工作得很好。
  • 正如我之前所说,which python : /Library/Frameworks/Python.framework/Versions/2.7/bin/python pytest --version : '从/usr/local/lib/python2.7导入/site-packages/pytest-3.8.2-py2.7.egg/pytest.pyc' ,这是失败的原因吗?
  • /usr/local/bin/pip install requests ---> 不要运行这个。将“from distutils.sysconfig import get_python_lib print(get_python_lib())”行添加到您的脚本并运行它,因为您想知道 python 解释器正在使用的指定包的路径。可能是您的 pip 指向站点包,但在运行代码时,解释器指向可能不包含“请求”的 dist 包
  • 当我将 get_python_lib 添加到代码中并使用 pytest 运行时,它会输出 '/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib /python2.7/site-packages'。相同的代码,当我正常运行python test.py(即没有pytest)时,它输出'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages'
  • 这是我提到的问题,现在按照我在答案中写的步骤,将请求安装到你提到的文件夹中,你很高兴
猜你喜欢
  • 2013-06-22
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-19
相关资源
最近更新 更多