初始安裝時候自動初始化 每個用戶在使用conda的時候,都必須要指定一種shell類型,如bash,fish,tcsh,zsh等,當(dāng)在安裝conda的時候,其實安裝過程中已經(jīng)進(jìn)行了這樣的初始化操作。因而當(dāng)使用conda activate 命令的時候,能夠成功的執(zhí)行。假設(shè)這個時候使用的是root用戶進(jìn)行安裝的。
除了base環(huán)境外,現(xiàn)在,新建一個環(huán)境
1 2 3 4 5 6
[root]# conda create -n flask python=2.7 #現(xiàn)在有兩個虛擬環(huán)境 [root]# conda env list base * /usr/local /anaconda3 flask /usr/local /anaconda3/envs/flask
給其他用戶初始化conda現(xiàn)在,我們進(jìn)行以下操作:
切換到用戶www
查看conda有哪些環(huán)境
進(jìn)入flask環(huán)境1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
[root]# su www [www]# conda env list base * /usr/local /anaconda3 flask /usr/local /anaconda3/envs/flask [www]# conda activate flask CommandNotFoundError: Your shell has not been properly configured to use 'conda activate' . To initialize your shell, run $ conda init <SHELL_NAME> Currently supported shells are: - bash - fish - tcsh - xonsh - zsh - powershell See 'conda init --help' for more information and options. IMPORTANT: You may need to close and restart your shell after running 'conda init' .
從以上結(jié)果可以看出,conda是生效的,但是當(dāng)前用戶卻無法激活某個環(huán)境,那是因為目前還沒有初始化當(dāng)前用戶在使用conda時應(yīng)該使用什么shell.查看用戶目錄下的.bashrc文件,內(nèi)容如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[www]# vim ~/.bashrc #============================內(nèi)容如下======================================= # .bashrc # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrcfi # Uncomment the following line if you don't like systemctl's auto-paging feature: # export SYSTEMD_PAGER= # User specific aliases and functions ~#=========================================================================
如果你的情況如上,沒有關(guān)于任何conda init相關(guān)的任何內(nèi)容,那么就表明確實沒有初始化,現(xiàn)在來初始化:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[zhangweixi@dev001 ~]$ conda init bash no change /usr/local /anaconda3/condabin/conda no change /usr/local /anaconda3/bin/conda no change /usr/local /anaconda3/bin/conda-env no change /usr/local /anaconda3/bin/activate no change /usr/local /anaconda3/bin/deactivate no change /usr/local /anaconda3/etc/profile.d/conda.sh no change /usr/local /anaconda3/etc/fish/conf.d/conda.fish no change /usr/local /anaconda3/shell/condabin/Conda.psm1 no change /usr/local /anaconda3/shell/condabin/conda-hook.ps1 no change /usr/local /anaconda3/lib/python3.7/site-packages/xontrib/conda.xsh no change /usr/local /anaconda3/etc/profile.d/conda.csh modified /home/zhangweixi/.bashrc ==> For changes to take effect, close and re-open your current shell. <==
如果輸出這樣的信息,表示初始化成功,再看.bashrc文件,內(nèi)容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
[zhangweixi@dev001 ~]$ vim ~/.bashrc#=======================內(nèi)容如下=========================== # .bashrc # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrcfi # Uncomment the following line if you don't like systemctl's auto-paging feature: # export SYSTEMD_PAGER= # User specific aliases and functions # >>> conda initialize >>> # !! Contents within this block are managed by 'conda init' !! __conda_setup='$('/usr/local/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null) ' if [ $? -eq 0 ]; then eval '$__conda_setup ' else if [ -f '/usr/local/anaconda3/etc/profile.d/conda.sh' ]; then . '/usr/local/anaconda3/etc/profile.d/conda.sh' else export PATH='/usr/local/anaconda3/bin:$PATH ' fi fi unset __conda_setup# <<< conda initialize <<<
由此可以看出,conda在該文件中寫入了一些東西,那么這個試試來試試能激活環(huán)境嗎?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
[zhangweixi@dev001 ~]$ conda activate flask CommandNotFoundError: Your shell has not been properly configured to use 'conda activate' . To initialize your shell, run $ conda init <SHELL_NAME> Currently supported shells are: - bash - fish - tcsh - xonsh - zsh - powershell See 'conda init --help' for more information and options. IMPORTANT: You may need to close and restart your shell after running 'conda init' .
妹的,還是不行呀?到底啥原因?如果你切換當(dāng)前賬號到root,然后再切換回來www用戶來,再執(zhí)行conda activate flask
,你會發(fā)現(xiàn),成功的激活環(huán)境。原因在于:當(dāng)某用戶進(jìn)入系統(tǒng)后,會自動執(zhí)行用戶目錄下的.bashrc文件 ,當(dāng)?shù)谝淮纬跏蓟痗onda init后,把相應(yīng)的內(nèi)容出入了.bashrc文件,可是這些代碼并沒有在環(huán)境中執(zhí)行,要想立刻生效,可以通過手動執(zhí)行這個腳本:source ~/.bashrc
,這樣,再去激活環(huán)境就沒有什么問題了。
在shell腳本中激活環(huán)境現(xiàn)在來編寫一個很簡單的shell,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
[www]# vim test.sh #==================內(nèi)如如下============== #!/bin/bash conda activate flask#====================內(nèi)容結(jié)束=========== [www]# bash test.sh CommandNotFoundError: Your shell has not been properly configured to use 'conda activate' . To initialize your shell, run $ conda init <SHELL_NAME> Currently supported shells are: - bash - fish - tcsh - xonsh - zsh - powershell See 'conda init --help' for more information and options. IMPORTANT: You may need to close and restart your shell after running 'conda init' .
操蛋,居然不行,直接告訴如何操作把,把.bashrc文件中初始化conda的那一段代碼復(fù)制到test.sh文件的上面,變成這樣:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#=========================test.sh內(nèi)如如下========================= #!/bin/bash # >>> conda initialize >>> # !! Contents within this block are managed by 'conda init' !! __conda_setup='$('/usr/local/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null) ' if [ $? -eq 0 ]; then eval '$__conda_setup ' else if [ -f '/usr/local/anaconda3/etc/profile.d/conda.sh' ]; then . '/usr/local/anaconda3/etc/profile.d/conda.sh' else export PATH='/usr/local/anaconda3/bin:$PATH ' fi fi unset __conda_setup# <<< conda initialize <<< conda activate flaskwhich python #==============================內(nèi)容結(jié)束==================== [www]# bash test.sh /usr/local /anaconda3/envs/flask/bin/python
有此可見,conda激活成功,只不過是shell執(zhí)行完畢后,環(huán)境也就自動銷毀了,但是在shell的過程當(dāng)中,還是處于所激活的環(huán)境的。