远程服务器运行jupyter

最近发现Jupyter Notebook这个很好用的工具,想跑一跑实验但是发现本地没有环境,需要用到服务器上的conda,所以参考科研第二步:远程在服务器上跑程序jupyter使用

写这篇文章主要是为了记录自己搭建的过程,以便以后翻阅,大家可以参考原文,原文写的更好也更详细。

一、配置步骤

  1. 服务器上安装jupyter

在当前需要使用的conda环境下运行:

conda install ipykernel

此时jupyter已经可以使用了,直接输入jupyter notebook或者jupyter lab是可以用的,但是这样是不会跳出网页的,因为我们是远程连接服务器使用的jupyter,所以我们需要配置jupyter实现远程连接。

  1. 生成jupyter配置文件
jupyter notebook --generate-config

运行这个代码如果报错如下

xxx@xxxxx:~/xxxx$ jupyter notebook --generate-config
usage: jupyter [-h] [--version] [--config-dir] [--data-dir] [--runtime-dir] [--paths] [--json] [--debug] [subcommand]

Jupyter: Interactive Computing

positional arguments:
  subcommand     the subcommand to launch

optional arguments:
  -h, --help     show this help message and exit
  --version      show the versions of core jupyter packages and exit
  --config-dir   show Jupyter config dir
  --data-dir     show Jupyter data dir
  --runtime-dir  show Jupyter runtime dir
  --paths        show all Jupyter paths. Add --json for machine-readable format.
  --json         output paths as machine-readable json
  --debug        output debug information about paths

Available subcommands: kernel kernelspec migrate run troubleshoot

Jupyter command `jupyter-notebook` not found.

那就先后运行

pip install jupyter
pip install jupyter notebook

生成jupyter配置文件之后我们可以给jupyter设置一个密码

  1. 设置密码
jupyter notebook password

输入的密码会保存到 .jupyter/jupyter_notebook_config.json文件中

  1. 设置远程ip可访问
    找到jupyter_notebook_config.py文件,在文件中加入如下几行
    jupyter_notebook_config.py应该在.jupyter隐藏文件夹中,注意.jupyter应该在***/home/用户名***文件夹下
c.NotebookApp.ip = '*'    #允许所有ip访问,很重要
c.NotebookApp.open_browser = False    #不打开浏览器
c.NotebookApp.port = 8888             #端口为8888,可以自己设置

至此就可以使用刚才设置的密码和端口在自己电脑上的浏览器上连接jupyter了,首先在服务器上运行jupyter notebook或者jupyter lab
若服务器地址为10.24.82.184那么只需要在浏览器中输入10.24.82.184:8888(注意这里的冒号不要打成中文输入法的冒号),8888是刚才在配置文件中设置的端口号。

  1. .jupyter添加kernel

因为已经可以使用jupyter了,但是我们发现,在jupyter上运行ipynb的时候,内核只有一个默认的python3。

所以要添加kernel首先conda activate激活想要添加的环境的虚拟环境

然后使用which python查看当前虚拟环境python所在的路径,然后cd进入该文件夹
示例,进入虚拟环境tf27的python所在的路径,我们只需要进入到虚拟环境名称这个文件夹就好了,不用进入到bin/python
借用上面参考链接的图

然后在该目录下使用

python -m ipykernel install --user --name [虚拟环境名] --display-name [想要在jupyter上显示的环境名]

例如以tf27这个虚拟环境为例,我想让它在jupyter上显示TF27

python -m ipykernel install --user --name tf27 --display-name TF27

运行发现当前虚拟环境下没有ipykernel这个包

因此需要用conda下载,conda install ipykernel
下载完后重试即可,完成后在服务器上跑jupyter lab然后浏览器中输入[服务器ip]:[端口号]即可访问jupyter lab

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值