-
-
Notifications
You must be signed in to change notification settings - Fork 47
Setting Up Python Virtual Environment
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.
The instructions below are a summarized version from Anaconda Install Instructions
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
wget https://repo.continuum.io/archive/Anaconda3-2018.12-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
source activate myenv
# to deactivate when done
source deactivate
# 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
We recommend using the virtualenvwrapper library to manage your Python environments. This tool wraps the virtualenv library (virtualenv) and provides a number of convenient tools.
Note that the Python standard library provides venv. This provides similar functionality to virtualenv, but has some limitations.
You can use the the built-in library directly, but this guide will assume you are using the virtualenvwrapper library as it provides a number of convenient features.
See https://virtualenvwrapper.readthedocs.io/en/latest/install.html)
Make sure to follow the setup instructions, which include setting up an
Envs
directory and adding the virtualenvwrapper.sh
setup script to your
path. Follow these instructions carefully.
Once the installation is complete, you have access to convenient commands to create and use virtual environments.
To create a new virtual environment for this project called bostoninfo
run:
mkvirtualenv bostoninfo
This will create the virtual environment in the Envs
directory and activate
it. You should see (bostoninfo)
at the beginning of your command prompt.
Consult the command reference for more commands. The two basic commands you need to activate and deactivate virtual environments after creating them are:
-
workon [env you want to activate]
- without an argument this will list your available environments; with the name of an environment as its argument, it will activate that argument. -
deactivate
- Switch from a virtual environment to the system-installed version of Python.
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