python学习之科学计算的jupyter安装和配置 day11


活动地址:CSDN21天学习挑战赛

jupyter的环境

1,

linux操作系统,这里使用的版本是centos7

2,

Python环境,这里使用的是python3.8编译安装。




一,

Python3.8的编译安装

在编译安装前,需要配置编译环境,编译环境应该如下:

(1)Python3.8的安装包下载

官方下载地址:Download Python | Python.org

(2)可用的阿里云yum,阿里云yum文件内容如下:

[aliyun]
name=aliyun
baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
enable=1
gpgcheck=0

(3)

yum安装依赖,命令如下:

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel  gcc  libffi-devel deltarpm -y

 这里说明一下为什么这么多依赖,sqlit-devel如果不安装,Python3.8编译不会报错,但将会在运行jupyter的时候报错:ModuleNotFoundError: No module named 'sqlite'。

libffi-devel如果不安装,Python3.8编译不会报错,但将会在运行jupyter的时候报错:ModuleNotFoundError: No module named '_ctypes'

(4)

解压Python3.8的tar包,并进入解压后的目录,执行编译命令:

./configure --prefix=/usr/local/python3/
make && make install

编译安装成功的样子:

(cd /usr/local/python3/share/man/man1; ln -s python3.8.1 python3.1)
if test "xupgrade" != "xno"  ; then \
	case upgrade in \
		upgrade) ensurepip="--upgrade" ;; \
		install|*) ensurepip="" ;; \
	esac; \
	 ./python -E -m ensurepip \
		$ensurepip --root=/ ; \
fi
Looking in links: /tmp/tmp2ibn73jg
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-19.2.3 setuptools-41.2.0

(5)

编译安装完成后,将Python命令和pip命令加入系统环境变量:

ln -sf /usr/local/python3/bin/python3 /usr/bin/python
ln -sf /usr/local/python3/bin/pip3 /usr/local/bin/pip

pip源更改为阿里云源,具体做法参见我的博客:pip的官方源替换为国内高速镜像源_zsk_john的博客-CSDN博客_pip官方源 

升级一下pip:

[root@master Python-3.8.2]# pip install --upgrade pip
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting pip
  Downloading http://mirrors.aliyun.com/pypi/packages/1f/2c/d9626f045e7b49a6225c6b09257861f24da78f4e5f23af2ddbdf852c99b8/pip-22.2.2-py3-none-any.whl (2.0MB)
     |████████████████████████████████| 2.0MB 388kB/s 
Installing collected packages: pip
  Found existing installation: pip 19.2.3
    Uninstalling pip-19.2.3:
      Successfully uninstalled pip-19.2.3
Successfully installed pip-22.2.2

测试是否安装正确:

[root@master bin]# python
Python 3.8.8 (default, Aug 11 2022, 10:48:17) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
[root@master bin]# pip -V
pip 22.2.2 from /usr/local/python3/lib/python3.8/site-packages/pip (python 3.8)

以上输出表示Python编译安装成功。

二,

正式安装jupyter

(1)开始正式安装jupyter,执行以下命令安装:

pip install ipython jupyter

安装完毕的截图:

 Using cached http://mirrors.aliyun.com/pypi/packages/f0/36/639d6742bcc3ffdce8b85c31d79fcfae7bb04b95f0e5c4c6f8b206a038cc/zipp-3.8.1-py3-none-any.whl (5.6 kB)
Installing collected packages: webencodings, wcwidth, Send2Trash, pure-eval, ptyprocess, pickleshare, mistune, ipython-genutils, fastjsonschema, executing, backcall, zipp, traitlets, tornado, tinycss2, soupsieve, six, pyzmq, pyrsistent, pyparsing, pygments, pycparser, psutil, prompt-toolkit, prometheus-client, pkgutil-resolve-name, pexpect, parso, pandocfilters, nest-asyncio, MarkupSafe, lxml, jupyterlab-widgets, jupyterlab-pygments, entrypoints, defusedxml, decorator, debugpy, attrs, terminado, python-dateutil, packaging, matplotlib-inline, jupyter-core, jinja2, jedi, importlib-resources, cffi, bleach, beautifulsoup4, asttokens, stack-data, qtpy, jupyter-client, jsonschema, argon2-cffi-bindings, nbformat, ipython, argon2-cffi, nbclient, ipykernel, qtconsole, nbconvert, jupyter-console, notebook, widgetsnbextension, ipywidgets, jupyter
Successfully installed MarkupSafe-2.1.1 Send2Trash-1.8.0 argon2-cffi-21.3.0 argon2-cffi-bindings-21.2.0 asttokens-2.0.7 attrs-22.1.0 backcall-0.2.0 beautifulsoup4-4.11.1 bleach-5.0.1 cffi-1.15.1 debugpy-1.6.2 decorator-5.1.1 defusedxml-0.7.1 entrypoints-0.4 executing-0.9.1 fastjsonschema-2.16.1 importlib-resources-5.9.0 ipykernel-6.15.1 ipython-8.4.0 ipython-genutils-0.2.0 ipywidgets-7.7.1 jedi-0.18.1 jinja2-3.1.2 jsonschema-4.9.1 jupyter-1.0.0 jupyter-client-7.3.4 jupyter-console-6.4.4 jupyter-core-4.11.1 jupyterlab-pygments-0.2.2 jupyterlab-widgets-1.1.1 lxml-4.9.1 matplotlib-inline-0.1.3 mistune-0.8.4 nbclient-0.6.6 nbconvert-6.5.1 nbformat-5.4.0 nest-asyncio-1.5.5 notebook-6.4.12 packaging-21.3 pandocfilters-1.5.0 parso-0.8.3 pexpect-4.8.0 pickleshare-0.7.5 pkgutil-resolve-name-1.3.10 prometheus-client-0.14.1 prompt-toolkit-3.0.30 psutil-5.9.1 ptyprocess-0.7.0 pure-eval-0.2.2 pycparser-2.21 pygments-2.12.0 pyparsing-3.0.9 pyrsistent-0.18.1 python-dateutil-2.8.2 pyzmq-23.2.0 qtconsole-5.3.1 qtpy-2.2.0 six-1.16.0 soupsieve-2.3.2.post1 stack-data-0.3.0 terminado-0.15.0 tinycss2-1.1.1 tornado-6.2 traitlets-5.3.0 wcwidth-0.2.5 webencodings-0.5.1 widgetsnbextension-3.6.1 zipp-3.8.1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

 可以看到使用了非常多的依赖,主要的依赖是tornado,jinja2,ipython,jupyter-console,jupyter-client。

jupyter的可执行程序都安装在了前面编译安装Python的时候定义的那个目录 /usr/local/python3/bin目录下了:

[root@master bin]# ls
2to3              idle3     jsonschema                jupyter-dejavu      jupyter-migrate      jupyter-qtconsole        jupyter-trust  pydoc3      python3.8         send2trash
2to3-3.8          idle3.8   jupyter                   jupyter-execute     jupyter-nbconvert    jupyter-run              pip            pydoc3.8    python3.8-config  untitled.txt
aaa.ipynb         ipython   jupyter-bundlerextension  jupyter-kernel      jupyter-nbextension  jupyter-serverextension  pip3           pygmentize  python3-config
easy_install-3.8  ipython3  jupyter-console           jupyter-kernelspec  jupyter-notebook     jupyter-troubleshoot     pip3.8         python3     qtpy

(2)

jupyter的一些个性化设置

设置密码:

./jupyter notebook password
[root@master bin]# ./jupyter notebook password
Enter password: 
Verify password: 
[NotebookPasswordApp] Wrote hashed password to /root/.jupyter/jupyter_notebook_config.json
[root@master bin]# cat /root/.jupyter/jupyter_notebook_config.json
{
内容省略
  }

可以看到,密码jupyter做了加密,存放在了/root/.jupyter/jupyter_notebook_config.json这个文件内。

jupyter出于安全考虑,并不允许使用root账号启动,因此,需要设置为允许root用户启动,也不允许非本机访问jupyter,因此,需要将其做成一个服务,在其它服务器上都可以使用该jupyter服务,设置密码而不使用token是因为token每次启动的时候都是不一样的,需要在服务器上查看,而有了密码,将会非常方便的随时随地打开jupyter了。(注意,这些操作是非常不安全的,因此,jupyter不建议安装在重要的服务器上。)

  • 生成jupyter的配置文件
./jupyter notebook --generate-config

此命令会生成一个py文件,文件路径为/root/.jupyter/jupyter_notebook_config.py,如果配置文件修改的有问题或者想恢复到最开始的状态,再次执行以上命令就可以了,它会提示你是否覆盖原文件,选择y覆盖就可以了。

  • 修改jupyter的配置文件
vim /root/.jupyter/jupyter_notebook_config.py
#  Default: False  注释放开,修改为True
c.NotebookApp.allow_root = True
#  Default: 'localhost',注释放开,写入服务器ip
c.NotebookApp.ip = '192.168.217.16'

#  Default: 8888 修改端口号,随意定,我这里是18808,默认端口不是很安全的哦
c.NotebookApp.port = 18808
  • 启动jupyter

启动命令为:

./jupyter notebook

如果嫌麻烦,可以将jupyter命令放入系统环境变量,命令为;

ln -sf /usr/local/python3/bin/jupyter /usr/local/bin/
[root@master ~]# jupyter notebook
[I 17:13:15.247 NotebookApp] Serving notebooks from local directory: /root
[I 17:13:15.248 NotebookApp] Jupyter Notebook 6.4.12 is running at:
[I 17:13:15.248 NotebookApp] http://192.168.217.16:18808/
[I 17:13:15.248 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 17:13:15.265 NotebookApp] No web browser found: could not locate runnable browser.
[W 17:13:29.662 NotebookApp] Clearing invalid/expired login cookie username-192-168-217-16-18808
[W 17:13:29.664 NotebookApp] Clearing invalid/expired login cookie username-192-168-217-16-18808
[W 17:13:29.674 NotebookApp] Forbidden
[W 17:13:29.677 NotebookApp] 403 GET /api/sessions?_=1660204640102 (192.168.217.1) 17.350000ms referer=http://192.168.217.16:18808/tree?
[W 17:13:29.682 NotebookApp] Forbidden
[W 17:13:29.683 NotebookApp] 403 GET /api/terminals?_=1660204640103 (192.168.217.1) 19.240000ms referer=http://192.168.217.16:18808/tree?
[I 17:13:31.890 NotebookApp] 302 GET /tree? (192.168.217.1) 2.550000ms

可以看到,启动的时候不是监听127.0.0.1了,是我们设置的IP了,打开任意一个浏览器,输入IP+18808,截图如下:

 输入前面jupyter notebook password 设置的密码就可以愉快的使用jupyter了:

 总结

jupyter安装不是特别复杂,但有很多的细节需要注意,尤其是Python编译阶段,有一些依赖不安装也是可以编译成功的(比如,libffi-devel),但jupyter启动的时候就会出问题,此时,只有添加依赖后重新编译Python啦,否则,会一直报错。

后台运行jupyter命令:

nohup jupyter notebook 2>&1 &

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晚风_END

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值