Skip to the content.

Virtual Environment Tutorial

There are multiple ways of creating a Python virtual environment. This tutorial covers two of those:

venv creates virtual environments in the location where run (generally with Python projects).

mkvirtualenv creates virtual environments in one place (generally in your home directory).

(The Python Software Foundation recommends venv for creating virtual environments.)

Option 1: Using "venv"

macOS/Linux terminal (python3 -m venv ENV)

python3 -m venv sbase_env
source sbase_env/bin/activate

Windows CMD prompt (py -m venv ENV):

py -m venv sbase_env
call sbase_env\\Scripts\\activate

To exit a virtual env, type deactivate.


Option 2: Using virtualenvwrapper

macOS/Linux terminal:

python3 -m pip install virtualenvwrapper --force-reinstall
export WORKON_HOME=$HOME/.virtualenvs
source `which virtualenvwrapper.sh`

(Shortcut: Run source virtualenv_install.sh from the top-level SeleniumBase folder to perform the above steps.)

(If you add source `which virtualenvwrapper.sh` to your local bash file (~/.bash_profile on macOS, or ~/.bashrc on Linux), virtualenvwrapper commands such as mkvirtualenv will be available whenever you open a new command prompt.)

Windows CMD prompt:

py -m pip install virtualenvwrapper-win --force-reinstall --user

(Shortcut: Run win_virtualenv.bat from the top-level SeleniumBase folder to perform the above step.)

Create a virtual environment:

mkvirtualenv sbase_env

(If you have multiple versions of Python installed on your machine, and you want your virtual environment to use a specific Python version, add --python=PATH_TO_PYTHON_EXE to your mkvirtualenv command with the Python executable to use.)

virtualenvwrapper commands:

Creating a virtual environment:

mkvirtualenv sbase_env

Leaving your virtual environment:

deactivate

Returning to a virtual environment:

workon sbase_env

Listing all virtual environments:

workon

Deleting a virtual environment:

rmvirtualenv sbase_env

If the python and python3 versions don’t match (while in a virtualenv on macOS or Linux), the following command will sync the versions:

alias python=python3

(To remove an alias, use: unalias NAME)


To verify the python version, use:

python --version

To see the PATH of your python (macOS/Linux), use:

which python

python-guide.org/en/latest/dev/virtualenvs has more information about Python virtual environments. For specific details about VirtualEnv and VirtualEnvWrapper, see http://virtualenv.readthedocs.org/en/latest/ and http://virtualenvwrapper.readthedocs.org/en/latest/.