How to use python virtual environment with conda

Milind Soorya / August 21, 2021

2 min read

Introduction

In this tutorial I will show you how to setup python virtual environment. This process will work even if you are a mac, windows or linux user as we will be installing it using a popular software package manager called Anaconda.

What’s a Virtual Environment?

A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system.

What’s Conda?

Conda is a popular opensource python package manager similar to pip. Conda quickly installs, runs and updates packages and their dependencies.

How to Create a Virtual Environment with Conda

  1. Check conda is installed and in your PATH if not install from conda

    $ conda -V
    
  2. Check conda is up to date

    conda update conda
    

    To see a list of available python versions first, type

    conda search "^python$"
    

    and press enter.

  3. Create a virtual environment for your project

    conda create -n yourenvname python=x.x anaconda
    
  4. Activate your virtual environment.

    # if you are using a UNIX operating sytem like ubuntu
    source activate yourenvname
    
    # if you are using windows
    conda activate yourenvname
    
  5. Install additional Python packages to a virtual environment. Failure to specify “-n yourenvname” will install the package to the root Python installation.

    conda install -n yourenvname [package]
    
  6. Deactivate your virtual environment.

    conda deactivate
    
  7. List all virtual envinment

    conda env list
    
  8. Delete a no longer needed virtual environment

    conda remove -n yourenvname -all
    

You might also like:-

Learn about building products as a Data Scientist

Get a once-per-month email with my latest article and additional details about my launches, products, and experiments ✨

No spam, sales, or ads. Unsubscribe as your heart desires.