Python包管理 Conda
更换国内源
参考:https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/
编辑文件~/.condarc,内容如下:
channels:
- defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
- 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/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
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
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
运行 conda clean -i 清除索引缓存,保证用的是镜像站提供的索引。
然后运行 conda install 即可,如:
conda install pandas lightgbm scikit-learn=0.21.3
conda install -c conda-forge xgboost=0.90
环境管理
1. 创建环境
conda create -n foo
conda activate foo
2. 删除环境
conda remove -n foo --all
3. 迁移环境
当需要将一个conda环境迁移到另外一台计算机上运行时,可以使用conda-pack工具。
在源计算机上安装conda-pack
$ conda install conda-pack
在源计算机上利用conda-pack打包一个环境
$ conda pack -n my_env -o out_name.tar.gz
即:将环境 my_env 打包成 out_name.tar.gz 文件
在目标计算机上恢复环境
$ mkdir -p my_env
$ tar -xzf my_env.tar.gz -C my_env
# Activate the environment. This adds `my_env/bin` to your path
$ source my_env/bin/activate
# Run python from in the environment
(my_env) $ python --version
# Cleanup prefixes from in the active environment.
# Note that this command can also be run without activating the environment
# as long as some version of python is already installed on the machine.
(my_env) $ conda-unpack
注意事项: conda-pack 只能迁移符合规范(约定)的包,而对于一些不符合规范的包(比如:cuda-toolkit 在env_root/pkgs 下安装了好多文件)则仍需要手动迁移数据。
安装包
查找(tensorflow)
$ conda search tensorflow
Loading channels: done
# Name Version Build Channel
...
tensorflow 2.2.0 eigen_py36h84d285f_0 pkgs/main
tensorflow 2.2.0 eigen_py37h1b16bb3_0 pkgs/main
tensorflow 2.2.0 eigen_py38hfc6e53c_0 pkgs/main
tensorflow 2.2.0 gpu_py36hf933387_0 pkgs/main
tensorflow 2.2.0 gpu_py37h1a511ff_0 pkgs/main
tensorflow 2.2.0 gpu_py38hb782248_0 pkgs/main
tensorflow 2.2.0 mkl_py36h5a57954_0 pkgs/main
tensorflow 2.2.0 mkl_py37h6e9ce2d_0 pkgs/main
...
安装指定build包(tensorflow-gpu + nccl)
$ conda install tensorflow=2.2.0=gpu_py36hf933387_0 nccl
...
发布自定义包到conda-forge
conda-forge中的包都通过github维护,发布自定义包前必须申请github账户(略)。
1.初次发布
基本步骤:
将 https://github.com/conda-forge/staged-recipes 克隆到自己的仓库中
在自己克隆的 staged-recipes 仓库中从 main 分支创建一个分支。将目录 recipes/example 复制为 recipes/<自定义目录>。
修改文件 recipes/<自定义目录>/meta.yaml,根据其中的指定信息指定自己安装包属性。比如HyperGBM初次发布时的meta.yaml文件内容如下:
{% set name = "HyperGBM" %}
{% set version = "0.2.3.2" %}
package:
name: {{ name|lower }}
version: {{ version }}
source:
url: https://github.com/DataCanvasIO/{{ name }}/archive/refs/tags/{{ version }}.tar.gz
sha256: a2a6509ecb86c38279183cf747d1e14aa8012319dd91788b81d17ce876bfa19a
build:
noarch: python
number: 0
script: "{{ PYTHON }} -m pip install . -vv"
entry_points:
- hypergbm = hypergbm.utils.tool:main
requirements:
host:
- python >=3.6
- pip
run:
- python >=3.6
- numpy >=1.16.5
- pandas >=0.25.3
- scikit-learn >=0.22.1,<1.0.0
- featuretools >=0.23.0,<1.0.0
- imbalanced-learn >=0.7.0
- lightgbm >=3.2.0
- xgboost >=1.3.0
- py-xgboost >=1.3.0
- catboost >=0.26
- fsspec >=0.8.0
- hypernets =0.2.3.2
- dask
- distributed
- dask-ml
- tqdm
- plotly
- graphviz
- python-graphviz
- shap
- pytest
test:
imports:
- hypergbm
requires:
- pip
commands:
- pip check # [not win]
- hypergbm --help # [not win]
- python -m hypergbm.utils.tool --help # [win]
about:
home: https://github.com/DataCanvasIO/HyperGBM
license: Apache-2.0
license_file: LICENSE
summary: 'A full pipeline AutoML tool integrated various GBM models.'
description: |
...
doc_url: https://hypergbm.readthedocs.io/
dev_url: https://github.com/DataCanvasIO/HyperGBM
extra:
recipe-maintainers:
- lixfz
将提交自己的分支到github,发起一个merge到 conda-forge/staged-recipes的 PullRequest,在其中完成包发布:
PR会触发CI,根据meta.yaml的内容分别在Linux/MacOS/Windows上进行自定义包的build过程,如果build失败,修改meta.yaml,反复进行,直至build成功。
build成功后,在comment中通过@管理人员申请审查。初次发布包的人一般无权直接@管理人员,可借助conda-forge的辅助功能进行:在comment中输入”@conda-forge-admin, please ping conda-forge/help-python”。
等待管理人员进行审查,如果没有问题的话,管理人员会merge你的PR,发布完成。大概率的,管理员会发现一些问题并提出修改要求,按照其提出的意见进行修改即可。
发布完成后就可以使用conda安装了(大概1-2小时的延迟)。 国内镜像同步conda-forge的频率较低,所以初期使用需要暂时禁用国内镜像。
参考文档:https://conda-forge.org/docs/maintainer/adding_pkgs.html
2.升级安装包
安装包发布完成后,会自动在conda-forge中创建一个新的仓库(feedstock),路径:https://github.com/conda-forge/<包名>-feedstock
,其中的README.md中有升级包的说明,参照执行即可。