Why do we delete virtual environments?
There are a number of reasons that you may want to delete a virtual environment:
You no longer work on a project so you want to delete the project files and its associated virtual environment.
When you created the virtual environment, you did so with a typo in its name; you want to recreate it with the correct name.
You are having problems running your application, for whatever reason. To rule out the problem being associated with your virtual environment, you want to quickly rebuild it from scratch.
Whatever reason you have for wanting to delete a virtual environment, you’ll be able to do it in no time! 😎
Delete a virtual environment
To delete a virtual environment, simply remove the virtual environment directory:
~ → cd projects/demo-app ~/projects/demo-app → ls demo.py env ~/projects/demo-app → rm -r env/ ~/projects/demo-app → ls demo.py ~/projects/demo-app →
For example, if you create a virtual environment with a typo in the name you can quickly delete it and recreate it with the correct name:
~/projects/demo-app → ls demo.py ~/projects/demo-app → python -m venv envvv ~/projects/demo-app → ls demo.py envvv ~/projects/demo-app → rm -r envvv/ ~/projects/demo-app → python -m venv env ~/projects/demo-app → ls demo.py env
It is worth noting that it is theoretically possible to delete a virtual environment while it is activated. This may cause some confusion, because the env
that is prepended to your command prompt may stay in place. If this happens, type deactivate and the env
that is prepended to your command prompt will be removed:
(env) ~/projects/demo-app → ls demo.py env (env) ~/projects/demo-app → rm -r env/ (env) ~/projects/demo-app → deactivate ~/projects/demo-app →
Let’s Recap!
You are now able to….
Remove virtual environments using
rm
, as with other files or directories.Recognize when a virtual environment looks like it has been activated, but has in fact been deleted.
Congratulations, you have now completed part 2 of the course on creating a Python virtual environment with venv. Now let’s check your understanding with a quiz before moving on to IDEs, and PyCharm specifically.