Search code examples
pythonpython-poetry

Poetry clean/remove package from env after removing from toml file


I installed a package with poetry add X, and so now it shows up in the toml file and in the venv (mine's at .venv/lib/python3.10/site-packages/).

Now to remove that package, I could use poetry remove X and I know that would work properly. But sometimes, it's easier to just go into the toml file and delete the package line there. So that's what I tried by removing the line for X.

I then tried doing poetry install but that didn't do anything When I do ls .venv/lib/python3.10/site-packages/, I still see X is installed there.

I also tried poetry lock but no change with that either.

So is there some command to take the latest toml file and clean up packages from being installed that are no longer present in the toml?


Solution

  • When ever you manual edit the pyproject.toml you have to run poetry lock --no-update to sync the locked dependencies in the poetry.lock file. This is necessary because Poetry will use the resolved dependencies from the poetry.lock file on install if this file is available.

    Once the pyproject.toml and poetry.lock file are in sync run poetry install --sync to get the venv in sync with the poetry.lock file.