Open-Source Internship opportunity by OpenGenus for programmers. Apply now.
Reading time: 20 minutes
In this post, we will go through the all commands to master the use of virtual environments using conda/ Anaconda. This is a cheatsheet for Anaconda with virtual environment.
Anaconda is a widely used open-source distribution of Python and R programming languages speciafically designed for applications in Machine Learning, Data Processing and others. In it, the packages are managed through conda (alternative to pip).
Virtual environment is an wrapper/ environment which will have its own copies of packages and environment variables and is useful when working with different versions of the same software package on the same system.
Install Anaconda
1. Windows Users
Anaconda is just like you normal application. Head over to www.anaconda.com/distribution/ and download the graphical installer. Double click on the installer and follow the instructions
2. Linux/Mac users
For Linux or Mac, follow these steps in your command line:
cd ~
wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh
bash Anaconda3-5.0.1-Linux-x86_64.sh -b -p ~/anaconda
rm Anaconda3-5.0.1-Linux-x86_64.sh
echo 'export PATH="~/anaconda/bin:$PATH"' >> ~/.bashrc
source .bashrc
conda update conda
And, it's done!
Check conda is installed and in your PATH
Open a terminal client.
Enter conda -V into the terminal command line and press enter.
If conda is installed you should see the version of the conda installed.
conda -V
Output:
conda 3.7.0
If this works, you are good to proceed further or else go through the installation steps again.
Check conda is up to date
In the terminal client enter
$ conda update conda
Update any packages if necessary by typing y
to proceed.
Create a virtual environment for your project
Following is the command to create a virtual environment named opengenusapp using python version 3.6:
$ conda create -n opengenusapp python=3.6 anaconda
You can use a different name and any Python version available. To check Python version available, use the following command:
conda search "^python$"
You can even skip add Python while creating the environment and once in the environment, you can install it.
conda create -n opengenusapp
It will install the Python version and all the associated anaconda packaged libraries at
“path_to_your_anaconda_location/anaconda/envs/opengenusapp”
Activate your virtual environment.
To go into your new virtual environment, use the following command:
$ source activate opengenusapp
On activating a conda environment, it will modify the PATH and shell variables to point to the specific isolated Python set-up you created.
Command line prompt may change as well by including the name of the virtual environment you are currently in.
List existing virtual environments
To see a list of all your environments, use the command:
conda env list
or
conda info -e
Install additional Python packages to a virtual environment.
To install additional packages, use the following command:
$ conda install -n opengenusapp [package]
It will install the specified package in the mentioned virtual environment "opengenusapp"
Deactivate your virtual environment
To go out of the current virtual environment, use the following command:
source deactivate
Delete a no longer needed virtual environment
To delete a virtual environment, use the following command:
conda remove -n opengenusapp -all
See list of installed packages
To check the installed packages in the current virtual environment, use the following command:
conda list
To check the list of installed packages in a deactivated virtual environment, use the following command:
conda list -n myenv
Rename/ copy a virtual environment
To copy an exisiting virtual environment, use the following command:
conda create --name new_name --clone old_name
Following it, we may delete the old virtual environment:
conda remove --name old_name --all
Remove Anaconda
To remove Anaconda completely from your system, use the following command:
rm -rf ~/anaconda