DjangoDjango

When PyCharm Deletes Poetry Virtual Environments: The Fix

Jan 25, 2024 · by Tim Kamanin

I use PyCharm as my Python IDE and Poetry for dependency and virtual environment management. However, the magnificent combination of these tools occasionally encounters a weird issue: PyCharm deletes the Poetry virtual environment when you launch the IDE. 

Though the problem occurs infrequently, when it does, it leads to a relentless cycle of creating a virtual environment, installing packages, opening PyCharm, only to find that it has deleted the virtual environment, and then needing to recreate it with Poetry. This cycle can repeat itself multiple times - you can imagine the frustration.

Today, I encountered that pesky issue. So, I googled around and found the solution!

The solution to PyCharm deleting the existing Poetry environment is as follows:

  1. While in the problematic project directory, run the following command:

    poetry env use $(poetry run --quiet -- bash -c 'readlink -f $(which python)')
    
    The command will update Poetry's envs.toml configuration file with the correct virtual environment.

  2. Then, install all dependencies again:

    poetry install
    
    And you should be good to go.

If you're interested in what's causing the issue and want to dive a bit deeper, check out the following comment on the PyCharm issue tracker.

Hey, if you've found this useful, please share the post to help other folks find it:

There's even more:

Subscribe for updates

  • via Twitter: @timonweb
  • old school RSS:
  • or evergreen email ↓ ↓ ↓