需求
最近想要從conda2切換到conda3,但是因為之前的許多應用還是依賴於python2的環境,於是就產生了動態切換conda3的需求
分析
筆者使用的是Ubuntu的環境,並且conda是安裝在用戶目錄下的,也就是/home/user/anaconda2
,成功安裝conda3之后,也會在用戶目錄下生成目錄/home/user/anaconda3
,於是我想應該只需要動態切換一些環境變量就能實現動態切換了
解決方案
安裝anaconda3
去官方目錄下載最新版本的anaconda3,筆者下載好的文件是:Anaconda3-2019.10-Linux-x86_64.sh
, 在終端目錄下運行:
$ bash Anaconda3-2019.10-Linux-x86_64.sh
在安裝的最后一步會提示需不需要運行conda init
,這個時候選擇yes
這項功能會在~/.bashrc
文件下生成一段shell代碼
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/user/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/home/user/anaconda3/etc/profile.d/conda.sh" ]; then
# . "/home/user/anaconda3/etc/profile.d/conda.sh" # commented out by conda initialize
else
# export PATH="/home/user/anaconda3/bin:$PATH" # commented out by conda initialize
fi
fi
unset __conda_setup
# <<< conda initialize <<<
筆者之前安裝的anaconda2
生成的代碼是這樣的
# added by Anaconda2 5.3.0 installer
# >>> conda init >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/home/user/anaconda2/bin/conda' shell.bash hook 2> /dev/null)"
if [ $? -eq 0 ]; then
\eval "$__conda_setup"
else
if [ -f "/home/user/anaconda2/etc/profile.d/conda.sh" ]; then
# . "/home/user/anaconda2/etc/profile.d/conda.sh" # commented out by conda initialize
CONDA_CHANGEPS1=false conda activate base
else
\export PATH="/home/user/anaconda2/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda init <<<
於是筆者就想能不能把這2段初始化代碼放到shell函數中,然后用一個參數選擇初始化那一段代碼
select_conda() {
if [ $1 == "conda2" ]
then
# added by Anaconda2 5.3.0 installer
# >>> conda init >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/home/user/anaconda2/bin/conda' shell.bash hook 2> /dev/null)"
if [ $? -eq 0 ]; then
\eval "$__conda_setup"
else
if [ -f "/home/user/anaconda2/etc/profile.d/conda.sh" ]; then
# . "/home/user/anaconda2/etc/profile.d/conda.sh" # commented out by conda initialize
CONDA_CHANGEPS1=false conda activate base
else
\export PATH="/home/user/anaconda2/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda init <<<
fi
if [ $1 == "conda3" ]
then
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/user/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/home/user/anaconda3/etc/profile.d/conda.sh" ]; then
# . "/home/user/anaconda3/etc/profile.d/conda.sh" # commented out by conda initialize
else
# export PATH="/home/user/anaconda3/bin:$PATH" # commented out by conda initialize
fi
fi
unset __conda_setup
# <<< conda initialize <<<
fi
}
測試
當我想要選擇anaconda2
的時候,就在~/.bashrc
中加上
select_conda "conda2"
重新打開一個終端:
$ python
Python 2.7.12 (default, Oct 8 2019, 14:14:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
當我想要選擇anaconda3
的時候,就在~/.bashrc
中加上
select_conda "conda3"
終端運行:
$ . ~/.bashrc
$ python
Python 3.7.4 (default, Aug 13 2019, 20:35:49)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.