How to Build and Manage Your Virtual Environments with Anaconda

Setup Your Virtual Machine Learning Environment with Python

Gal Hever
5 min readApr 20, 2020

Introduction

In this blog-post we will discover how to create and use different virtual environments in parallel with Anaconda.

First, let’s understand what does it mean a virtual environment and then we will move on to see how it works in practice.

What is a Virtual Environment?

Each python project requires different versions of packages that can usually be set with the known requirements.txt file (a file that contains all the dependencies for a specific project). The main goal of the virtual environment is to save the dependencies and the specific versions that fit to each of our projects.

For example, if one of your projects requires:

  • Python (>= 3.5)
  • NumPy (>= 1.11.0)
  • SciPy (>= 0.17.0)

You can build a new python 3.5 virtual environment without deleting the current version that you already have and it will save those dependencies specifically for this project. Besides, it’s also really cheap to create new virtual environments and they help to keep your global directory tidy and organized by installing just the relevant packages that are necessary for your current projects.

So, basically the first thing that you should do when you start a new project is to define your virtual environment! So, let’s start!

So, Pip or Conda?

Both, pip and conda are package managers. pip is python's official package manager, which means that it contains more packages and also the most up-to-date versions compared to conde. conda that was designed specifically for Data Science domain, may contain fewer packages and older versions but it ensures that the packages do not overwrite or modify each other. So the best and safest way is to try to install packages using a conda. And please if you use pip add the python -m command before each pip command to prevent other environments to mess up your workspace.

Now, let’s see how it works in practice!

Manage Your Environments

To set up a new environment using conda (the package manager associated with anaconda platform) open the conda command line: Start-> Anaconda Prompt->Right click->“Run”.

Check Your Current Environments

First, let’s check which environments already exist in our global directory. Type the following in the conda command line:

conda env list

Or

conda info --envs

And with pip:

python -m pip list

Setup a New Virtual Environment

Type the command below in the terminal to create a new conda virtual environment that runs on python=3.6, just change “env_name” to an indicative name:

conda create --name [env_name] python=3.6

Note: You can choose different python versions regardless of the version that is already installed in your global directory.

If you want to do the same thing with pip you can use the command:

python -m venv [env_name]

Or

virtualenv [env_name]

You will be able to see that you created a new folder under your project folder for the new environment.

Note: If you used PyCharm to create your virtual environment so the folder will be named as ‘venv’.

Activate Your Environment

To start the environment you can use the following command:

conda activate [env_name]

To activate the environment with pip:

source [env_name]/bin/activate

Or

[env_name]\Scripts\activate.bat

Install/Uninstall Packages

If you want to install few packages in parallel using pip, you can do that through the requirements file as following:

python -m pip install -r requirements.txt

Or, alternatively you can use it to uninstall few packages at the same time:

pip uninstall -r requirements.txt

But, if you want to install/uninstall a specific package in your environment using conda, use the following command:

conda install --name [env_name] [package_name]

If the package does not exist in conda then install it through pip:

python -m pip install [package_name]

And if you want to install a specific version of the package:

python -m pip install [package_name]==[version]

Installed Packages in the Virtual Environment

Listing all of the installed packages and versions in the current environment:

conda list

You can also output it in a form of a configuration file that can be used with pip:

python -m python -m pip freeze

If you want to output it to a requirements file, you can use:

python -m pip freeze > requirements.txt

Deactivate Your Environment

When you finish working with your conda environment you can shut it down by:

conda deactivate

And with pip:

deactivate

Delete an Environment

If you want to delete the environment from your global directory:

conda env remove -n [env_name]

Build Virtual Environment with PyCharm

If you use PyCharm you can set up your environment through the terminal but be sure that it is connected to your project.

Go to “Settings”:

And then choose “Add”:

Here you can check that it is connected to the environment that you already built or to create a new environment.

If it’s disconnected you can activate the environment from the PyCharm terminal:

source [env_name]/bin/activate

Use your Virtual Environment in Jupyer Notebook

Activate your virtual environment and then pip install ipykernel:

Write the below command (change venv1 to your virtual environment name) and then enter to jupyter nootebook:

Now that you will open a new notebook choose the kernel that is connected to your virtual environment:

Another way to do that is to open Jupyter Notebook through the Pycharm terminal when your virtual environment is activated.

End Notes

This short practical tutorial reviewed the basic and useful commands that will help you to manage your virtual environments in python in the future. I hope that you will start using it from now on and it will help you to keep your global directory clean and tidy:)

--

--

No responses yet