前言
技术栈
Windows 10 Python 3.8.10 Python 3.11.2 pip 23.0.1 pipx 1.2.0 poetry 1.4.1
Python 3.8 安装目录
C:\Python38
Python 3.11 安装目录
C:\Python311
安装 poetry
设置将以下路径加入
path
环境变量C:\Python38 C:\Python38\Scripts C:\Python311 C:\Python311\Scripts C:\Users\qbit\.local\bin\
复制文件
C:\Python38\python.exe -> C:\Python38\py38.exe C:\Python311\python.exe -> C:\Python38\py311.exe
设置 pip 国内镜像源
py38 -m pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ py311 -m pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
升级 pip
py38 -m pip install pip --upgrade py311 -m pip install pip --upgrade
安装或升级 pipx
py38 -m pip install pipx --upgrade py311 -m pip install pipx --upgrade
在 Python 3.11 下用 pipx 安装 Poetry
py311 -m pipx install poetry --force -i https://mirrors.aliyun.com/pypi/simple/
查看安装情况
> where poetry c:\Users\qbit\.local\bin\poetry.exe > poetry --version Poetry (version 1.4.1)
修改 potry 缓存目录
poetry config cache-dir D:\pypoetry
设置将虚拟环境目录放在项目内
poetry config virtualenvs.in-project true
测试多版本共享
测试 Python 3.11
创建空目录
F:\tmp\test311
,在里面创建文件pyproject.toml
,文件内容如下:[tool.poetry] name = "test" version = "0.1.0" description = "" authors = ["qbit"] readme = "README.md" [[tool.poetry.source]] name = "aliyun" url = "https://mirrors.aliyun.com/pypi/simple/" default = true [tool.poetry.dependencies] python = "^3.11" requests = "~2.28.2" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api"
创建虚拟环境并安装第三方库
poetry update -vv
查看虚拟环境信息
> poetry env info Virtualenv Python: 3.11.2 Implementation: CPython Path: F:\tmp\test311\.venv Executable: F:\tmp\test311\.venv\Scripts\python.exe Valid: True System Platform: win32 OS: nt Python: 3.11.2 Path: C:\Python311 Executable: C:\Python311\python.exe
测试 Python 3.8
创建空目录
F:\tmp\test38
,在里面创建文件pyproject.toml
,文件内容如下:[tool.poetry] name = "test" version = "0.1.0" description = "" authors = ["qbit"] readme = "README.md" [[tool.poetry.source]] name = "aliyun" url = "https://mirrors.aliyun.com/pypi/simple/" default = true [tool.poetry.dependencies] python = "^3.8" requests = "~2.28.2" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api"
切换 python 版本并创建虚拟环境
poetry env use C:\Python38\python.exe
查看虚拟环境信息
> poetry env info Virtualenv Python: 3.8.10 Implementation: CPython Path: F:\tmp\test38\.venv Executable: F:\tmp\test38\.venv\Scripts\python.exe Valid: True System Platform: win32 OS: nt Python: 3.8.10 Path: C:\Python38 Executable: C:\Python38\python.exe
安装第三方库
poetry update -vv
后记
如果报类似如下错误,部分包安装不成功,可以
poetry shell
进入虚拟环境后用pip
安装报错的包_WheelFileValidationError version.py is not mentioned in RECORD In {self._zipfile.filename}, hash / size of {item.filename} didn't match RECORD
- 出现上面这个错误的原因是第三方包不够规范,使得 poetry 在校验的时候出错。可以参考 installer: do not fail on invalid wheels, print only a warning
本文出自 qbit snap
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。