马春杰杰 Exit Reader Mode

[mcj]如何在ubuntu的Jupyter中使用对应的环境

在安装了anaconda之后,我新建了一个环境pytorch,然后我想在以后用jupyter notebook来操作这个pytorch虚拟环境,那么我该怎么做呢?有两个方法

方法1:

首先激活虚拟环境

conda activate pytorch

然后,新建一个文件夹,这个文件夹就是之后的工作目录,比如我新建了一个名为pytorch的文件夹,然后进入这个文件夹

mkdir pytorch
cd pytorch
jupyter notebook

输入这三条命令之后,会自动打开浏览器的notebook

(pytorch) ubuntu@mcj:~/pytorch$ jupyter notebook
[I 11:21:09.699 NotebookApp] [nb_conda_kernels] enabled, 1 kernels found
[I 11:21:10.441 NotebookApp] [nb_conda] enabled
[I 11:21:10.441 NotebookApp] Serving notebooks from local directory: /home/ubuntu/pytorch
[I 11:21:10.441 NotebookApp] The Jupyter Notebook is running at:
[I 11:21:10.441 NotebookApp] http://localhost:8888/?token=bdc3b5189846063c99b94624075721658017d1f058bddc31
[I 11:21:10.441 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 11:21:10.444 NotebookApp] 
    
    To access the notebook, open this file in a browser:
        file:///run/user/1000/jupyter/nbserver-17990-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=bdc3b5189846063c99b94624075721658017d1f058bddc31

(firefox:18101): LIBDBUSMENU-GLIB-WARNING **: Unable to get session bus: Error spawning command line 'dbus-launch --autolaunch=202260e9bbf841ffaa0cc912d02844aa --binary-syntax --close-stderr': Child process exited with code 1
[I 11:21:15.609 NotebookApp] Kernel started: 256a53e2-18ed-4819-8ff6-d069bacebc8e
[W 11:21:15.690 NotebookApp] 404 GET /nbextensions/widgets/notebook/js/extension.js?v=20190407112109 (127.0.0.1) 8.21ms referer=http://localhost:8888/notebooks/door.ipynb
[I 11:21:16.581 NotebookApp] Adapting to protocol v5.1 for kernel 256a53e2-18ed-4819-8ff6-d069bacebc8e

这时,打开的notebook已经是我们想要的虚拟环境了。

方法2:

可以先不激活,首先安装nbconda

conda install nb_conda

然后直接在工作目录输入

jupyter notebook

这时,我们可以选择kernel,选择我们想要的环境了