9

python的环境,你再也不用愁-conda - 生活处处有BUG

 2 years ago
source link: https://www.cnblogs.com/bugs-in-life/p/16672747.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Conda Guide


Conda简介

conda是一个包,依赖和环境管理工具,适用于多种语言,如: Python, R, Scala, Java, Javascript, C/ C++, FORTRAN。

应用场景:比如在A服务器开发了一个应用,安装了N个包。现在要迁移到B服务器,又要重新安装一遍,还不知道A服务器上哪些包是必须的。conda就是解决这种问题,把该应用需要的包都安装到应用所在的环境中,迁移的时候,只要把环境导出,再导入到B环境即可。

Conda的安装

windows的安装就不演示了,直接在网上搜miniconda安装包,然后一路点下一步即可安装完成。

下边讲解linux下的安装

创建condarc.mirror文件

channels:
  - conda-forge
  - bioconda
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/main
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/r
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.bfsu.edu.cn/anaconda/cloud
  msys2: https://mirrors.bfsu.edu.cn/anaconda/cloud
  bioconda: https://mirrors.bfsu.edu.cn/anaconda/cloud
  menpo: https://mirrors.bfsu.edu.cn/anaconda/cloud
  pytorch: https://mirrors.bfsu.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.bfsu.edu.cn/anaconda/cloud
curl -L -o /tmp/miniconda.sh https://repo.anaconda.com/miniconda/Miniconda3-py39_4.11.0-Linux-x86_64.sh
/bin/bash /tmp/miniconda.sh -b -p /opt/conda
rm /tmp/miniconda.sh
conda clean -tipsy
find /opt/conda -follow -type f -name '*.a' -delete
find /opt/conda -follow -type f -name '*.pyc' -delete
conda clean -afy
cp ./condarc.mirror /root/.condarc

更新conda

conda update conda

镜像服务器

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
conda config --set show_channel_urls yes
# 执行完上述命令后,会在Users目录生成.condarc

查看所有环境

conda env list
conda create --name [name] python_or_others
ps: conda create --name FastAPI python=3.9.12
conda activate env_name
conda deactivate
conda remove -n env_name --all
conda create --clone ENVNAME --name NEWENV

package管理

列出package

conda list

列出指定环境中的所有软件包

conda list -n myenv

安装package

pip install xxxx 或者 conda install xxxx
ps:pip install tensorflow

如果不用-n指定环境名称,则被安装在当前活跃环境,也可以通过-c指定通过某个channel安装

conda install (-n python34) numpy

更新package

conda update (-n python34) numpy

卸载package

conda remove/uninstall package_name

查找package信息

conda search (-n python34) numpy

更新目前环境所有package

conda update --all

导出当前环境的package信息

conda env export > environment.yaml

删除索引缓存、锁定文件、未使用的缓存包和tarball(压缩包).

conda clean -a

环境的复制

  • 注意:yaml的方式,很消耗资源,系统配置至少要2核4G以上,且yaml的package不能过多,否则会被killed

1、导出环境

conda env export > environment.yaml

文件内容示例

name: kyle
 
channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  - https://repo.anaconda.com/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  - defaults
 
dependencies:
  - _pytorch_select=0.2=gpu_0
  - pip:
    - opencv-python==4.1.2.30
 

2、导入环境

conda env create -f environment.yaml

3、Clone环境

conda env update -n my_env --file ENV.yaml

您的关注,是我的创作动力!

公众号 @生活处处有BUG


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK