Using conda, you can create, activate, deactivate, switch, and delete virtual environments with simple and intuitive commands.
This tutorial will guide you through the basics of working with conda environments in simple and easy-to-follow steps.
Let’s get started.
Conda Create Environment
To create a virtual environment with conda, all you need is access to a terminal on a system with conda installed.
1. To create a virtual env:
Replace ‘environment_name’ with the name of your virtual environment.
Once you run the conda create command, you will be prompted whether you wish to proceed with the location of the virtual environment.
Type the letter ‘Y’ to proceed.
Conda Activate Environment
Once you have created your anaconda environment, you will need to activate it before use.
Luckily, you can use a simple command as:
The command above will initialize and activate the environment with the specified name.
In most cases, your terminal prompt will update with the prefix of your environment name when active.
Conda Show Environments
In some instances, you may have more than one virtual environment. Instead of manually browsing for their names, you can use the command:
To show the available environments and the path where they are located.
Conda Deactivate Environment
It is recommended to deactivate an environment when performing Python operations globally.
You can use the deactivate command as;
You must have at least one environment active before running the deactivate command:
Conda Delete Environment
After you are done with a specific environment, you can delete it from your system using the commands provided below:
Note that before running the delete commands, ensure the target environment is deactivated.
The command will delete the environment with the specified name.
You can also use the -n instead of the –name flag.
Conda also allows you to specify the environment path instead of the name. The command is as shown:
Or the short form:
If you want to remove the environment manually, you can delete the directory where the environment is stored.
Start by getting the path of the environment:
Once you have the path to the target environment, use the rm command:
Although removing the environment directory works, it is not recommended. It may lead to anaconda referencing an environment that does not exist.
Conclusion
This tutorial covered the basics of working with virtual environments with conda. We started with creating new environments, how to activate and deactivate them, and finally, how to delete inactive environments.
Removing conda virtual environments allows you to remove corrupted environments and save some space on your system.