Skip to content

Setting Up Python Virtual Environment

alidaniell edited this page Sep 18, 2018 · 17 revisions

Setting up a virtual environment makes it easier for managing packages and runtime. A virtual environment is an isolated runtime environment where users can upgrade and install distribution packages without interfering with the behavior of other Python applications running on the system.

The two preferred ways to create virtual environments are Anaconda and Python Venv.

A good resource about using Conda is the Conda Cheat Sheet.

Using Anaconda

Linux

wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
wget https://repo.anaconda.com/archive/Anaconda3-5.2.0-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
bash Anaconda-latest-Linux-x86_64.sh

Follow the installation instructions. Once done, restart the terminal so the changes make effect.

Test your installation by running

conda list

Then you can create your virtual environment, activate the environment to work, and deactivate when done.

# to create a new environment
conda create -n myenv python=3.6

# to activate and do work
conda activate myenv

# to deactivate when done
conda deactivate myenv

Mac-OS

# to install Miniconda—In your Terminal window, run:
bash Miniconda3-latest-MacOSX-x86_64.sh

# to install Anaconda
wget https://repo.continuum.io/anaconda/Anaconda3-3.7.0-Linux-x86_64.sh -O ~/anaconda.sh
bash ~/anaconda.sh -b -p $HOME/anaconda
export PATH="$HOME/anaconda/bin:$PATH"

Follow the prompts on the installer screens. If you are unsure about any setting, accept the defaults. You can change them later.

Test your installation by running

conda list

Then you can create your virtual environment, activate the environment to work, and deactivate when done.

# to create a new environment
conda create -n myenv python=3.6

# to activate and do work
conda activate myenv

# to deactivate when done
conda deactivate myenv

Windows

Using Python Virtual Environment

Linux

Mac-OS

Windows

Download Python Venv. Follow the installation instructions to install Python Venv to the Windows environment and learn more about using it. It's assumed that you are using Python version 3.

Then you can create your virtual environment, activate the environment to work, and deactivate when done.

# to create a new environment
python -m venv <dir-name>
# to activate and do work
<dir-name>\Scripts\activate.bat

Confirm your virtual environment is set up: Your prompt will show the name of your virtual environment in parentheses, and the path to the virtual environment's Scripts directory will precede the rest of your usual path.

# to deactivate when done
<dir-name>\Scripts\deactivate.bat

Clone this wiki locally