简体   繁体   中英

No module named 'requests' in Jupyter with Python3, but "Requirement already satisfied" for Python3

I'm on a macOS with Catalina, running my environment from venv . I'm trying to import requests within a Jupyter notebook Python3, but I'm getting the following error:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-2-95039fbd75c1> in <module>()
----> 1 import requests

ModuleNotFoundError: No module named 'requests'

However, requests is already installed for Python3:

(venv) 42piratas@Darkseid PLAYGROUND % pip3 install requests
Requirement already satisfied: requests in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (2.24.0)
Requirement already satisfied: idna<3,>=2.5 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from requests) (2.10)
Requirement already satisfied: chardet<4,>=3.0.2 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from requests) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from requests) (1.25.10)
Requirement already satisfied: certifi>=2017.4.17 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from requests) (2020.6.20)  

If I try to import requests from the terminal or from a script, it works for Python3, but it won't work for the native macos Python either. But as I said above, I'm using a Python3 notebook

And just in case, if I run the code below within the notebook...

from platform import python_version
print(python_version())

...I get 3.6.5

UPDATE/FIXED: As pointed out below by @mz below, my Python3 is v3.8 and Jupyter was running v3.6. To fix this, I had to change one "kernel.json" file, as explained in this thread: Jupyter using the wrong version of python

You didn't use the virtual environment venv when you launch the Jupyter Notebook.

There are two methods to solve this problem:

  1. Create a Jupyter kernel

You can create a Jupyter kernel in your virtual environment. This blog maybe helpful.

  1. Install requests in the exit Jupyter notebook

Run the following command in the Jupyter Notebook cell.

!pip3 install requests

your pip is installing it for python3.8, and also in a virtualenv, whereas your notebook is python3.6.5. Make sure you pip install for the right environment and the right version of python to do this uninstall the older version of python, then:

  • Go to C:\Users\LENOVO\AppData\Local\Programs\Python\ and revome the files of older version

  • Try installing jupyter notebook by pip install jupyter

  • Run jupyter notebook by code jupyter notebook

  • Then again do !pip install requests

  • import requests

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