pytest 설치 되어있어도 못찾을 때
pytest가 설치되어있는데도 못찾겠다고 할 때,
env 밖에서 지워주고
env 안에서 삭제 후 재설치한다.
pip3로 깔려있는거 지우고 env안에서 pip로 다시 깔면 됨.
______ ERROR collecting tests/test_satsuki.py ____________________
tests/test_satsuki.py:10: in <module>
import requests
E ModuleNotFoundError: No module named 'requests'
______ ERROR collecting tests/test_satsuki.py ____________________
ImportError while importing test module '/Users/username/dev/satsuki/tests/test_satsuki.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_satsuki.py:10: in <module>
import requests
E ModuleNotFoundError: No module named 'requests'
Instead of Requests, maybe you’re missing Scrapy or Pyglet or NumPy or Pandas. The point is, pytest says the module is not found but you are sure it exists. You can even prove it is there:
$ pip install requests
Requirement already satisfied: requests in /Users/username/.local/share/virtualenvs/satsuki-7hJc15h4/lib/python3.7/site-packages (2.21.0)
Why is pytest cursing me with the ModuleNotFoundError?
This issue is not the module or package you’re trying to use. We’ve shown that it is there. Installing, uninstalling, and reinstalling won’t fix this. Reinstalling your virtual environment won’t fix it. Resynching Pipenv won’t fix it.
The issue is which pytest you are using and your use of a virtual environment.
If you have installed pytest system-wide, in other words, outside of a virtual environment, pytest has a nasty habit of only looking outside your virtual environment for modules! If your project is using a module that is only installed in your virtual environment, and you’re using a system-wide pytest, it won’t find the module, even if you’ve activated the virtual environment.
Let’s put this another way. Here’s how you could recreate the problem:
- Install pytest outside of all virtual environments. This way it is system wide. For example:
$ pip3 install pytest - Now, make sure that the package you need is not installed system wide. For example:
$ pip3 uninstall requests - Activate a virtual environment for your project that needs the package. For example:
$ pipenv shell - Install the package in the virtual environment. For example:
$ pip install requests - You should now be able to see both pytest and the package. If you check pytest and the package, it will give you your first hint that there’s a problem — notice where pytest is located versus where the package is located:
$ pytest --version
This is pytest version 4.1.0, imported from /usr/local/lib/python3.7/site-packages/pytest.py
$ pip install requests
Requirement already satisfied: requests in /Users/username/.local/share/virtualenvs/satsuki-7xyz/lib/python3.7/site-packages (2.21.0) - Run pytest and get your ModuleNotFoundError!
Fixing pytest and the ModuleNotFoundError
Simply uninstall pytest from your system and only install it within a virtualenv when you need it.
Here’s the step-by-step:
- Exit any virtual environment
- Use Pip to uninstall pytest
- Activate your project’s virtual environment
- Install pytest inside the virtual environment
- pytest will now find your virtual-environment-only packages!
An example of fixing it on the command line:
(venv) $ exit
$ pip3 uninstall pytest
Uninstalling pytest-4.1.0:
Would remove:
/usr/local/bin/py.test
/usr/local/bin/pytest
/usr/local/lib/python3.7/site-packages/_pytest/*
/usr/local/lib/python3.7/site-packages/pytest-4.1.0.dist-info/*
/usr/local/lib/python3.7/site-packages/pytest.py
Proceed (y/n)? y
Successfully uninstalled pytest-4.1.0
$ pipenv shell
(venv) bash-5.0$ pip install pytest
Collecting pytest
Downloading https://files.pythonhosted.org/packages/9e/bf/2974be45c498a0ebc2708bfada25d5d1874ab3315a4e76ce7d38e29724fa/pytest-4.1.1-py2.py3-none-any.whl (216kB)
100% |████████████████████████████████| 225kB 7.8MB/s
Successfully installed pytest-4.1.1
$ pytest
==================== test session starts ===================
platform darwin -- Python 3.7.2, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: /Users/username/dev/satsuki, inifile: setup.cfg
collected 8 itemstests/test_satsuki.py ........ [100%]================ 8 passed in 19.75 seconds =================
Yay!