Server | 玩转conda和jupyter | 在HPC的节点上使用jupyter notebook

 

2024年07月16日

如果遇到这个bug:ModuleNotFoundError: No module named 'jupyter_server.contents'

pip uninstall traitlets
pip install traitlets==5.9.0

 

终于解决了这个跟幽灵一样的bug,password有时候有用,有时候没用,上一秒有用,下一秒没用。

根本原因:很多小弟Copy paste我的代码,连端口都懒得改,一旦我们投递到一个node上,就会发生问题,我可以登录,但无法使用我的密码,必须用ta的密码,这就导致了幽灵一样的bug。

确定了我用的是ServerApp,直接用jupyter server password生成密码即可,不存在其他问题,把端口改成一个特殊的端口,不要share给其他人。

仔细看看文档:

 

2024年07月09日

!!!不要随便更新核心工具,比如Python、R、Conda,痛苦无穷!!!

更新了jupyter lab,以前的password无效了,重新生成一下就可以了,但要用对命令jupyter server password。 

改成lab之后有点混乱了,移除掉之前的config文件,改用这个/home/zz950/softwares/miniconda3/bin/jupyter lab --no-browser --port=${port} --ip=${node}。

# not work
# jupyter notebook password
# Enter password:
# Verify password:
# [JupyterPasswordApp] Wrote hashed password to /home/zz950/.jupyter/jupyter_server_config.json

jupyter server password
Enter password:
Verify password:
[JupyterPasswordApp] Wrote hashed password to /home/zz950/.jupyter/jupyter_server_config.json

 

2024年06月25日

一个搞了我很久的疑难杂症:

  • conda新建了env,安装了PyTorch,shell上可以在Python里import torch,没问题,kernel也被加入了jupyter里;
  • 但一旦进入jupyter notebook,进入kernel,却死活都import torch不成功,在nb了也装了PyTorch,无济于事;
  • 最终发现:是kernel的添加方式有问题,用了mira的方式添加,一切就都正常了,就是这么小小的一步,搞了我n个小时!!!

 

source /home/zhl595/miniconda3/bin/activate /home/zhl595/miniconda3/envs/PyTorch_2_3_0
jupyter kernelspec list
jupyter kernelspec remove <kernel-name>

python -m ipykernel install --user --name PyTorch_2_3_0
# don't use this one
# ipython kernel install --name "PyTorch_2_3_0" --user

 

2024年05月21日 

添加新的kernel

# which ipython
# /home/zz950/softwares/miniconda3/envs/r4p3/bin/ipython
ipython kernel install --name "PyTorch2.3.0" --user
# remove
jupyter kernelspec remove old_kernel

 

参考:https://queirozf.com/entries/jupyter-kernels-how-to-add-change-remove

 

2023年09月14日

新bug,jupyter notebook不work,尤其是在远程输入password的时候,无法连接。

但使用jupyter-lab或者jupyter_server的时候则可以,其实test非常简单,都不用qsub,就在server上开jupyter,然后远程用 

 


 

将新env里的python kernel添加到jupyter里

ipython kernel install --name "scenic" --user

 

这是Mac的姊妹篇,讲述如何在Server上搭建数据分析环境。 

参考:R版本及R包版本的管理 | 历史版本 | 安装R4.0 | 安装Seurat v4 | nlopt

 

qsub任务投递环境配置

  • 华大、HKU、DFCI都是用qsub投递任务,但命令有些差异。
  • 主要是投递任务,在server上开启jupyter,然后在Mac上远程分析,因为Mac的资源远远不够。
  • 基本命令:qsub,qs/qstat, qhost, qdel. 差不多这些就够了。

 

深度分析环境配置 - 基本流程:

  • miniconda
  • conda装R和Python到一个环境r4p3
  • 装jupyter,插件
  • 装R和Python的kernel,以及load到jupyter里
  • 装R和Python

 

无密码进入【在本地创建id_rsa.pub,传到server,创建authorized_keys,改一下权限】

问题:可以在一台电脑上配置多个服务器【办公室的可以同时登陆argos和O2】,但家里的MacBook就不行了,必须输入密码。

scp $HOME/.ssh/id_rsa.pub   jf611@argos.dfci.harvard.edu:/home/jf611/
ssh jf611@argos
cat id_rsa.pub >> .ssh/authorized_keys
chmod 0600 .ssh/authorized_keys
rm -f id_rsa.pub

   

2022年10月20日

过渡期,在Macbook上更新所有conda tools

conda update conda && conda update conda-build

通过装seurat4顺带装上R4,核心包给拿下。

conda create -n r4p3 -c conda-forge -c bioconda r-seurat=4* python=3.8

 

不要在conda env里装jupyter,因为我们希望能够用base里的jupyter统领所有env下面的kernel。

conda install jupyter

  

nb添加目录

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user

  配置Jupyter,确保可以远程访问

jupyter notebook --generate-config
vi /Users/zhixinli/.jupyter/jupyter_notebook_config.py
ipython
In [1]: from notebook.auth import passwd In [2]: passwd() Enter password: Verify password: Out[2]: 'sha1:xxxxxxxxxxxxxxxxx'
c.NotebookApp.ip='*'
c.NotebookApp.password = u'sha:ce...刚才复制的那个密文'
c.NotebookApp.open_browser = False
c.NotebookApp.port =8888 #可自行指定一个端口, 访问时使用该端口

  

这个kernel就可以出现在kernel栏里了。 

jupyter kernelspec list
jupyter kernelspec remove local-venv

 

激活环境env,加入R和Python的kernels

conda activate r4p3

    

The IPython kernel is the Python execution backend for Jupyter.

conda install ipykernel
ipython kernel install --name "Python_3.8.15" --user

 

插播python2的安装,主要是为了运行SPRING。【也到但系安装ipython,最后用python2的语法测试】

conda create -n r3p2
conda activate r3p2
conda install  python=2.7
python -m pip install scikit-learn numpy scipy matplotlib h5py
python -m pip install networkx fa2 python-louvain
conda install -c conda-forge -c bioconda r-base=3

conda install ipykernel
ipython kernel install --name "Python_2.7" --user
print "fish"
print ("fish") # 注意print后面有个空格
print("fish") # print()不能带有任何其它参数

 

设置R包路径

mkdir /Users/zhixinli/R_lib_4
export R_LIBS_USER=/Users/zxli/R_lib_4

进入R env,加入R4.1 kernel

.libPaths()
.libPaths("/home/-/softwares/R_lib_361")

install.packages('IRkernel')
IRkernel::installspec()【用下面的具体指令】

install.packages("devtools", dependencies=TRUE, INSTALL_opts = c('--no-lock'))
library(devtools)
IRkernel::installspec(name = 'ir41', displayname = 'R 4.1')
IRkernel::installspec(name = 'ir42', displayname = 'R 4.2')

  

conda下任何R的依赖包基本都可以用conda来安装。

比如devtools的依赖包就需要conda来装

conda install -c conda-forge r-usethis r-pkgdown

 

杀掉ssh端口监听

lsof -ti:17435 | xargs kill -9

  

最后,在jupyter开启之前要导入R lib,否则会找不到。

export R_LIBS_USER=/home/zz950/softwares/R_lib_4

  

 


 

2022年08月09日

还是没彻底玩明白conda和jupyter。

conda倒是好理解,创建一个环境env,相当于一个目录,然后在里面自行安装各种软件,与外界保持独立,需要调用时直接source activate一下即可。

难点就在多环境管理上,python2/3,R3/4,它们的package是不能兼容的,一般的选择是把他们装到不同的conda env里,需要的时候就调用。

 

果然,工欲善其事必先利其器,有了下面这个工具,就不必频繁切换env了,版本和库随意切换,所以一定要配置好。

所以,根据我们应用的场景,我们最少需要两个env,一个是高版本(R4 + Python3),一个是低版本(R3 + Python2),其他用的最少的就单独装一个env。

 

conda install nb_conda_kernels

The main idea is to have multiple ipython kernels. The package nb_conda_kernels will automatically detect different conda environments with notebook kernels and automatically register them.  

This extension enables a Jupyter Notebook or JupyterLab application in one conda environment to access kernels for Python, R, and other languages found in other environments. When a kernel from an external environment is selected, the kernel conda environment is automatically activated before the kernel is launched. This allows you to utilize different versions of Python, R, and other languages from a single Jupyter installation.

 

references就用cellranger的,特殊情况下再装其他版本。

这样softwares和references就都管理好了,调用起来就特别方便。

能在集群上用jupyter就不要再自己电脑上用了,一个资源不够,二是不利于统一管理。 

 

 

参考:

 

 


 

2022年05月23日 更新

SPRING需要用到python2,所以必须在nb里装一个kernel

流程:

  1. 在conda里创建一个env
  2. 进入env装jupyter(pip即可),以及ipykernel
  3. 用绝对路径来投递任务

 


 

投递任务,注意资源设置

#!/bin/bash
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
#SBATCH --mem=4G
#SBATCH --time 00:05:00
#SBATCH --job-name jupyter-notebook
#SBATCH --output jupyter-notebook-%J.log

# get tunneling info
XDG_RUNTIME_DIR=""
node=$(hostname -s)
user=$(whoami)
cluster="tigercpu"
port=8889

# print tunneling instructions jupyter-log
echo -e "
Command to create ssh tunnel:
ssh -N -f -L ${port}:${node}:${port} ${user}@${cluster}.princeton.edu

Use a Browser on your local machine to go to:
localhost:${port}  (prefix w/ https:// if using password)
"

# load modules or conda environments here
module load anaconda3

# Run Jupyter
jupyter-lab --no-browser --port=${port} --ip=${node}

  

在本地电脑映射端口

ssh -N -f -L 8889:tiger-h26c2n22:8889 <yourusername>@tigercpu.princeton.edu

 

在浏览器中打开即可使用

  

有些分析比较耗费资源,结果文件也是上G的,这时再把结果copy到本地处理就也不合适了。

那就需要在HPC上使用python或R来处理数据,之前使用jupyter一直不成功,想把vim变成R的IDE,发现更难,各种配置很复杂,使用起来门槛也比较高。

今天碰巧搜素了PBS上运行jupyter,还真的找到了正确的配置方法,核心就是用ssh做了一个映射,用本地的端口来监听远程的端口,只要在一个局域网内,就能通过地址和主机名来连接,通过ssh协议来通讯

 

-N      Do not execute a remote command.  This is useful for just forwarding ports.
-f      Requests ssh to go to background just before command execution.  This is useful if ssh is going to ask for passwords or passphrases, but the user wants it in
             the background.  This implies -n.  The recommended way to start X11 programs at a remote site is with something like ssh -f host xterm.

             If the ExitOnForwardFailure configuration option is set to “yes”, then a client started with -f will wait for all remote port forwards to be successfully
             established before placing itself in the background.
-L local_socket:remote_socket
             Specifies that connections to the given TCP port or Unix socket on the local (client) host are to be forwarded to the given host and port, or Unix socket, on
             the remote side.  This works by allocating a socket to listen to either a TCP port on the local side, optionally bound to the specified bind_address, or to a
             Unix socket.  Whenever a connection is made to the local port or socket, the connection is forwarded over the secure channel, and a connection is made to
             either host port hostport, or the Unix socket remote_socket, from the remote machine.

             Port forwardings can also be specified in the configuration file.  Only the superuser can forward privileged ports.  IPv6 addresses can be specified by
             enclosing the address in square brackets.

             By default, the local port is bound in accordance with the GatewayPorts setting.  However, an explicit bind_address may be used to bind the connection to a
             specific address.  The bind_address of “localhost” indicates that the listening port be bound for local use only, while an empty address or ‘*’ indicates that
             the port should be available from all interfaces.

  

如此大部分的数据分析都可以用HPC来做了,有望实现数据分析的大一统。

在HPC上还有个优点,数据管理比较规范,数据也不容易丢失。

  

参考:

Jupyter on the Cluster

https://raw.githubusercontent.com/jalvesaq/Nvim-R/master/doc/Nvim-R.txt

https://gist.github.com/tgirke/7a7c197b443243937f68c422e5471899

 

posted @ 2019-12-09 10:50  Life·Intelligence  阅读(1625)  评论(0编辑  收藏  举报
TOP