[英]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
.我在带有 Catalina 的 macOS 上,从venv
运行我的环境。 I'm trying to import requests
within a Jupyter notebook Python3, but I'm getting the following error:我正在尝试在 Jupyter notebook Python3 中import requests
,但出现以下错误:
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:但是,已经为 Python3 安装了requests
:
(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.如果我尝试从终端或脚本导入requests
,它适用于 Python3,但它也不适用于本机 macos Python。 But as I said above, I'm using a Python3 notebook但正如我上面所说,我使用的是 Python3 笔记本
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
...我得到3.6.5
UPDATE/FIXED: As pointed out below by @mz below, my Python3 is v3.8 and Jupyter was running v3.6.更新/修复:正如下面@mz 所指出的,我的 Python3 是 v3.8,而 Jupyter 运行的是 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要解决此问题,我必须更改一个“kernel.json”文件,如该线程中所述: Jupyter 使用错误版本的 python
You didn't use the virtual environment venv
when you launch the Jupyter Notebook.启动 Jupyter Notebook 时没有使用虚拟环境venv
。
There are two methods to solve this problem:有两种方法可以解决这个问题:
You can create a Jupyter kernel in your virtual environment.您可以在虚拟环境中创建 Jupyter kernel。 This blog maybe helpful.这个博客可能会有所帮助。
requests
in the exit Jupyter notebook在退出 Jupyter 笔记本中安装requests
Run the following command in the Jupyter Notebook cell.在 Jupyter Notebook 单元格中运行以下命令。
!pip3 install requests
your pip is installing it for python3.8, and also in a virtualenv, whereas your notebook is python3.6.5.你的 pip 正在为 python3.8 安装它,也在 virtualenv 中,而你的笔记本是 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:确保你 pip 安装正确的环境和正确的版本 python 来执行此操作卸载旧版本的 python,然后:
Go to C:\Users\LENOVO\AppData\Local\Programs\Python\
and revome the files of older version Go 到C:\Users\LENOVO\AppData\Local\Programs\Python\
并删除旧版本的文件
Try installing jupyter notebook by pip install jupyter
尝试通过pip install jupyter
Run jupyter notebook by code jupyter notebook
通过代码jupyter notebook
Then again do !pip install requests
然后再次执行!pip install requests
import requests
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.