jupyter notebook的入门配置、安装、启动、添加虚拟环境、小技巧

把之前使用jupyter时的记录与遇到过的问题总结一下,给自己做个备份,也希望能对大家有所帮助。毕竟这个东西还是蛮好用的,尤其是在做数据分析的时候,当数据量很大时,不用反复加载数据,可以大大节约等待的时间,即写即看。

目录

jupyter的安装

启动jupyter

jupyter中添加conda创建的虚拟环境

jupyter安装nbextensions 显示添加目录

jupyter的自动换行配置

jupyter行号显示是:


 

jupyter的安装

即使自己没有root权限与密码,也还是可以通过--user来安装软件

命令汇总

pip3 install  -i https://pypi.tuna.tsinghua.edu.cn/simple jupyter --user

或者

 pip install  -i https://pypi.tuna.tsinghua.edu.cn/simple jupyter --user 

mkdir ~/.jupyter

source ~/.bash_profile

jupyter notebook password

jupyter notebook --ip=0.0.0.0 --port=8903

详细步骤:

下载安装

pip3 install jupyter --user

 pip3 install  -i https://pypi.tuna.tsinghua.edu.cn/simple jupyter --user

安装完后,记得为jupter notebook的运行找自己当前用户的home目录创建一个保存访问信息的文件目录。

mkdir ~/.jupyter

注意:如果报错启动jupyter notebook ,提示 bash: jupyter: command not found没有需要重新启动配置环境。

source ~/.bash_profile

设置从浏览器登录使用jupyter notebook的登录密码

jupyter notebook password

输入登录密码。 (直接摁enter作为登录密码也可以)

会显示:

Wrote hashed password to /文件路径 /.jupyter/jupyter_notebook_config.json

Wrote hashed password to /home/xxxx/.jupyter/ jupyter_notebook_config.json

启动挂起程序,可以让在其他位置机器通过浏览器访问。常用在服务器上启动,然后自己本机终端浏览器使用

创建指定访问的登录端口port,不能用其他软件已经设占用了的端口。一般会设为比较大的数字通常被占用的可能性小点

jupyter notebook --ip=0.0.0.0 --port=13343

ppp端口此设置的为13343.dev4

在终端浏览器通过服务器+ip地址登录,http://ip地址:13343 

 

启动jupyter

如果服务器直接输入jupyter notebook  提示bash: jupyter: command not found

那就需要重新启动下配置环境,直接输入命令

source ~/.bash_profile

先用xshell在服务器终端启动。可以先用cd进入自己的目标目录

将jupyter 挂起

nohup jupyter notebook --ip=0.0.0.0 --port=8903 &

然后在终端自己的电脑用浏览器输入地址加端口:
http://服务器ip:13343

 

关掉后台运行的jupyter notebook

需要先使用ps命令(用job 命令只要中间连接一中断,就看不到了)

ps -ef |grep jupyter

看结果中的第一个数字值,表示pid ,jupyter的进程号,注意第二个数字值是父进程。

然后用kill -9 pid

 

 

jupyter中添加conda创建的虚拟环境

参考:https://blog.csdn.net/u014665013/article/details/81084604

前奏:自行安装anaconda,并创建虚拟环境(如自己之前创建的tf环境。可以通过conda env list命令查看到之前是否已经有安装)。(如果没有可以通过 conda create -n 环境名 命令来创建)

首先安装ipykernel

在terminal下执行命令行:conda install ipykernel

在虚拟环境下创建kernel文件 (即先**环境了,命令  source activate 环境名)

在terminal下执行命令行:conda install -n 环境名称 ipykernel

比如我的虚拟环境叫python27(后面举例都默认这个虚拟环境),那么我的就是:conda install -n python27 ipykernel

conda install -n tf ipykernel

**conda环境

在terminal下执行命令行:

windows版本:source activate 环境名称 我的命令是:source activate python27

linux版本:source activate 环境名称我的命令是:source activate python27

将环境写入notebook的kernel中

python -m ipykernel install --user --name 环境名称 --display -name "jupyter中显示的环境名称"

这里引号里面的名称自己可以随便起,用于在jupyter里面做标识,这里我仍然在jupyter里面叫python27,所以我的命令是:python -m ipykernel install --user --name python27 --display-name "python27"

打开notebook服务器

在terminal下执行命令行jupyter notebook

上面的相关步骤就可以完成jupyter的相关配置

如果经常需要用jupyter notebook,那么可以直接在创建虚拟环境(还指定了python版本)的时候便安装好ipykernel

命令:conda create -n 环境名称 python=3.6  ipykernel

另外删除kernel环境:

jupyter kernelspec remove 环境名称

 

技巧1:jupyter安装nbextensions 显示添加目录

 

xshell中:

确保没有运行jupyter notebook(可以用ps -u username查看)  ,然后再在xshell中:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple jupyter_contrib_nbextensions --user

jupyter contrib nbextension install --user --skip-running-check

 

jupyter contrib nbextension install --user --skip-running-check

会闪过一串东西。

然后再启动jupyter,再浏览器中输入地址与端口,会看到多了一个Nbextensions。

jupyter notebook的入门配置、安装、启动、添加虚拟环境、小技巧

 

然后再去打开ipynb文件的时候,有目录的就会默认在左侧生成目录。

 

技巧2:jupyter的自动换行配置

jupyter中默认的情况,对于一大长串的内容,并不会自动换行,显得一行中特别长,需要左右拉动窗口才能看到另一侧的信息,但是这个只需要在配置文件中配置一下,即可实现自动换行与缩进。

在配置文件中设置一下。

第一步,用

jupyter --config-dir

这个命令查看自己jupyter的配置文件位置,一般都是一个叫.jupyter的文件夹。

第二步,找到并打开

nbconfig/notebook.json

文件夹,没有就新建一个。

第三步,添加配置如***意json格式

{

  "MarkdownCell": {

    "cm_config": {

      "lineWrapping": true

    }

  },

  "CodeCell": {

    "cm_config": {

      "lineWrapping": true

    }

  }

}

如果里面已经有内容了,注意只需要上文第一个大括号里面的东西添加到原来的有的大括号里面即可(就只需要一个大的json串就可以了),并不需要两个外层大括号了。

第四步,重启jupyter-notebook

source ~/.bash_profile

鸣谢与参考
链接:https://www.zhihu.com/question/56110064/answer/217777230
 

 

技巧3:jupyter行号显示是:

Jupyter Notebook默认不显示行号,不方便我们调试排错。操作如下:

jupyter notebook的入门配置、安装、启动、添加虚拟环境、小技巧

找到菜单View找到点击Toggle Line Numbers,即可

鸣谢:各参考连接的原作者