virtualenv安装使用
用pip安装的好像不太好使,使用下面方式安装的可以
#安装virtualenv
sudo apt install python3.8-venv
#创建自己的virtual_env目录
python3 -m venv my_env
我这里是python3.8,所以安装3.8版的
然后在~/.bashrc 里加上
export PATH=${HOME}/.local/bin:${PATH}
jupyter 安装使用
sudo pip3 install jupyter
1.在命令行中切换到虚拟环境,然后执行
比如
source bin/activate
pip install --user ipykernel
如果出现报错
ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.
则去掉--user执行
#创建自己env 的kernel
python -m ipykernel install --user --name=pythonLearn
安装完成 ,在Jupyter Notebook中切换为对应的环境即可
启动jupyter
方法1. ssh远程使用jupyter notebook
- 在远程服务器上,启动jupyter notebooks服务:
jupyter notebook --no-browser --port=8889
- 在本地终端中启动SSH:
ssh -N -f -L localhost:8888:localhost:8889 username@serverIP
其中: -N 告诉SSH没有命令要被远程执行; -f 告诉SSH在后台执行; -L 是指定port forwarding的配置,远端端口是8889,本地的端口号的8888。
注意:username@serverIP替换成服务器的对应账号。
- 最后打开浏览器,访问:http://localhost:8888/
方法2. 利用jupyter notebook自带的远程访问功能
官方指南在此:官方指南
- 生成默认配置文件
jupyter notebook --generate-config
- 生成访问密码(token)
终端输入ipython
,设置你自己的jupyter访问密码,注意复制输出的sha1:xxxxxxxx
密码串
In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'sha1:xxxxxxxxxxxxxxxxx'
- 修改
./jupyter/jupyter_notebook_config.py
中对应行如下
c.NotebookApp.ip='*'
c.NotebookApp.password = u'sha:ce...刚才复制的那个密文'
c.NotebookApp.open_browser = False
c.NotebookApp.port =8888 #可自行指定一个端口, 访问时使用该端口
NotebookApp.allow_remote_access = True
- 在服务器上启动
jupyter notebook
jupyter notebook
- 最后打开浏览器,访问:http://ip:8888/
方法3:
配置自己的config文件,如./jupyter/jupyter_notebook_config_backup.py
jupyter notebook --config ./jupyter/jupyter_notebook_config_backup.py
自启动配置
sudo vi /etc/systemd/system/jupyter_start.service
[Unit]
Description=Jupyter Notebook
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/jupyter-notebook --no-browser
User=XXXX
Group=XXXX
WorkingDirectory=/home/XXXX/workspace/python_env/jupyter
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start jupyter_start
sudo systemctl status jupyter_start
jupyter_start.service - Jupyter Notebook
Loaded: loaded (/etc/systemd/system/jupyter_start.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2021-08-01 00:24:39 CST; 9s ago
Main PID: 73758 (jupyter-noteboo)
Tasks: 1 (limit: 9426)
Memory: 43.8M
CGroup: /system.slice/jupyter_start.service
└─73758 /usr/bin/python3 /usr/local/bin/jupyter-notebook --no-browser
8月 01 00:24:39 XXXX systemd[1]: Started Jupyter Notebook.
8月 01 00:24:39 XXXX jupyter-notebook[73758]: [I 00:24:39.392 NotebookApp] 启动notebooks 在本地路径: /home/XXXX/workspace/python_env/jupyter
8月 01 00:24:39 XXXX jupyter-notebook[73758]: [I 00:24:39.392 NotebookApp] Jupyter Notebook 6.4.0 is running at:
8月 01 00:24:39 XXXX jupyter-notebook[73758]: [I 00:24:39.392 NotebookApp] http://localhost:8888/
8月 01 00:24:39 XXXX jupyter-notebook[73758]: [I 00:24:39.392 NotebookApp] 使用control-c停止此服务器并关闭所有内核(两次跳过确认).