自己的Anaconda管理多个虚拟环境,这样就可以在不同的环境下安装互不干扰的库

📅 2026/8/23 13:45:59
自己的Anaconda管理多个虚拟环境,这样就可以在不同的环境下安装互不干扰的库
项目场景Anaconda可以安装多个虚拟解释器每个解释器可以安装自己独有的库从而每个解释器之间起到互不干扰的作用这点Anaconda就非常强大了。查看Anaconda的解释环境在电脑开始中选择Anaconda Prompt (setup)之后输入conda env list就会显示出所有的已存在虚拟环境了。这里的base是安装Anaconda后自带的一个虚拟环境选择Anaconda Prompt (setup)后默认进入的也是这个环境其他环境例如alphapose等虚拟环境都是我自建的。镜像源配置找到用户目录下的.condarc文件Windows在C:\Users\用户名macOS/Linux在~/channels: - defaults show_channel_urls:truedefault_channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main custom_channels: conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud配置pip源配置文件路径*WindowsC:\Users\用户名\pip\pip.iniLinux/macOS~/.pip/pip.conf[global]index-urlhttps://pypi.tuna.tsinghua.edu.cn/simple extra-index-urlhttps://mirrors.aliyun.com/pypi/simple https://pypi.doubanio.com/simple管理自己的虚拟环境创建环境conda create-npytorch1.7python3.8输入以上代码即可创建自己的环境该环境名叫pytorch1.7 所用python解释器版本为3.8这里选择: y到这里表示环境创建成功下面就可以激活环境安装自己需要的库了。激活环境输入activate pytorch1.7激活环境下面pip install自己所需要的包(库)即可。退出环境输入conda deactivate即退出环境删除虚拟环境输入命令conda remove -n pytorch1.7 --all即可删除虚拟环境。5.使用pip导出conda虚拟环境中的包piplist--formatfreezerequirements.txt6.根据requirements.txt 安装包pip install-r requirements.txt#临时换源pip install-i https://pypi.tuna.tsinghua.edu.cn/simple-r requirements.txt7.pip 查看第三方库有哪些版本pip index versions numpy8.conda 导出环境配置yml文件运行以下代码condaenvexportenvironment.yml在任意环境下我是在 base 环境运行如下代码:condaenvcreate-fenvironment.yml会创建一个和你导出环境一模一样的虚拟环境。9.把虚拟环境加入Jupyter lab和Jupyter notebook:(1)激活虚拟环境conda activate yolov5(2)安装ipykernelcondainstallipykernel(3)注册进jupyter labpython-mipykernelinstall--user--nameyolov5 --display-namePython(yolov5)(4)删除虚拟环境rm-r/home/daiwenbin/.local/share/jupyter/kernels/yolov510.当pip安装包非常慢的时候方法1访问https://pypi.org/搜索想要的包即可可以下载tar.gz源文件然后进行安装pip install ./tensorrt_cu12-10.14.1.48.post1.tar.gz我这里下载的是tensorrt_cu12-10.14.1.48.post1.tar.gz包。方法2前提是你先前已经安装过某个包这里我以tensort_cu12举例先前我在我的虚拟环境中安装了这个包现在要想得到这个包的whl文件执行如下操作单个包使用pip download tensorrt_cu12 -d ./offline_packages多个包使用首先步骤6导出requirements.txt文件pip download -r requirements.txt -d ./offline_packages可以得到whl文件