簡體   English   中英

使用 Python3 的 Jupyter 中沒有名為“請求”的模塊,但 Python3 的“要求已經滿足”

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

我在帶有 Catalina 的 macOS 上,從venv運行我的環境。 我正在嘗試在 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'

但是,已經為 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)  

如果我嘗試從終端或腳本導入requests ,它適用於 Python3,但它也不適用於本機 macos Python。 但正如我上面所說,我使用的是 Python3 筆記本

為了以防萬一,如果我在筆記本中運行下面的代碼......

from platform import python_version
print(python_version())

...我得到3.6.5

更新/修復:正如下面@mz 所指出的,我的 Python3 是 v3.8,而 Jupyter 運行的是 v3.6。 要解決此問題,我必須更改一個“kernel.json”文件,如該線程中所述: Jupyter 使用錯誤版本的 python

啟動 Jupyter Notebook 時沒有使用虛擬環境venv

有兩種方法可以解決這個問題:

  1. 創建一個 Jupyter kernel

您可以在虛擬環境中創建 Jupyter kernel。 這個博客可能會有所幫助。

  1. 在退出 Jupyter 筆記本中安裝requests

在 Jupyter Notebook 單元格中運行以下命令。

!pip3 install requests

你的 pip 正在為 python3.8 安裝它,也在 virtualenv 中,而你的筆記本是 python3.6.5。 確保你 pip 安裝正確的環境和正確的版本 python 來執行此操作卸載舊版本的 python,然后:

  • Go 到C:\Users\LENOVO\AppData\Local\Programs\Python\並刪除舊版本的文件

  • 嘗試通過pip install jupyter

  • 通過代碼jupyter notebook

  • 然后再次執行!pip install requests

  • import requests

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM