Poetry: How to delete virtual env?

Created on 1 Mar 2019  路  13Comments  路  Source: python-poetry/poetry

How do I delete a virtual env? I tried just deleting the file but poetry stopped working after that.

Question candidatfaq

Most helpful comment

The latest preview version (which can be installed with get-poetry.py --preview; see the readme) supports poetry env remove, which will handle removing the virtualenv for you. remove expects one argument: the Python version to remove the virtualenv for. In my case, I'm using Python 3.7, so I usually type something like poetry env remove 3.7.

If you want to see where the virtualenv is stored, there are various ways to print it out. poetry env info, which is also new in the preview version, will print a nice summary of information about the current environment Poetry is using, including the full path to the virtualenv.

All 13 comments

The latest preview version (which can be installed with get-poetry.py --preview; see the readme) supports poetry env remove, which will handle removing the virtualenv for you. remove expects one argument: the Python version to remove the virtualenv for. In my case, I'm using Python 3.7, so I usually type something like poetry env remove 3.7.

If you want to see where the virtualenv is stored, there are various ways to print it out. poetry env info, which is also new in the preview version, will print a nice summary of information about the current environment Poetry is using, including the full path to the virtualenv.

On macOS, envs are located here ~/Library/Caches/pypoetry/virtualenvs

On Linux, the easiest way to find the virtualenv is to pop into a subshell and run which python:

$ poetry shell
$ which python

I feel like this should be easier. Maybe something like $ poetry destroy [-f] for the environment associated with the current working directory?

@arnegroskurth the poetry env remove <python> command does this.

As pointed out above you can also do rm -rf `poetry env info -p` to remoe the virtual env directory depending on what you need to do.

@abn Sure but that is more complicated and requires the user to know about the poetry env * commands. At least me (and I suspect quite a lot of other users) are only using poetry {install,update,add,remove} the vast majority of the time and are happy that everything regarding the venv is abstracted away from me.

So I feel like there should be something to just level the playing-field without me having to think about venvs again.

Sounds to me more like a deficiency in the documentation and/or onboarding guide for a new user to Poetry. That is definitely an area that we are interested in improving on.

At this time, I do not think we want to encourage the addition of multiple commands to do the same task. And, personally, adding something like poetry destroy does not really solve the issue, it is yet another command anyway that the user needs to know about. Additionally, it can also be ambiguous as it could also mean "destroy everything poetry related".

@abn I just feel like that the benefit of using poetry over venv+pip is for the user to not have to care about two separate tools for dependency-management and project-isolation anymore. And because I can create and update the environment with top-level poetry commands, I feel like that I should also be able to remove the environment with a top-level command that is much more discoverable ($ poetry help) then anything under $ poetry env.

That definitely is one of the advantages of using poetry. Although, it is important to note that explicit creation of an environment relies on poetry env use <python>. What you are referring to is the implicit creation of the environment by commands that require an existing virtual environment. All commands relating to environment managent are already grouped under env.

We appreciate your feedback on this. However, for this matter I do not see a reason to change the current behaviour at this time.

As pointed out above you can also do rm -rf `poetry env info -p` to remoe the virtual env directory depending on what you need to do.

Please note that the suggested command can be quite dangerous if accidentally run in a directory not handled by poetry.

This worked for me:

  • cd into the folder where pyproject.toml is
  • Run poetry env list (this will show you the venv for that project)
  • Then run poetry env remove whatever-WhATeVs-py3.9 to delete it

Running poetry shell after will create a new venv, after which running poetry install will install all the deps listed in pyproject.toml.

you can actually just run poetry env remove python from the project root folder and it will delete its virtualenv

addendum: just note the python parameter is your python executable name (it isn't just an argument saying "delete python in this venv"鈥攊t's saying "use this executable name to lookup the venv name").

so, if your default path python isn't your venv python (e.g. /usr/bin/python is still 2.7, but you develop using /usr/local/bin/python3), you need to run poetry env remove python3 or else you'll get yelled at for not having a py2.7 venv

the lookup is here: execute code using the python argument to find python version then python version is appended to the venv directory. looks like the code is designed to support multi-python-version venvs for the same project, which makes sense, but caught me off guard with needing to fight system python vs. usable python.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mozartilize picture mozartilize  路  3Comments

sobolevn picture sobolevn  路  3Comments

ulope picture ulope  路  3Comments

thmo picture thmo  路  3Comments

probablykasper picture probablykasper  路  3Comments