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!
While in the problematic project directory, run the following command:
poetry env use $(poetry run --quiet -- bash -c 'readlink -f $(which python)')
envs.toml
configuration file with the correct virtual environment.
Then, install all dependencies again:
poetry install
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: