python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > 多版本Python共享Poetry

Windows下多版本Python共享Poetry测试安装

作者:qbit

这篇文章主要为大家介绍了Windows下多版本Python共享Poetry的测试安装配置,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

前言

技术栈

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

以上就是Windows下多版本Python共享Poetry测试安装的详细内容,更多关于Windows多版本Python共享Poetry的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文