How to rebuild Poetry environment from scratch and force reinstall everything?

Viewed 11479

Poetry has some stale dependencies because the use of develop = true packages. Poetry cannot figure out on its own that dependencies have been updated. How do I force Poetry to reinstall everything in its virtualenv to work around this issue?

2 Answers

These instructions are only for Linux/macOS for Windows Subsystem for Linux. For the Microsoft Windows shell, please use your own command-line knowledge to apply these instructions.

Recreating Poetry environment

Do the following in the folder with 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

Recreating Poetry environment with different Python version

Poetry may refer to your installed Python version, so you might tell it to change its link to your python interpreter as well:

# Make Poetry to use python 3.9 from Homebrew, installed earlier
poetry env use `which python3.9`
poetry shell
python -V
Python 3.9.9

Kudos to this tip about removing the virtualenv.

Fixing damaged poetry command

If the poetry command itself is damaged and no longer runs, you can reinstall Poetry by:

which poetry
/Users/mikkoohtamaa/.poetry/bin/poetry

Then remove this and install:

rm -rf /Users/mikkoohtamaa/.poetry

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3 -

The official documentation recommends the poetry env remove command, which uses the following syntax:

poetry env remove /full/path/to/python
poetry env remove python3.10
poetry env remove 3.10
poetry env remove test-O3eWbxRl-py3.10

As with the accepted answer this will need to be followed by poetry shell and poetry install to recreate the environment and reinstall dependencies.

Related