Search code examples
pythonpippipenv

Python: "No module named 'requests' " after pip3 install requests in pipenv


I'm trying to install the library requests in pipenv, with Python 3. I've tried:

  1. pip install requests, before forgetting that pip installs only for Python 2, leading to,
  2. pip3 install requests, which returns assuringly

Requirement already satisfied: requests in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (2.19.1)
Requirement already satisfied: idna<2.8,>=2.5 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests) (2.7)
Requirement already satisfied: urllib3<1.24,>=1.21.1 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests) (1.23)
Requirement already satisfied: certifi>=2017.4.17 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests) (2018.8.24)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests) (3.0.4)

When I run my python files with import requests, everything works, but when I run pipenv run python mypythonscript.py, I get the error ModuleNotFoundError: No module named 'requests'. Please advise what I'm missing.


Solution

  • The problem here is that pip3 install requests runs outside of pipenv's virtualenv, it is installed outside of virtualenv, and thus requests library is not found from code running inside the virtualenv.

    The correct way to install the library into the virtualenv is pipenv install requests, which will also add requests to your Pipfile. If you want to install it temporarily without saving into the Pipfile, you can run pipenv run pip install requests.