python 配置
目录
简介
记录一下 python 的配置.
安装
普通安装
- 搜索
winget search python - 安装
winget install Python.Python.3.13 - 安装 pipx
py -m pip install --user pipx - 配置 pipx
py -m pipx ensurepath
新工具安装
winget install --id=astral-sh.uv -e
配置源
- 运行完命令, 会显示配置文件路径
pip config set global.index-url http://mirrors.aliyun.com/pypi/simple/pip config set global.trusted-host mirrors.aliyun.com
效果如下
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com新工具
- Astral · GitHub
- PDM README_zh.md
- Poetry Introduction | Documentation | Poetry - Python dependency management and packaging made easy
uv
安装
- 下载二进制文件 Releases · astral-sh/uv 到
/usr/local/bin/ - 配置镜像源到
~/.bashrc
export UV_DEFAULT_INDEX=https://mirrors.aliyun.com/pypi/simple/
export UV_PYTHON_INSTALL_MIRROR=https://gh-proxy.com/https://github.com/astral-sh/python-build-standalone/releases/download使用
- 命令工具
uv tool可以用来代替pipx:- 添加
$PATH路径export PATH="/root/.local/bin:$PATH" - 安装
uv tool install ansible-core - 常用命令
uv tool upgrade/uninstall/list, 查看安装路径uv tool dir
- 安装 python
- 最新版本
uv python install uv python install 3.10特定版本uv python list查看所有的版本, 包括系统自带的
- 最新版本
- 运行脚本
uv run 1.py运行脚本
- 项目
uv init hi: 会创建项目文件夹, 锁定 python 版本uv init --python 3.10在项目里锁定特定 python 版本pyproject.toml添加镜像源[tool.uv] python-install-mirror = "https://gh-proxy.com/https://github.com/astral-sh/python-build-standalone/releases/download" [[tool.uv.index]] url = "https://mirrors.aliyun.com/pypi/simple/" default = trueuv add requests: 创建.venv环境, 锁包uv.lock项目锁定版本号,同时修改
.python-version为3.13.7,pyproject.toml的requires-python = "==3.13.7"
项目初始化
fastapi
mkdir fastapi-demo && cd fastapi-demo
uv add fastapi uvicorn
# 开发命令
import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=8000, log_level="info",reload=True)
# 生产环境
uvicorn main:app --host 0.0.0.0 --port 8080 --workers 4