之前安装jupyter notebook通过其他方式总是出现各种问题,通过Anaconda3集成环境安装会少掉许多问题记录下安装过程我尽量写得详细些。安装jupyter notebook之前我们先安装Anaconda

一、下载Anaconda3

wget https://repo.anaconda.com/archive/Anaconda3-2018.12-Linux-x86_64.sh

二、安装Anaconda3

bash Anaconda3-2018.12-Linux-x86_64.sh

出现如下协议。

Welcome to Anaconda3 2018.12

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>> 

按enter键出现--More--然后一直按住enter知道出现如下询问

1.同意许可条款

Do you accept the license terms? [yes|no]
[no] >>>

输入yes

2.设置安装路径(可以默认)

Anaconda3 will now be installed into this location:
/root/anaconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/root/anaconda3] 

这里输入安装路径可以选择不输入然后直接按enter(直接按enter键默认安装在/root/anaconda3目录下)

3.Anaconda安装完成

installation finished.
Do you wish the installer to initialize Anaconda3
in your /root/.bashrc ? [yes|no]

输入yes完成序初始化Anaconda3

这里完成后会询问是否安装Microsoft VSCode

Do you wish to proceed with the installation of Microsoft VSCode? [yes|no]
>>> 

咋们用不到果断输入no拒绝,到此安装完成接下来配置下Anaconda3

三、配置Anaconda3

配置环境变量

Anaconda3之前默认安装在/root/anaconda3目录下那么这里的路径就填它

打开配置文件

vi /etc/profile

在最后一行加入:

#anaconda3
export ANACONDA_HOME="/root/anaconda3"
export PATH=$PATH:$ANACONDA_HOME/bin

然后保存退出,使用如下命令更新配置

source /etc/profile

四、安装jupyter notebook

先升级下pip

pip install --upgrade pip

pip安装jupyter notebook

pip install jupyter notebook

五、配置jupyter notebook

1.生成jupyter_notebook_config.py配置文件

jupyter notebook --generate-config

如果执行成功后,会出现下面的信息:

Writing default config to: /root/.jupyter/jupyter_notebook_config.py

2.配置密码

输入ipython

出现如下信息

Python 3.7.1 (default, Dec 14 2018, 19:28:38) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:

输入from notebook.auth import passwd

接着在输入passwd()

然后输入登录密码远程登录需要用,接着确认密码(注意:这里输入时是不会显示的)

下面附上完整过程

[root@wqwfnddfs-vdsvdvs ~]# ipython
Python 3.7.1 (default, Dec 14 2018, 19:28:38) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from notebook.auth import passwd                                                                                        

In [2]: passwd()                                                                                                                
Enter password: 
Verify password: 
Out[2]: 'sha1:cf201838bde1:c66c065d657f86e2bc8bb19cfe9ebd153391h789'

In [3]:  #这里不需要输入内容了直接按ctrl+z退出即可

上面完成后会显示sha1:cf201838bde1:c66c065d657f86e2bc8bb19cfe9ebd153391h789这样一串密钥保存好待会会用到

3.修改jupyter_notebook_config.py配置文件

接下来我们修改之前生成的jupyter_notebook_config.py配置文件

vi ~/.jupyter/jupyter_notebook_config.py

打开后大概看到的样式比较多我只例举了前几行(全部都是注释掉的)如果需要看全部配置项可以点击这里查看

# Configuration file for jupyter-notebook.

#------------------------------------------------------------------------------
# Application(SingletonConfigurable) configuration
#------------------------------------------------------------------------------

我们用不着配置这么多,只需找到如下几项设置即可(如果难找直接复制下面配置项改下你自己需求的部分)

# 设置访问的ip,这里将ip设置为*号,意味允许任何IP访问
c.NotebookApp.ip = '*'
# 这里的密钥就是上边我们生成的让你保存的那一串(注意格式 u'密钥' )
c.NotebookApp.password = u'sha1:cf201838bde1:c66c065d657f86e2bc8bb19cfe9ebd153391h789'
# 服务器上并没有浏览器可以供Jupyter打开(所以False关闭)
c.NotebookApp.open_browser = False
# 这里设置端口号,例如 8889 或其他自己喜欢的端口(记得开放你的端口号)
c.NotebookApp.port = 8889
# 设置jupyter的工作目录(此目录要已存)
c.NotebookApp.notebookdir = '/root/wanglin'
# 是否允许远程访问
c.NotebookApp.allow_remote_access = True

至此你的jupyter notebook已经安装并配置完成,接下来就是启动。

六、启动jupyter notebook

启动网上有好多,大体就是两种方式直接启动后台运行

1.直接启动(jupyter notebook)

jupyter notebook

出现如下显示说明启动成功

[root@wqwfnddfs-vdsvdvs ~]# jupyter notebook
[W 01:24:12.318 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 01:24:12.393 NotebookApp] JupyterLab extension loaded from /root/anaconda3/lib/python3.7/site-packages/jupyterlab
[I 01:24:12.393 NotebookApp] JupyterLab application directory is /root/anaconda3/share/jupyter/lab
[I 01:24:12.394 NotebookApp] Serving notebooks from local directory: /root
[I 01:24:12.394 NotebookApp] The Jupyter Notebook is running at:
[I 01:24:12.395 NotebookApp] http://(wqwfnddfs-vdsvdvs or 127.0.0.1):8889/
[I 01:24:12.395 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

注意:如果你是使用的root用户这里会报错,错误提示如下

Running as root is not recommended. Use --allow-root to bypass.

解决办法有两种

①是在之前上面的配置文件jupyter_notebook_config.py中找到

#c.NotebookApp.allow_root = False这行把它的False修改为True(注意:第一个字母是大写

或者直接复制#c.NotebookApp.allow_root = True(注意:把前面的#号去掉)

②是运行时加上 --allow-root推荐使用第①种方法免得后面出现问题麻烦

jupyter notebook --allow-root

2.后台运行(jupyter notebook)

接下来说说后台运行使用后台运行一般都是搭建在阿里之类的云服务器上的,可以实现随时随地的访问jupyter notebook方便快捷

启动命令如下(具体啥意思就不深究了,感兴趣有时间可以自己去百度哦)

nohup jupyter notebook > jupyter.log &

执行上面命令显示如下说明后台启动成功(不懂啥意思忽略提示按enter建)

[root@wqwfnddfs-vdsvdvs ~]# nohup jupyter notebook > jupyter.log &
[1] 29635 
[root@wqwfnddfs-vdsvdvs ~]# nohup: ignoring input and redirecting stderr to stdout

浏览器输入你的http://IP:8889即可访问你的jupyter notebook了(这里的IP指的是你服务器的外网IP)

注意:如果还是访问不了请检查一下你的防火墙是否允许8889这个端口

成功访问后的截图,这里输入之前配置密钥输入的密码即可登录

jupyter1

登录后界面

jupyter2

至此你的jupyter notebook已经全部完成

七、参考文档:

  1. Linux Centos7安装anaconda3和jupyter
  2. 配置 jupyter notebook远程访问
  3. 树莓派 CENTOS7 Python3.7安装 jupyter notebook远程可访问的服务器
  4. centos7重新安装python,yum。并部署jupyter notebook

版权声明:本文为原创文章,版权归 HEML 所有,转载请注明出处!
本文链接:https://www.wlinn.xyz/archives/19.html

Last modification:November 13th, 2019 at 11:42 am
如果觉得我的文章对你有用,请随意赞赏