conda创建和使用python的虚拟环境

倾然丶 夕夏残阳落幕 提交于 2021-02-12 04:28:55

https://uoa-eresearch.github.io/eresearch-cookbook/recipe/2014/11/20/conda/

当我们使用服务器的时候,会存在多个用户,并且可能系统管理员也安装了anaconda,这样python指向的环境有肯能是管理员的目录,

我们可以通过 conda env list 查看存在的环境,以及当前使用的是哪个环境, 可以根据pytorch的不同版本安装在相应的环境中,便于适应不同的项目

$conda env list
# conda environments:
#
pytorch1.0               /home/username1/.conda/envs/pytorch1.0
                         /home/username1/anaconda2
base                  *  /home/username2/install/anaconda2

其中星号表示当前使用的环境路径,我们可以通过以下方式修改

1. 创建虚拟环境

conda create -n env_name python=2.7.14

env_name:要创建的虚拟环境名称

2. 激活环境

source activate env_name

3. 查看环境

conda env list

显示结果,当前环境已经变为我们设置的目录了

# conda environments:
#
pytorch1.0            *  /home/username1/.conda/envs/pytorch1.0
                         /home/username1/anaconda2
base                     /home/username2/install/anaconda2

4. 退出环境

conda deactivate

5. 删除环境

conda remove -n env_name --all

 注意:win10下cmd或者powershell经常无法激活虚拟环境,即如下错误

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If your shell is Bash or a Bourne variant, enable conda for the current user with

    $ echo ". D:\Anaconda3/etc/profile.d/conda.sh" >> ~/.bashrc

or, for all users, enable conda with

    $ sudo ln -s D:\Anaconda3/etc/profile.d/conda.sh /etc/profile.d/conda.sh

The options above will permanently enable the 'conda' command, but they do NOT
put conda's base (root) environment on PATH.  To do so, run

    $ conda activate

in your terminal, or to put the base environment on PATH permanently, run

    $ echo "conda activate" >> ~/.bashrc

Previous to conda 4.4, the recommended way to activate conda was to modify PATH in
your ~/.bashrc file.  You should manually remove the line that looks like

    export PATH="D:\Anaconda3/bin:$PATH"

^^^ The above line should NO LONGER be in your ~/.bashrc file! ^^^

解决办法:可以使用anaconda自带的Anaconda Prompt运行

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!