Set up virtual environment for Python using Anaconda
If you are dealing with the problem of setting up an environment in anaconda and don’t have any idea why do we have to deal with the pain of setting up the environment then this is the right place for you.
Anaconda is an open source software that contains Jupyter, spyder, etc that are used for large data processing, data analytics, heavy scientific computing. Anaconda works for R and Python programming language. Package versions are managed by the package management system conda.
Installing Anaconda :
Head over to anaconda.com and install the latest version of Anaconda. Make sure to download the “Python 3.7 Version” for the appropriate architecture.
Why do we need to set up a virtual environment ?
Like many other languages Python requires a different version for different kind of applications. The application needs to run on a specific version of the language because it requires a certain dependency that is present in older versions but changes in newer versions. Virtual environments makes it easy to ideally separate different applications and avoid problems with different dependencies. Using virtual environment we can switch between both applications easily and get them running.
There are multiple ways of creating an environment using virtualenv, venv and conda. Conda command is preferred interface for managing intstallations and virtual environments with the Anaconda Python distribution.
Let’s go through the steps of creating a virtual environment using conda interface:
Step 1: Check if conda is installed in your path.
- Open up the anaconda command prompt.
- Type conda -V and press enter.
- If the conda is successfully installed in your system you should see a similar output.
Step 2: Update the conda environment
- Enter the following in the anaconda prompt.
conda update conda
Step 3: Set up the virtual environment
- Type conda search “^python$” to see the list of available python versions.
- Now replace the envname with the name you want to give to your virtual environment and replace x.x with the python version you want to use.
conda create -n envname python=x.x anaconda
Let’s create a virtual environment name Geeks for Python3.6
Step 4: Activating the virtual environment
- To see the list of all the available environments use command conda info -e
- To activate the virtual environment, enter the given command and replace your given environment name with envname
conda activate envname
When conda environment is activated it modifies the PATH and shell variables points specifically to the isolated Python set- up you created.
Step 5: Installation of required packages to the virtual environment
- Type the following command to install the additional packages to the environment and replace envname with the name of your environment.
conda install -n yourenvname package
Step 6: Deactivating the virtual environment
- To come out of the particular environment type the following command. The settings of the environment will remain as it is.
Step 7: Deletion of virtual environment
- If you no longer require a virtual environment. Delete it using the following command and replace your environment name with envname
conda remove -n envname -all
Last Updated on November 9, 2021 by admin