Poetry 支持 根据环境动态切换镜像源,可以通过以下两种方式实现国内外镜像源自动切换:
方案 1:使用环境变量 + 脚本控制
1. 配置多源
# 添加阿里云镜像(默认)
poetry source add aliyun https://mirrors.aliyun.com/pypi/simple/ --priority=default
# 添加AWS镜像(备用)
poetry source add aws https://pypi.org/simple/ --priority=supplemental
2. 创建切换脚本
#!/bin/bash
# switch_source.sh
if [ "$REGION" = "CN" ]; then
poetry source remove aws
poetry source add aliyun https://mirrors.aliyun.com/pypi/simple/ --priority=default
else
poetry source remove aliyun
poetry source add aws https://pypi.org/simple/ --priority=default
fi
运行方式:
# 国内环境
export REGION=CN && ./switch_source.sh
# 国外环境
export REGION=US && ./switch_source.sh
方案 2:利用 pyproject.toml 条件配置
1. 在 pyproject.toml 中定义源
# 基础源配置
[[tool.poetry.source]]
name = "default"
url = "https://pypi.org/simple/"
priority = "primary"
# 国内覆盖配置(通过环境变量触发)
[tool.poetry-override.source]
url = { val = "https://mirrors.aliyun.com/pypi/simple/", if = "env('REGION') == 'CN'" }
2. 通过环境变量切换
# 国内环境
export REGION=CN
poetry install # 自动使用阿里云源
# 国外环境
unset REGION
poetry install # 自动回退到AWS源
方案 3:使用 pre-commit 钩子自动切换
1. 创建 .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: switch-pypi-source
name: Switch PyPI source by region
entry: |
if [ "$(curl -s ifconfig.me/country-iso)" = "CN" ]; then
poetry source remove aws
poetry source add aliyun https://mirrors.aliyun.com/pypi/simple/ --priority=default
fi
language: system
always_run: true
2. 安装钩子
pre-commit install
此后每次 git commit 时会自动检测地理位置并切换源。
最佳实践建议
-
优先级控制:
poetry source add --priority=default aliyun https://mirrors.aliyun.com/pypi/simple/-
default>supplemental>explicit
-
-
测试源速度:
poetry debug:info | grep -A 5 "Sources" -
完整配置示例:
[[tool.poetry.source]] name = "aliyun" url = "https://mirrors.aliyun.com/pypi/simple/" priority = "default" [[tool.poetry.source]] name = "aws" url = "https://pypi.org/simple/" priority = "supplemental"
注意事项
-
认证源:如果需要私有源认证,使用:
poetry config http-basic.aliyun <username> <password> -
缓存清理:切换源后建议清理缓存:
poetry cache clear --all pypi
通过以上方法,可以灵活实现 国内外环境自动适配,兼顾速度和稳定性。
是的,Poetry 可以与 .env 文件中的环境变量结合,实现动态镜像源切换。以下是具体实现方法:
方案 1:通过 python-dotenv 自动读取 .env
1. 安装依赖
poetry add python-dotenv # 生产环境非必需可加 --dev
2. 创建 .env 文件
# .env
REGION=CN # 或 US/AWS 等
PYPI_SOURCE_CN=https://mirrors.aliyun.com/pypi/simple/
PYPI_SOURCE_GLOBAL=https://pypi.org/simple/
3. 在 Poetry 脚本中读取环境变量
创建 pypi_source.py:
# pypi_source.py
import os
from dotenv import load_dotenv
load_dotenv()
region = os.getenv("REGION", "GLOBAL")
source_url = os.getenv(f"PYPI_SOURCE_{region}")
print(f"Using PyPI source: {source_url}")
4. 通过 Hook 自动切换
在 pyproject.toml 中添加:
[tool.poetry.scripts]
switch-source = "pypi_source:main" # 运行脚本切换源
运行方式:
poetry run switch-source # 根据 .env 自动选择源
方案 2:直接集成到 pyproject.toml
1. 修改 pyproject.toml
[tool.poetry]
# ...其他配置...
[[tool.poetry.source]]
name = "dynamic-source"
url = { env = "PYPI_SOURCE", default = "https://pypi.org/simple/" }
priority = "default"
2. 在 .env 中定义源
# .env
PYPI_SOURCE=https://mirrors.aliyun.com/pypi/simple/
3. 通过 poetry-plugin-dotenv 自动加载
poetry self add poetry-plugin-dotenv
此后运行 poetry install 时会自动读取 .env。
方案 3:Shell 脚本 + 环境变量
1. 创建切换脚本 switch_pypi.sh
#!/bin/bash
# 读取 .env 中的 REGION
export $(grep -v '^#' .env | xargs)
if [ "$REGION" = "CN" ]; then
poetry source remove --all
poetry source add aliyun https://mirrors.aliyun.com/pypi/simple/ --priority=default
else
poetry source remove --all
poetry source add aws https://pypi.org/simple/ --priority=default
fi
赋予执行权限:
chmod +x switch_pypi.sh
2. 在 .env 中控制
# .env
REGION=CN
3. 使用方式
./switch_pypi.sh # 自动根据 .env 切换
关键点说明
| 功能 | 实现方式 |
|---|---|
动态加载 .env |
使用 python-dotenv 或 poetry-plugin-dotenv
|
| 条件判断 | 通过 Shell 脚本或 Python 脚本读取 REGION 等变量 |
| 优先级控制 | 在 pyproject.toml 中设置 priority = "default"
|
| 零手动干预 | 结合 CI/CD 或 Hook 自动运行 |
完整工作流示例
-
开发环境:
echo "REGION=CN" > .env poetry install # 自动用阿里云源 -
生产环境(海外):
echo "REGION=AWS" > .env poetry install # 自动用 AWS 源 -
CI/CD 集成(如 GitHub Actions):
jobs: deploy: steps: - run: echo "REGION=${{ secrets.DEPLOY_REGION }}" > .env - run: poetry install
注意事项
-
安全性:
- 不要将
.env提交到 Git(需添加到.gitignore) - 敏感信息(如密码)应使用
poetry config http-basic.<source> <user> <password>
- 不要将
-
缓存问题:
poetry cache clear pypi --all # 切换源后清理缓存 -
跨平台兼容:
- Shell 脚本需适配 Windows(或改用 Python 脚本)
通过以上方法,可实现 完全自动化 的国内外镜像源切换,无需手动干预。