conda
anaconda
-
安装 anaconda
-
利用
anaconda低版本Python,例如Python 3.6.13Terminaltriangle@LEARN:~$ conda create --name 新环境名 python=版本号triangle@LEARN:~$ conda activate 环境名 // 切换python版本triangle@LEARN:~$ conda deactivate // 退出当前Python环境,回到默认环境triangle@LEARN:~$ conda info --envs // 查看虚拟环境triangle@LEARN:~$ conda remove -n 环境名 --all // 删除虚拟环境 -
将安装的Python添加到
jupyter中 (base 不需要安装,自带), 一定要切换回基础环境,再向 jupyter 中添加内核Terminal(base) triangle@LEARN:~$ conda activate python36 // 切换到新环境(python36) triangle@LEARN:~$ pip install ipykernel // 安装该版本的内核(python36) triangle@LEARN:~$ conda deactivate // 注意:一定要切换换回基础环境(base) triangle@LEARN:~$ python -m ipykernel install --name python36 // 将新环境的内核添加到 jupyter 中(base) triangle@LEARN:~$ jupyter kernelspec list // 查看添加的内核Available kernels:python3 D:\Program\anaconda3\share\jupyter\kernels\python3python36 C:\ProgramData\jupyter\kernels\python36(base) triangle@LEARN:~$ jupyter kernelspec remove 内核名 // 移除内核 -
在指定
Python版本的环境中安装OpenCV。opencv-contrib-python直接用最新版,SIFT 的专利已经过期,用之前的3.4.1版本,可能有问题。opencv-python也可以直接用最新版Terminaltriangle@LEARN:~$ pip install opencv-python opencv-contrib-python // 直接装最新的triangle@LEARN:~$ pip install opencv-python==版本号 opencv-contrib-python==版本号 // 选择版本 -
替换源头
除了命令行可以修改源,也可以直接改配置文件
- window 路径:
C:\Users\[主机名]\.condarc- linux 路径 :
~/.condarc
channels:
- defaults
- conda-forge
show_channel_urls: true
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- 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
pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
deepmodeling: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/miniforge
-
安装 miniforge
-
在
windows系统中初始化
- 在
linux系统中初始化
- 修改源,同
anaconda配置
windows:.\miniforge3\.condarc与C:\Users\[主机名]\.condarclinux:~/.condarc与/etc/conda/.condarc
show_channel_urls: true
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- 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
pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
deepmodeling: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/mamba
Tip
miniforge 自带 mamba ,无需安装
mamba 是 conda 的下载加速版,用法基本同 conda。
虚拟环境
概念
conda
├── python 3.8
│ ├── venv 1
│ └── venv 2
└── python 3.10
├── venv 1
└── venv 2conda 用于管理多个不同版本的 pyhton 环境,而「虚拟环境」则是在同一 python 版本下,「项目工程本地」搭建不同的 python 环境。
venv
pyproject.toml
介绍
在传统的 Python 项目中,我们往往需要维护多个配置文件:
setup.py用于项目打包、配置项目元数据requirements.txt管理依赖- 各种工具的配置文件(.pylintrc、pytest.ini 等)
而且不同的打包工具 setup.py 配置也可能存在差异。因此,PEP 518 定义了 pyproject.toml 文件的基本结构和构建系统规范,解决 Python 项目构建时的依赖问题,让构建过程变得更加可靠。
配置
# 项目元信息
[project]
name = "demo" # 需要是当前工程文件夹中的包名或脚本名
version = "0.1.0"
description = ""
authors = [{ name = "triangle" }]
license = { text = "MIT" }
readme = "README.md"
requires-python = ">=3.10" # python 版本要求
dependencies = [ # 正式环境依赖
"requests>=2.25.0",
"numpy>=1.20.0",
]
optional-dependencies.test = [ # 测试框架附加依赖
"pytest>=6.0.0",
"pytest-cov>=2.0.0",
]
optional-dependencies.dev = [ # 开发环境附加依赖
"black>=21.0",
"flake8>=3.9.0",
]
# 项目 url
[project.urls]
Homepage = "https://example.com"
Repository = "https://github.com/example/your-package"
# 项目相关的快捷命令,项目 instal 成功后才可执行
[project.scripts]
hello = 'cli.hello:hello' # 运行 cli/hello.py 中的 hello 函数
# 设置包管理构建系统,例如 poetry、setuptools 等
[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"
# 拓展的工具配置,例如配置 poetry、setuptools、black、isort、pytest 等
[tool.xxx]
xxx = 'xx'poetry
介绍
但是 python 官方只干了一半的活,只定了协议,未提供工具,因此,诞生了 poetry
。使用 poetry 可以非常方便的管理「虚拟环境」与 pyproject.toml。
虚拟环境
poetry 除了能进行包管理外,还可以配置虚拟环境
若当前环境处于 conda 的虚拟环境下时,会直接使用 conda 的虚拟环境,不会再创建 .venv 虚拟环境
Note
解决方案
- 使用
conda安装目标版本的 python (poetry python install也能装,但是poetry不能完全替换源,会使用默认的pypi.org) conda conda info --envs查看 python 的安装路径poetry env use [python]根据 conda 安装的 python 创建虚拟环境
工具配置
[tool.poetry]
package-mode = false # 当前项目非 .whl 包开发,只使用 poetry 的包管理能力
# 配置 poetry 的代理源,使用 'poetry.exe source' 自动管理
[[tool.poetry.source]]
name = "aliyun"
url = "xxxx"
priority = "primary"
default = true # 无法安装时才设置。使 poetry 的默认回调行为都使用上述源其他配置见 Doc
依赖管理
poetry 区分 dev 与 test 依赖,是以 group 的形式实现,即 dev 与 test 是两个依赖组,而默认组则是「运行环境依赖」。此外,根据需要也可以自定义 group。
# poetry.exe add aiohttp
[project]
dependencies = [
"aiohttp (>=3.11.18,<4.0.0)"
]
# poetry.exe add numpy --group test
[tool.poetry.group.test.dependencies]
numpy = "^2.2.5"
# poetry.exe add pandas --group dev
[tool.poetry.group.dev.dependencies]
pandas = "^2.2.3"poetry.lock
poetry.lock 锁定文件存在以下作用:
- 记录确切的版本号,保证每个人安装的库版本都一样
- 库依赖关系的准确记录
- 可通过锁定文件检查
pyproject.toml的正确性 - 可用于
git、svn版本控制,防止开发人员的本地依赖库版本不统一
在执行 poetry add,poetry remove,poetry update 命令后,会自动更新 poetry.lock。但有时会直接修改 pyproject.toml,这就导致 pyproject.toml 与 poetry.lock 不一致,这种情况下可以手动更新
插件
在新版的 poetry 中,一些指令是通过插件的形式提供,例如 export、shell等
# 该配置已经无效
[tool.poetry.requires-plugins]
poetry-plugin-export = ">=1.8"
poetry-plugin-shell = ">=1.0"但是插件无法通过 poetry install 自动安装
若想实现 npm run [command] 的效果,需要借助脚本实现
# pyproject.toml 中配置
[tool.taskipy.tasks]
test_echo = "echo 'test'"pdm
介绍
pdm
与 poetry 一样,通过 pyproject.toml 进行项目管理。pdm 的设计大部分功能都借鉴了 poetry ,且对 poetry 的不足之处进行了优化,使用更加简便,但目前 pdm 用户量没有 poetry 多,属于战未来的产品。pdm 的功能最接近 npm
虚拟环境
pdm 可以启用 PEP 582 来创建隔离的开发环境,但是 PEP 582 规则被拒绝了。因此,还是推荐使用虚拟环境的方式来创建隔离开发环境,pdm 支持以下方式
virtualenv: 默认方式,是最早诞生的虚拟环境工具,兼容性好venv: 在python 3.3后集成的默认虚拟环境创建工具,推荐condauv
配置
配置文件分三种类型,从优先级从高到低为
<PROJECT_ROOT>/pdm.toml: 项目本地配置<CONFIG_ROOT>/config.toml: pdm 全局配置<SITE_CONFIG_ROOT>/config.toml: pdm 工具自带的默认配置,不可修改
<PROJECT_ROOT>/pdm.toml
# 'pdm config --local' 配置
[pypi]
url = 'xxx'
verify_ssl = falsepyproject.toml: 除了在pdm.toml可以对pdm进行配置,在pyproject.toml中也能配置
# 源配置
[[tool.pdm.source]]
name = "private"
url = "https://private.pypi.org/simple"
# 为 'pdm commands' 添加默认的 'options'
[tool.pdm.options]
add = ["--no-isolation", "--no-self"]
install = ["--no-self"]
lock = ["--no-cross-platform"]
# 在锁定文件中排除 'requests' 包的校验
[tool.pdm.resolution]
excludes = ["requests"]依赖管理
pdm 也可以按照使用类型对包进行分组管理group:运行依赖包、dev 开发依赖包、test 测试依赖包以及其他自定义分组。
构建
在 pdm 中并未强制要求用哪个构建工具,可根据 build
,在 pyproject.toml 中配置自行配置
[build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"脚本
pdm 在 pyproject.toml 中支持多种类型的脚本配置
[tool.pdm.scripts]
commond = "flask run -p 54321"
cmd = {cmd = "flask run -p 54321"}
shell = {shell = "cat error.log|grep CRITICAL > critical.log"}
call = {call = "foo_package.bar_module:main"}除了基本的脚本配置外,pdm 也支持一些复杂的脚本配置
- 组合命令
[tool.pdm.scripts]
lint = "flake8"
test = "pytest"
all = {composite = ["lint mypackage/", "test -v tests/"]}
# 简化写法
mytask.composite = [
"echo 'Hello'",
"echo 'World'"
]- 命令环境配置
[tool.pdm.scripts]
start.cmd = "flask run -p 54321" # start 执行的命令
start.env = {FOO = "bar", FLASK_DEBUG = "1"} # start 命令的环境变量
start.working_dir = "subdir" # 工作目录- 命令传参
[tool.pdm.scripts]
cmd = "echo 'xxx {args} xxx'" # {args} 会展开传入的参数
test = "echo 'xxx {args:--default} xxx'" # {args: xxx} 指定默认参数- 前置/后置命令
[tool.pdm.scripts]
pre_compress = "{{ Run BEFORE the `compress` script }}" # 前置 pre_xxxx
compress = "tar czvf compressed.tar.gz data/"
post_compress = "{{ Run AFTER the `compress` script }}" # 后置 post_xxxx锁定文件
当进行包安装操作时,会生成 pdm.lock 锁定文件
- 保证在所有机器上,项目的包都一样
- 便于版本控制,多人协同开发
uv
Note
功能相对简单,但使用较多
介绍
uv
可以替代 pip、virtualenv、pip-tools 等工具,提供依赖管理、虚拟环境创建、Python 版本管理等一站式服务,且基于 pyproject.toml 进行项目管理。
Tip
uv 除了通过 pip 进行安装外,还可以从 github
下载安装包,独立安装
虚拟环境
项目管理
配置
Tip
支持的配置项
- 方式一:
uv.toml
# 源配置
[[index]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
default = true- 方式二:
pyproject.toml,配置项使用tool.uv.前缀
[[tool.uv.index]]
name = "pytorch"
url = "https://download.pytorch.org/whl/cu121"
explicit = true
[tool.uv.sources]
torch = { index = "pytorch" }