在 Jupyter Notebook 中使用 conda 环境

Posted

技术标签:

【中文标题】在 Jupyter Notebook 中使用 conda 环境【英文标题】:Use conda environment in Jupyter Notebook 【发布时间】:2020-04-03 20:54:38 【问题描述】:

我在 Windows 10 上运行 Anaconda,并将 Jupyter Notebook 用于我的机器学习项目。

我最近开始了解 conda 中的虚拟环境。

我使用以下命令在 conda 中创建了一个虚拟环境

conda create -n test1

激活环境

activate test1

在 ipykernel 中安装的环境(不确定是否使用了正确的术语)

python -m ipykernel install --user --name test1

现在环境 test1 在 jupyter 笔记本中可见,但是当我启动它时,我收到了 kernel starting... please wait 消息。

这里有一些sn-ps。

也在 jupyter 提示符下(再次不确定条款)我收到以下错误

[I 21:08:24.924 NotebookApp] Creating new notebook in /MachineLearning/ImageProcessing
[I 21:08:29.550 NotebookApp] Kernel started: ec56795d-df5b-48d5-8f95-473ab1253407
Traceback (most recent call last):
  File "C:\Users\shesh\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\shesh\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\shesh\AppData\Roaming\Python\Python38\site-packages\ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "C:\Users\shesh\AppData\Roaming\Python\Python38\site-packages\traitlets\config\application.py", line 663, in launch_instance
    app.initialize(argv)
  File "<C:\Users\shesh\AppData\Roaming\Python\Python38\site-packages\decorator.py:decorator-gen-125>", line 2, in initialize
  File "C:\Users\shesh\AppData\Roaming\Python\Python38\site-packages\traitlets\config\application.py", line 87, in catch_config_error
    return method(app, *args, **kwargs)
  File "C:\Users\shesh\AppData\Roaming\Python\Python38\site-packages\ipykernel\kernelapp.py", line 527, in initialize
    self.init_sockets()
  File "C:\Users\shesh\AppData\Roaming\Python\Python38\site-packages\ipykernel\kernelapp.py", line 287, in init_sockets
    self.init_iopub(context)
  File "C:\Users\shesh\AppData\Roaming\Python\Python38\site-packages\ipykernel\kernelapp.py", line 295, in init_iopub
    self.iopub_thread = IOPubThread(self.iopub_socket, pipe=True)
  File "C:\Users\shesh\AppData\Roaming\Python\Python38\site-packages\ipykernel\iostream.py", line 68, in __init__
    self._setup_pipe_in()
  File "C:\Users\shesh\AppData\Roaming\Python\Python38\site-packages\ipykernel\iostream.py", line 141, in _setup_pipe_in
    self._pipe_in = ZMQStream(pipe_in, self.io_loop)
  File "C:\Users\shesh\AppData\Roaming\Python\Python38\site-packages\zmq\eventloop\zmqstream.py", line 127, in __init__
    self._init_io_state()
  File "C:\Users\shesh\AppData\Roaming\Python\Python38\site-packages\zmq\eventloop\zmqstream.py", line 546, in _init_io_state
    self.io_loop.add_handler(self.socket, self._handle_events, self.io_loop.READ)
  File "C:\Users\shesh\AppData\Roaming\Python\Python38\site-packages\tornado\platform\asyncio.py", line 99, in add_handler
    self.asyncio_loop.add_reader(fd, self._handle_events, fd, IOLoop.READ)
  File "C:\Users\shesh\AppData\Local\Programs\Python\Python38\lib\asyncio\events.py", line 501, in add_reader
    raise NotImplementedError
NotImplementedError
[W 21:09:29.591 NotebookApp] Timeout waiting for kernel_info reply from ec56795d-df5b-48d5-8f95-473ab1253407
[I 21:10:29.197 NotebookApp] Saving file at /MachineLearning/ImageProcessing/Untitled.ipynb

我只想在 jupyter notebook 中使用新创建的环境。

【问题讨论】:

这确实是“Conda environments not showing up in Jupyter Notebook”的副本 不幸的是,该线程上有很多错误的答案,但我可以特别推荐this answer 作为正确的路线。 @merv 我按照您的 cmets 中提供的答案中提到的步骤进行操作。我很困惑。每次我想使用该环境在 jupyter 中进行一些开发时,是否需要激活该特定环境? 我也想知道这个NotImplementedError 是什么意思。我正在执行任何错误的步骤吗?为什么它不起作用? 不,您总是使用 Jupyter 从 env 中激活和启动 Jupyter,然后您可以选择要在 notebook 中用作内核的 env 【参考方案1】:

请尝试以下步骤:

source activate test1
conda install ipykernel
ipython kernel install --name test1 --user 

【讨论】:

我尝试了这个解决方案,但没有奏效。实际上,只有在这样做之后,我才陷入了目前的境地。不知道我哪里做错了。 能否请您检查一下控制面板环境变量中python路径是否设置正确?

以上是关于在 Jupyter Notebook 中使用 conda 环境的主要内容,如果未能解决你的问题,请参考以下文章

将数据框从 jupyter notebook 写入雪花而不定义表列类型

在Jupyter notebook中使用特定虚拟环境中的python的kernel

怎么打开jupyter notebook?

Jupyter Lab 在错误的路径中打开,与 Jupyter Notebook 不同,两者在“jupyter_notebook_config.py”中具有相同的映射。

如何在Jupyter Notebook中使用Python虚拟环境?

在 jupyter notebook 中使用 joblib 时不显示打印输出