Jupyter 使用手冊
===
###### tags: `使用手冊`
## 安裝不同的python version 和 虛擬環境 (for 使用者)
- 這裏會安裝 pyenv 來切換不同的 python version
- 並打造類似 conda 的虛擬環境
1. 從 github 安裝 pyenv
- `git clone https://github.com/pyenv/pyenv.git ~/.pyenv`
2. 設定 pyenv
- 沒有 `~/.bashrc` 記得用 `touch ~/.bashrc` 創建新檔案
- 使用以下指令將設定加入 `~/.bashrc` 和 `.profile`
```
echo -e 'if shopt -q login_shell; then' \
'\n export PYENV_ROOT="$HOME/.pyenv"' \
'\n export PATH="$PYENV_ROOT/bin:$PATH"' \
'\n eval "$(pyenv init --path)"' \
'\nfi' >> ~/.bashrc
echo -e 'if [ -z "$BASH_VERSION" ]; then'\
'\n export PYENV_ROOT="$HOME/.pyenv"'\
'\n export PATH="$PYENV_ROOT/bin:$PATH"'\
'\n eval "$(pyenv init --path)"'\
'\nfi' >>~/.profile
echo 'if command -v pyenv >/dev/null; then eval "$(pyenv init -)"; fi' >> ~/.bashrc
```
3. 從 github 安裝 pyenv-virtualenv
- 注意,請務必確認已經安裝 pyenv 並正確設定好 `.bashrc` 後, 再來執行此步驟,以免發生路徑錯誤
- `git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv`
4. 讀取 `.bashrc`
- `source ~/.bashrc`
5. 更多設定 (optional)
- 如果無法正常運行在設定即可
```
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
source ~/.bashrc
```
6. pyenv 安裝 Python。
- 這裏使用安裝 python 3.8.16 作爲範例
1. 查看所有可安裝的 python version
- `pyenv install -l`
2. 安裝 python 3.8.16
- `pyenv install 3.8.16`
```
Downloading Python-3.8.16.tar.xz...
-> https://www.python.org/ftp/python/3.8.16/Python-3.8.16.tar.xz
Installing Python-3.8.16...
```
3. 查看已安裝的 python version
- `pyenv versions`
4. 設定環境 (可跳過)
- global 對應於全局 `pyenv global 3.8.16`
- local 對應於當前資料夾 `pyenv local 3.8.16`
- shell 對應於當前 shell `pyenv shell 3.8.16`
- 優先順序是 shell > local > global
5. 建立虛擬環境
- 在此命名虛擬環境爲 "torch2.0"
- `pyenv virtualenv 3.8.16 torch2.0`
- 需要指定 pyenv 已安裝的 Python 版本, 此處指定 `3.8.16`
- 不指定版本時, 會以當前 pyenv 正在使用的版本建立虛擬環境
6. 啟動虛擬環境
- `pyenv activate torch2.0`
7. 安裝 ipykernel 並創建kernel
- 在此命名kernel名稱爲 "torch2.0.1"
```
python3 -m pip install --upgrade pip
python3 -m pip install ipykernel
python3 -m ipykernel install --user --name="torch2.0.1"
```
- 安裝好後就可以到jupyterhub上查看, 是否有新的kernel環境
- 而後可以在虛擬環境中使用 `python3 -m pip` 安裝套件
8. **非常重要!!!** 果要在kernel "torch2.0.1" 安裝套件, 請使用terminal並進入該虛擬環境 `torch2.0`, 使用 `python3 -m pip` 安裝套件, 範例如下 (`$`表示terminal)
```
$ pyenv activate torch2.0
(torch2.0) $ python3 -m pip install matplotlib
(torch2.0) $ python3 -m pip install numpy pandas
(torch2.0) $ python3 -m pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/cu118
```
9. 退出虛擬環境
- `pyenv deactivate torch2.0`
* 如需要移除虛擬環境請使用 `pyenv uninstall torch1.6`
[==> 參考資料1](https://www.maxlist.xyz/2022/05/06/python-pyenv/)
[==> 參考資料2](https://blog.kyomind.tw/ubuntu-pyenv/)
## 切換 bash 作爲 default shell
1. 在家目錄下創立 `.profile` 並添加
```
if [ "$SHELL" != "/bin/bash" ]
then
export SHELL="/bin/bash"
exec $(which bash)
. "$HOME/.bashrc"
fi
```
2. 在家目錄下創立 `.bashrc` 並添加 (請使用複製貼上, 不要傻傻一個字一個字打)
```
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
```
3. 重新登入
## Jupyterlab Theme (其實不是很好用的說)
在 terminal 下指令
```
pip install jupyterlab-midnightsea-theme
pip install jupyterlab-theme-hale
pip install jupyterlab-gt-coar-theme
pip install quetz-theme
pip install jupyterlab-night
pip install jupyterlab-accessible-themes
pip install jupyter-theme-christmas
pip install jupyterlab_gpulab_theme_dark
pip install jupyterlab-theme-umich
pip install jupyterlab-miami-nights
```
到 Settings -> Theme 去選擇不同的 theme
