Python Virtual Environments:


What, Why, and How



Presented by Michael Mintz

Granite State Code Camp - Sat, Nov 14, 2020

About me:

  • I created the SeleniumBase framework.
  • I'm currently the DevOps Lead at iboss.

Topics & tools covered by this presentation:



  • Overview of Virtual Environments
  • Python package management
  • Python 3 "venv"
  • virtualenv / virtualenvwrapper
  • pip / "pip install"
  • requirements.txt files
  • setup.py files

Topics & tools that are NOT covered here:



  • "conda"
  • "pipenv"
  • "poetry"
  • "pipx"

(Other Python package management tools)

What is a Python virtual environment?



A Python virtual environment is a partitioned directory where a Python interpreter, libraries/packages, and scripts can be installed and isolated from those installed in other virtual environments or the global environment.

Why should we use Python virtual environments?



We should use Python virtual environments because different Python projects can have conflicting Python dependencies that cannot coexist in the same env.

Why? - continued



Example: Project A and Project B both depend on different versions of the same Python library!

Therefore, installing the second project requirements would overwrite the first one, causing it to break.

# Project A requirement:
urllib3==1.25.3

# Project B requirement:
urllib3==1.26.2

Why? - continued



It is also possible that Project A and Project B require different versions of Python installed!

# Project A requirement:
Python-3.8

# Project B requirement:
Python-2.7

How do we create and use Python virtual envs?



There are tools/scripts we can use:

  • The Python 3 "venv" command
  • virtualenv / virtualenvwrapper

Python 3 "venv"



"venv" creates virtual environments in the location where run (generally with Python projects).
# Mac / Linux
python3 -m venv ENV_NAME
source ENV_NAME/bin/activate

# Windows
py -m venv ENV_NAME
call ENV_NAME\Scripts\activate

# (Type "deactivate" to leave a virtual environment.)

"mkvirtualenv" (from virtualenvwrapper)



"mkvirtualenv" creates virtual environments in one place (generally in your home directory).
# Mac / Linux
python3 -m pip install virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source `which virtualenvwrapper.sh`
mkvirtualenv ENV_NAME

# Windows
py -m pip install virtualenvwrapper-win
mkvirtualenv ENV_NAME

# (Type "deactivate" to leave a virtual environment.)

List of commands from virtualenvwrapper



# Create a virtual environment:
mkvirtualenv ENV_NAME

# Exit your virtual environment:
deactivate

# Re-enter a virtual environment:
workon ENV_NAME

# List all virtual environments:
workon

# Delete a virtual environment:
rmvirtualenv ENV_NAME

Determining if you are in a virtual env



When activated, the name of your virtual env will appear in parentheses on the left side of your command prompt.

# Example of how it may look on a Windows machine:
C:\Users\Michael\github> mkvirtualenv my_env
(my_env)  C:\Users\Michael\github>

Installing packages with "pip install"



Once you have created a Python virtual environment and are inside, it is now safe to install packages from PyPI, setup.py files, and/or requirements.txt files.

# Install a package from PyPI:
pip install seleniumbase

# Install packages from a folder with setup.py:
pip install .  # Normal installation
pip install -e .  # Editable install

# Install packages from a requirements.txt file:
pip install -r requirements.txt

Other useful "pip" commands



# See which Python packages are installed:
pip list

# See which installed Python packages are outdated:
pip list --outdated

# Create a requirements file from installed packages:
pip freeze > my_requirements.txt


Live Demo Time!



The End. Questions?



Where to find me: