简体   繁体   English

Jupyter Notebook 和 virtualenv

[英]Jupyter Notebook and virtualenv

I have installed Jupyter notebook and use it with a virtualenv, I already have installed some packages in python but when I run in Jupyter notebook for example import numpy as np, I have a issue, module not found, there are a way to connect all of my python installed packages with Jupyter notebook?我已经安装了 Jupyter 笔记本并将其与 virtualenv 一起使用,我已经在 python 中安装了一些软件包,但是当我在 Jupyter 笔记本中运行时,例如 import numpy as np,我有一个问题,找不到模块,有办法连接所有我的 python 安装了 Jupyter 笔记本的软件包?

Thanks!谢谢!

To access the packages within your environment you have to register the kernel with jupyter.要访问您环境中的包,您必须使用 jupyter 注册 kernel。

  1. Switch to your virtual environment.切换到您的虚拟环境。
  2. install ipykernel pip install ipykernel安装 ipykernel pip install ipykernel
  3. pass the virtual environment name to the following command python -m ipykernel install --user --name=[name of your environment]将虚拟环境名称传递给以下命令python -m ipykernel install --user --name=[name of your environment]
  4. launch jupyter jupyter notebook启动 jupyter jupyter notebook
  5. select your notebook select 你的笔记本
  6. In the menu under your notebook name select kernel>change kernel>[name of your environment]在您的笔记本名称下的菜单中 select kernel>change kernel>[name of your environment]

As below:如下:

在此处输入图像描述

There are two things to consider:有两点需要考虑:

  1. Have you installed those modules inside the virtual env?您是否在虚拟环境中安装了这些模块?
  2. Are you launching jupyter from inside the virtual env?你是从虚拟环境中启动 jupyter 吗?

If you perform those actions correctly, then the error should go away.如果您正确执行了这些操作,那么错误应该 go 消失。

Hope this help!希望这有帮助!

The jupyter notebook command may not be referencing to the jupyter installation in the virutal environment you are using. jupyter notebook命令可能没有引用您正在使用的虚拟环境中的 jupyter 安装。 This is why even though you might have installed packages in your environment, jupyter is unable to import them because it is looking at a different place.这就是为什么即使您可能已经在您的环境中安装了软件包,jupyter 也无法导入它们,因为它正在查看不同的位置。

You could try to set up a different kernel for your environment, but I find that task to be tedious and after a while it becomes hard to keep track of your kernels.您可以尝试为您的环境设置不同的 kernel,但我发现这项任务很乏味,并且一段时间后很难跟踪您的内核。

The best way would be to start jupyter notebook with the python environment where all your modules are installed.最好的方法是使用安装了所有模块的 python 环境启动 jupyter notebook。 To do this, activate your virtual environment, and then do:为此,请激活您的虚拟环境,然后执行以下操作:

python -m jupyter notebook

This will open Jupyter with the jupyter installed in the place where your python is pointing to and it will have the packages installed there!这将打开 Jupyter 并在您的python指向的位置安装 jupyter,并且它将在那里安装软件包!

If you installed your packages in python and launched your jupyter notebook from your virtualenv, you cannot import those installed packages because virtualenv created an isolated Python environment.如果你在 python 中安装了你的包并从你的 virtualenv 启动了你的 jupyter notebook,你不能导入那些安装的包,因为 virtualenv 创建了一个隔离的 Python 环境。 You can read about virtualenv here .您可以在此处阅读有关 virtualenv 的信息。

There are two ways to resolve your problem:有两种方法可以解决您的问题:

  1. Launch jupyter notebook without virtualenv (simply type jupyter notebook in your terminal / command prompt)在没有 virtualenv 的情况下启动 jupyter notebook(只需在终端/命令提示符下输入jupyter notebook

  2. Install those packages inside your virtual environment.在您的虚拟环境中安装这些软件包。

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

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