如何从头开始强制重新安装Poetry环境?

Mik*_*maa 36 python-poetry

由于使用了develop = true包,Poetry 有一些陈旧的依赖关系。Poetry 无法自行判断依赖项已更新。如何强制 Poetry 重新安装其 virtualenv 中的所有内容来解决此问题?

Mik*_*maa 68

这些说明仅适用于适用于 Linux 的 Windows 子系统的 Linux/macOS。对于 Microsoft Windows shell,请使用您自己的命令行知识来应用这些说明。

再现诗歌环境

要在当前工作目录(UNIX shell)中重新安装 Poetry 环境的软件包:

# Enter the current Poetry environment
poetry shell

# Remove the current environment
# as referred by path to Python interpreter 
poetry env remove $(which python)

# Reinstall from Poetry's cache
poetry install
Run Code Online (Sandbox Code Playgroud)

手动重建 Poetry 环境

在文件夹中执行以下操作pyproject.toml

# Stop the current virtualenv if active or alternative use
# `exit` to exit from a Poetry shell session
deactivate

# Remove all the files of the current environment of the folder we are in
POETRY_LOCATION=`poetry env info -p` 
echo "Poetry is $POETRY_LOCATION"
rm -rf "$POETRY_LOCATION"

# Reactivate Poetry shell
poetry shell

# Install everything
poetry install

Run Code Online (Sandbox Code Playgroud)

使用不同的Python版本重新创建Poetry环境

Poetry 可能引用您安装的 Python 版本,因此您可能会告诉它更改其到您的解释器的链接python

# Make Poetry to use python 3.9 from Homebrew, installed earlier
poetry env use `which python3.9`
poetry shell
python -V
Run Code Online (Sandbox Code Playgroud)
Python 3.9.9
Run Code Online (Sandbox Code Playgroud)

感谢这个关于删除 virtualenv 的提示

修复损坏的诗歌命令

如果poetry命令本身损坏并且不再运行,您可以通过以下方式重新安装 Poetry:

which poetry
Run Code Online (Sandbox Code Playgroud)
/Users/mikkoohtamaa/.poetry/bin/poetry
Run Code Online (Sandbox Code Playgroud)

然后删除它并安装

# macOS
rm -rf /Users/mikkoohtamaa/.poetry
rm -rf  "/Users/$USER/Library/Application Support/pypoetry/venv"


# Linux
rm -rf ~/.local/share/pypoetry/

# Download and run Poetry install script
# If your curl is messed up you can try with --insecure flag
curl -sSL https://install.python-poetry.org/ | python3 - --force
Run Code Online (Sandbox Code Playgroud)

极度痛苦的问题

有时,在您升级 Python(例如使用 Homebrew)后,Poetry 可能无法安装。这可能与由 Poetry 决定使用哪个 Python 版本这一事实有关。

您看到的错误可能包括:

# Stop the current virtualenv if active or alternative use
# `exit` to exit from a Poetry shell session
deactivate

# Remove all the files of the current environment of the folder we are in
POETRY_LOCATION=`poetry env info -p` 
echo "Poetry is $POETRY_LOCATION"
rm -rf "$POETRY_LOCATION"

# Reactivate Poetry shell
poetry shell

# Install everything
poetry install

Run Code Online (Sandbox Code Playgroud)
dyld[6526]: Library not loaded: @loader_path/../Python
  Referenced from: <7295E559-55D4-3ACA-9820-D95D1F4AE151> /Users/moo/Library/Application Support/pypoetry/venv/bin/python3.10
  Reason: tried: '/Users/moo/Library/Application Support/pypoetry/venv/bin/../Python' (no such file), '/System/Volumes/Preboot/Cryptexes/OS@loader_path/../Python' (no such file), '/Users/moo/Library/Application Support/pypoetry/venv/bin/../Python' (no such file), '/usr/local/lib/Python' (no such file), '/usr/lib/Python' (no such file, not in dyld cache)

Traceback:

  File "<stdin>", line 923, in main
  File "<stdin>", line 562, in run
Run Code Online (Sandbox Code Playgroud)

在这种情况下,推荐的方法是放弃基于 Homebrew 的 Python 并开始使用pyenv

# Make Poetry to use python 3.9 from Homebrew, installed earlier
poetry env use `which python3.9`
poetry shell
python -V
Run Code Online (Sandbox Code Playgroud)

然后在新的 shell 中安装特定的 Python 版本并检查它是否正在使用:

Python 3.9.9
Run Code Online (Sandbox Code Playgroud)

指向 pyenv:

/Users/moo/.pyenv/shims/python
Run Code Online (Sandbox Code Playgroud)

现在使用特定的 Python 版本重新安装 Poetry,它应该可以工作:

which poetry
Run Code Online (Sandbox Code Playgroud)

  • “rm -rf `poetry env info -p` ”命令会破坏诗歌。执行后,所有诗歌命令都会失败,并显示“No pyvenv.cfg file”。 (2认同)
  • 事实上,将命令的输出传递给“rm -rf”听起来是一个糟糕的建议。 (2认同)

Dam*_*ien 19

官方文档推荐使用该poetry env remove命令,该命令接受以下任意语法:

poetry env remove /full/path/to/python
poetry env remove python3.11
poetry env remove 3.11
poetry env remove test-O3eWbxRl-py3.11
Run Code Online (Sandbox Code Playgroud)

您可以使用以下命令一次性删除所有虚拟环境poetry env remove --all


与接受的答案一样,这需要分别重新poetry shell创建poetry install环境并重新安装依赖项。