简体   繁体   English

Jupyter notebook 找不到模块

[英]Jupyter notebook cannot find module

I'm trying to load a module called folium into my Jupyter Notebook environment.我正在尝试将名为folium的模块加载到我的 Jupyter Notebook 环境中。 When I attempt to load this module, a ModuleNotFoundError is returned.当我尝试加载此模块时,会返回ModuleNotFoundError

When I run !pip list in the same Jupyter Notebook environment, folium is listed amongst my installed modules.当我在同一个 Jupyter Notebook 环境中运行!pip list时, folium列在我安装的模块中。

How do I successfully load this module?如何成功加载此模块?

Could it be that you expect a certain Python installation (virtual environment) to be used in Jupyter, but Jupyter is using a different kernel?难道你期望在Jupyter中使用某个Python安装(虚拟环境),但Jupyter使用的是不同的kernel?

With that assumption, I believe all you need to do is to add the virtual environment to Jupyter as a kernel. In more detail:有了这个假设,我相信您需要做的就是将虚拟环境作为 kernel 添加到 Jupyter。更详细地说:

  • Install & activate your virtual environment.安装并激活您的虚拟环境。 Below I'll use virtualenv , which nowadays seems to be the recommended way of working ( see Python docs ), but there are other options as well:下面我将使用virtualenv ,现在似乎是推荐的工作方式(参见 Python 文档),但还有其他选项:
    • pip install --user virtualenv if it is not installed yet (it is installed by default for the newer Python versions). pip install --user virtualenv如果尚未安装(默认情况下安装较新的 Python 版本)。 By the way, personally, I use Poetry instead of pip for a while now, and I like it a lot.顺便说一句,就我个人而言,我暂时使用Poetry而不是pip ,我非常喜欢它。
    • Go to the desired directory (with cd <path> ) and create the environment with python3 -m venv <my_venv_name> . Go 到所需目录(使用cd <path> )并使用python3 -m venv <my_venv_name>创建环境。
    • Activate the environment with source <my_venv_name>/bin/activate (deactivate with deactivate ).使用source <my_venv_name>/bin/activate环境(使用deactivate )。
  • Make sure ipykernel is installed ( pip install --user ipykernel )确保安装了ipykernel ( pip install --user ipykernel )
  • Add the venv to Jupyter with python3 -m ipykernel install --user --name=<any_name_referring_to_your_venv> .使用python3 -m ipykernel install --user --name=<any_name_referring_to_your_venv>将 venv 添加到 Jupyter。

Now you should be able to select in Jupyter this environment as a kernel, and all packages installed in that venv should be picked up by Jupyter automatically as well (you'll need to restart the kernel in that case though).现在你应该能够在 Jupyter 这个环境中将 select 作为 kernel,并且安装在那个 venv 中的所有包也应该由 Jupyter 自动获取(尽管在这种情况下你需要重新启动 kernel)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM