This recipe demonstrates how to set up an environment that is a clone of the current Anaconda installation. One possible use of this procedure is to set up an environment when we start the development of a new project.
Creating a virtual environment for development with condaÂ
Getting ready
This recipe assumes that you have a working installation of Anaconda. If you don't, follow the recipe for installing Anaconda on your operating system presented previously in this chapter.
How to do it...
- Start by opening a Terminal window and running the following commands:
conda update conda
conda update anaconda
These commands update both conda and Anaconda to the most recent versions. This is always recommended before creating a new environment.
- Next, run the following line to create a new environment named myenv:
conda create --name myenv
- You will be asked for confirmation and conda will then create the new environment. We now need to activate the new environment. For Linux and macOS, run the following command in the terminal:
source activate myenv
- In Windows, run the following:
activate myenv
- Notice that the command line changes to reflect the active environment, as shown in the following example:
(myenv) computer:~ username
- To confirm that the new environment was created, we can execute the following command:
conda info --envs
- This command now produces the following output:
# conda environments:
#
myenv * /Users/username/anaconda/envs/myenv
root /Users/username/anaconda
Notice that the currently active environment is marked with an asterisk.
- We can now install packages in the active environment without interfering with the original Python distribution. For example, to install the csvkit package, we use code in the following example:
conda install csvkit
- When you are done working in the environment, it is necessary to deactivate it. In Linux and macOS, this is done with the following command:
source deactivate
- In Windows, the command to do so is the following:
deactivate
- If you decide you don't need the myenv environment any longer, it can be deleted with the following command:
conda remove myenv --all
Note that you cannot remove the currently active environment.