将Python版本更改为3.x

94
根据 poetry 文档,设置新项目的正确方法是使用 poetry new poetry-demo。但是这会创建一个基于已停用的 python 2.7 的项目,并创建以下 toml 文件。
[tool.poetry]
name = "poetry-demo"
version = "0.1.0"
description = ""
authors = ["Harsha Goli <harshagoli@gmail.com>"]

[tool.poetry.dependencies]
python = "^2.7"

[tool.poetry.dev-dependencies]
pytest = "^4.6"

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
我该如何将它更新到3.7?只需将 python = "^2.7" 更改为 python = "^3.7" 即可。但是,运行 poetry install 后会出现以下错误:
[SolverProblemError]
The current project's Python requirement (2.7.17) is not compatible with some of the required packages Python requirement:
  - zipp requires Python >=3.6

Because no versions of pytest match >=4.6,<4.6.9 || >4.6.9,<5.0
 and pytest (4.6.9) depends on importlib-metadata (>=0.12), pytest (>=4.6,<5.0) requires importlib-metadata (>=0.12).
And because no versions of importlib-metadata match >=0.12,<1.5.0 || >1.5.0
 and importlib-metadata (1.5.0) depends on zipp (>=0.5), pytest (>=4.6,<5.0) requires zipp (>=0.5).
Because zipp (3.1.0) requires Python >=3.6
 and no versions of zipp match >=0.5,<3.1.0 || >3.1.0, zipp is forbidden.
Thus, pytest is forbidden.
So, because poetry-demo depends on pytest (^4.6), version solving failed.

创建新项目时,Poetry似乎会插入Poetry本身正在运行的Python版本。因此,您应该检查为什么在Python 2上运行poetry new poetry-demo,而不是3。 - Thomas
12个回答

0

我正在运行Ubuntu 22.04,只安装了Python 3.10。我已经多次破坏了系统Python,因此我拒绝使用旧版本或从pip运行任何安装。我想要的是一个旧版本的Python,它不会覆盖我的当前Python安装。我所做的是按照这个指南进行操作。

sudo apt install libgdbm-dev build-essential libnss3-dev libreadline-dev libffi-dev libsqlite3-dev libbz2-dev libncurses5-dev libssl-dev zlib1g-dev
cd /tmp
wget https://www.python.org/ftp/python/3.9.14/Python-3.9.14.tgz
tar -xf Python-3.9.14.tgz
cd Python-3.9.14 
./configure --enable-optimizations

## if you want to run in parallel set -j to how many cpus you have
## I subtracted 2 so my machine wouldn't have a stroke
# lscpu | egrep 'CPU\(s\)'
# make -j <cpus you are comfortable with>
make

## Super important to run altinstall to not overwrite
sudo make altinstall

## I updated the pyproject.toml to ^3.9
poetry env use /usr/local/bin/python3.9

现在正在测试,如果我在这之后没有发布任何内容,那么要么它起作用了,要么我已经不在人世了。


-2

只需编辑pyproject.toml文件,并将Python版本更改为3.7,例如:

python = "^3.7"

该问题明确表示这种方法行不通。 - bfontaine

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接