Search code examples
pythonpython-poetry

Poetry: How to keep using the old virtual environment when changing the name of the project root folder?


I am using Poetry in some of my python projects. It's not unusual that at some stage I want to rename the root folder of my project. When I do that and run poetry shell poetry creates a new virtual environment. But I don't want a new virtual environment, I just want to keep using the existing virtual environment. I know I can manually activate the old one by running source {path to the old venv}/bin/activate but then I would have to keep track of the old environment name separately and refrain from using poetry shell.

Is there something I can do about this? It's quite time consuming to start installing the dependencies again, pointing an IDE to the new environment and deleting the old virtual env, just because you have changed the root folder name - something that can happen multiple times. This question suggests that there is no solution to the problem but would want to confirm this because to me this seems quite annoying issue.


Solution

  • One option is to enable the virtualenvs.in-project option, e.g. by running

    poetry config virtualenvs.in-project true
    

    If set to true, the virtualenv wil [sic] be created and expected in a folder named .venv within the root directory of the project.

    This will cause Poetry to create new environments in $project_root/.venv/. If you rename the project directory the environment should continue to work.