Har*_*oli 22 python python-poetry
根据诗歌的 docs,设置新项目的正确方法是 with poetry new poetry-demo,但是这会通过创建以下 toml 文件创建基于现已弃用的 python2.7 的项目:
[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"
Run Code Online (Sandbox Code Playgroud)
如何将其更新到 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.
Run Code Online (Sandbox Code Playgroud)
Nae*_*Nae 72
使用:
poetry env use 3.9
Run Code Online (Sandbox Code Playgroud)
或者
poetry env use $(which python3.9)
Run Code Online (Sandbox Code Playgroud)
由于某种原因不适用于 python 3.9。尽管在文件中明确说明了版本要求pyproject.toml,但遗憾的是需要手动强制执行这一点。
fin*_*mer 23
每当您手动更改依赖项时,pyproject.toml您都必须注意以下几点:
poetry lock之后运行或删除poetry.lock文件以强制重新创建它。造成这种情况的原因是,poetry install取poetry.lock作为输入,如果能找到一个而不是pyproject.toml。
如果您更改 python 版本并使用项目内 virtualenv,请删除.venv运行前的poetry install. 诗歌一旦创建就不会更改 venv 的 python 版本,因为它使用 python 版本本身来创建 virtualenv。
Har*_*oli 12
有趣的是,由于该工具本身所依赖的软件包丢失,诗歌正在默默地失败,并继续安装损坏的 venv。以下是修复方法。
sudo apt install python3-venv
poetry env remove python3
poetry install
Run Code Online (Sandbox Code Playgroud)
我必须删除 pytest,然后使用poetry add pytest.
编辑:当将项目从 python3.7 升级到 python3.8时,我再次遇到了这个问题 - 为此而不是安装python3-venv,你需要安装python3.8-venv
小智 10
我有同样的问题。我通过修复文件中的第一行/home/nordman/.poetry/bin/poetry(nordman是我的本地名称)来解决它。
只需更改#!/usr/bin/env python为#!/usr/bin/env python3
我尝试了我能找到的所有解决方案,但无法让诗拿起Python的升级版本。终于找到了有用的东西。
\n太棒了;
\npoetry config virtualenvs.in-project true以使用本地 venvsdeactivate以退出任何现有的 venvpoetry shell以激活新的本地 venv这就是我所看到的:
\n(my-project) ~/I/my-project \xe2\x9d\xaf\xe2\x9d\xaf\xe2\x9d\xaf poetry env info\n\nVirtualenv\nPython: 3.9.6\nImplementation: CPython\nPath: /Users/my-user/.local/share/virtualenvs/my-project-some-hash\nValid: True\n\nSystem\nPlatform: darwin\nOS: posix\nPython: /usr/local/opt/python@3.9/Frameworks/Python.framework/Versions/3.9\n\nRun Code Online (Sandbox Code Playgroud)\n我尝试运行poetry env remove python并出现错误:
\n(my-project) ~/I/my-project \xe2\x9d\xaf\xe2\x9d\xaf\xe2\x9d\xaf poetry env remove python\n\n ValueError\n\n Environment "my-project-some-hash-py3.9" does not exist.\n\nRun Code Online (Sandbox Code Playgroud)\n同时,我在某处看到建议通过设置此属性来使用本地虚拟环境,所以我这样做了:
\npoetry config virtualenvs.in-project true
这并没有解决我的问题,但后来我意识到更改此设置不会自动生效,因为我已经在另一个非本地虚拟环境中(参见poetry env info上面的输出)。
因此,根据文档,我跑去deactivate激活当前的虚拟环境。
现在,我看到了这个:
\n~/I/my-project \xe2\x9d\xaf\xe2\x9d\xaf\xe2\x9d\xaf poetry env info\n\nVirtualenv\nPython: 3.10.1\nImplementation: CPython\nPath: NA\n\nSystem\nPlatform: darwin\nOS: posix\nPython: /Users/my-user/.pyenv/versions/3.10.1\n\nRun Code Online (Sandbox Code Playgroud)\n现在我运行poetry shell创建一个新的虚拟环境,它选择了新的本地 venvs 设置:
~/I/my-project \xe2\x9d\xaf\xe2\x9d\xaf\xe2\x9d\xaf poetry shell\nCreating virtualenv my-project in /Users/my-user/projects/my-project/.venv\nSpawning shell within /Users/my-user/projects/my-project/.venv\nRun Code Online (Sandbox Code Playgroud)\n终于,我看到了我期待的升级后的Python版本!
\n(.venv) ~/I/my-project \xe2\x9d\xaf\xe2\x9d\xaf\xe2\x9d\xaf poetry run python -V\nPython 3.10.1\nRun Code Online (Sandbox Code Playgroud)\n更新:完成上述所有操作后,我发现poetry env use效果很好!
(.venv) ~/I/my-project \xe2\x9d\xaf\xe2\x9d\xaf\xe2\x9d\xaf python -V\nPython 3.10.1\n(.venv) ~/I/my-project \xe2\x9d\xaf\xe2\x9d\xaf\xe2\x9d\xaf poetry env use 3.9.6\nRecreating virtualenv my-project in /Users/my-user/projects/my-project/.venv\nUsing virtualenv: /Users/my-user/projects/my-project/.venv\n(.venv) ~/I/my-project \xe2\x9d\xaf\xe2\x9d\xaf\xe2\x9d\xaf python -V\nPython 3.9.6\n\nRun Code Online (Sandbox Code Playgroud)\n
Poetry 使使用不同的 Python 版本或虚拟环境变得非常容易。根据Poetry 文档指定 Python 版本的推荐方法是
poetry env use /path/to/preferred/python/version
Run Code Online (Sandbox Code Playgroud)
您可以通过运行获取 Python 版本的路径
which python3.7
Run Code Online (Sandbox Code Playgroud)