2. Python & Virtual Environments

Assuming you have the skill requirements for this book, you might want to just skip to the redis installation portion. You will need redis installed locally for this book (and for celery) although rabbitmq is another valid option.

  • Installing Python

  • Creating & Activating a Virtual Environment

  • Installing Python Packages (pip install)

  • Installing Redis (macOS, Windows, Linux)

2.1. Install Python 3+

You can download python on https://www.python.org/downloads/. That’s the easiest way on all platforms.

Below are a few blog posts on our website that are useful references:

2.2. Using Virtual Environments

Once you have Python installed, you’ll need to use a virtual environment for your project. Virtual environments keep all of the software requirements (ie dependency versions) isolated from other projects.

For better isolation, you can consider Docker but that’s not required here.

2.2.1. venv

Here’s the easies way to create a virtual environment using Python’s built-in venv module.

python -m venv my_venv

Replace my_venv with any folder name you want to create your virtual environment in. Python handles the creation of the environment, you must activate it. You can also substitute my_venv for . to create the virtual environment in your current directory (aka folder).

A few key commands:

  • Activate: source bin/activate (mac/linux)

  • Activate: .\Scripts\activate (windows)

  • Deactivate deactivate (all platforms; assuming your virtual environment is activated)

  • Install packages (activate first): pip install requests (replace requests with any python package)

  • Remove packages (activate first) pip uninstall requests

  • Installed packages pip freeze

  • Save package history (to re-install/re-create current environment): pip freeze > requirements.txt

2.2.2. pipenv

pipenv (https://github.com/pypa/pipenv) is another very popular way to create an manage your virtual environments.

Official installation options are on https://github.com/pypa/pipenv#installation.

Install:

python -m pip install pipenv
# or
python3 -m pip install pipenv

Create

cd path/to/project/dir
python -m pipenv --python 3.8

pipenv creates a Pipfile that is a document that describes your virtual environment.

A few key commands:

  • Activate: pipenv shell (mac/linux/windows)

  • Deactivate deactivate (all platforms; assuming your virtual environment is activated)

  • Install packages (activate first): pipenv install requests (replace requests with any python package)

  • Remove packages (activate first) pipenv uninstall requests

  • Installed packages pipenv run pip freeze

  • Save package history (to re-install/re-create current environment): pipenv run pip freeze > requirements.txt