Python在Jupyter Notebook中看不到软件包 [英] Python doesn't see packages with Jupyter Notebook
问题描述
我在虚拟环境中使用Jupyter Notebook. 我安装了一个依赖项,但是无法导入它:
I use Jupyter Notebook with a virtual environment. I have a dependency installed, but can't import it:
单元格1:
!pip3 install sent2vec
cell 1:
!pip3 install sent2vec
Requirement already satisfied: sent2vec in
venv/lib/python3.7/site-packages (0.0.0)
单元格2:
import sent2vec
cell 2:
import sent2vec
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-5-06231d291a17> in <module>
----> 1 import sent2vec
ModuleNotFoundError: No module named 'sent2vec'
这怎么可能发生?该如何解决?
How this can happen? How to fix this?
> pip3 list
Package Version
------------ ---------
certifi 2019.9.11
chardet 3.0.4
Cython 0.29.14
idna 2.8
joblib 0.14.0
langdetect 1.0.7
nltk 3.4.4
numpy 1.17.1
pip 19.3.1
requests 2.22.0
scikit-learn 0.21.3
scipy 1.3.2
sent2vec 0.0.0
setuptools 41.6.0
six 1.13.0
urllib3 1.25.7
wheel 0.33.6
推荐答案
您会注意到,已安装的软件包中未列出jupyter
.这意味着您是在其他虚拟环境中运行它.正如我在回答您的问题的评论中提到的那样,您可以运行which jupyter
来查找正在运行Jupyter Notebook应用程序的位置(假设您使用的是* NIX系统);在这种情况下,它将不会出现在第一个代码块中显示的python3.7
虚拟环境中.
You'll note that jupyter
is not listed in your installed packages. That means you're running it from a different virtual environment. As I mentioned in the comment responding to your question, you can run which jupyter
to find out where your Jupyter Notebook application is being run from (assuming you're on a *NIX system); in this case, it won't be from the python3.7
virtual environment that shows up in your first code block.
要解决此问题,只需运行pip3 install jupyter
,然后重试运行jupyter notebook
.
To resolve the issue, you simply need to run pip3 install jupyter
, then retry running jupyter notebook
.
或者,您可以将虚拟环境添加为内核,以便在从原始环境运行Jupyter时可以选择它.为此,您将运行(假设pip
已连接到您的原始环境):
Alternatively, you can add your virtual environment as a kernel so that it can be selected when you're running Jupyter from your original environment. To do this, you would run (assuming pip
is connected to your original environment):
pip install ipykernel
ipython kernel install --user --name=<insert name of your venv>
然后,您应该能够在新笔记本上选择该venv作为内核. (有关Jupyter中venv激活的信息,来源).
You should then be able to select that venv as a kernel on new notebooks. (Source for info on venv activation in Jupyter).
这篇关于Python在Jupyter Notebook中看不到软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!