Linux远程配置virtual env + jupyter

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

  1. 在远程服务器上,启动jupyter notebooks服务:

jupyter notebook --no-browser --port=8889
  1. 在本地终端中启动SSH:

ssh -N -f -L localhost:8888:localhost:8889 username@serverIP

其中: -N 告诉SSH没有命令要被远程执行; -f 告诉SSH在后台执行; -L 是指定port forwarding的配置,远端端口是8889,本地的端口号的8888。

注意:username@serverIP替换成服务器的对应账号。

  1. 最后打开浏览器,访问:http://localhost:8888/

方法2. 利用jupyter notebook自带的远程访问功能

官方指南在此:官方指南

  1. 生成默认配置文件
    jupyter notebook --generate-config
  2. 生成访问密码(token)
    终端输入ipython,设置你自己的jupyter访问密码,注意复制输出的sha1:xxxxxxxx密码串

In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'sha1:xxxxxxxxxxxxxxxxx'
  1. 修改./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
  1. 在服务器上启动jupyter notebook
    jupyter notebook
  2. 最后打开浏览器,访问: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停止此服务器并关闭所有内核(两次跳过确认).

​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值