简体   繁体   中英

ImportError: No module named requests in 'pytest'

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

This code works fine. observe the first line of test.py , it's commented. Now, if I run the same script with uncommenting import requests , getting below error

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

Executing without pytest, works fine (no import error) And also, if test.py calls the function of other module ( which has import requests ) throws same error. Is it conflict of request of pytest ? I really don't understand this, can you please help me ?

which python : /Library/Frameworks/Python.framework/Versions/2.7/bin/python pytest --version : 'imported from /usr/local/lib/python2.7/site-packages/pytest-3.8.2-py2.7.egg/pytest.pyc' , is this a reason for failure ?

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

Add the following lines of code to your script and run it :

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

Now check the output, you will get some path printed which points to the exact location of the packages the python interpreter is currently using

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

cd(change directory) to the above path and ls(list directory) to check if package exists ; if not :

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

or else if you have a requirements.txt file then you could try:

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

Now run your scripts and please let me know if it worked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM