Setup the virtual environment and packages

If you're already familiar with Python virtual environments and requirements.txt files, you can skip this chapter.

venv

One issue that developers often encounter is the incredible number of ways that Python and its packages can be installed and configured. One way to avoid versioning issues with Python packages is to use a virtual environment. The application 'virtualenv' creates an isolated Python environment that you can install specific package versions into. Additionally, you can output these package names and versions into a file traditionally called 'requirements.txt' and use that file to perfectly rebuild your Python environment on another compute environment. venv is a part of the Python standard library that has very similar functionality to virtualenv. If you're using a version of Python prior to 3.3 you won't be able to use venv and will have to install virtualenv. Either one works for our purposes — we'll be using venv as a matter of personal preference.

We will be using a virtual environment because Zappa requires one (I'll get to Zappa in a later chapter). Please note that Zappa also requires Python 2.7 or Python 3.6, and if your virtual environment is not one of these then you will probably run into issues. We'll be using Python 3.6 in this guide.

If you are on a Mac and don't have Python 3.6 and pip installed, I'd check out Homebrew.

If you are on Windows, I recommend installing the Windows Subsystem for Linux, which will give you an Ubuntu environment. You don't have to do that to get Python/Flask/Zappa to run, but for the purposes of this guide I'm going to assume you're using some form of Linux shell (which you can get on Windows with the Windows Subsystem for Linux).

If you are on a recent distribution of Ubuntu, Python 3.6 will come pre-installed but the venv extension will not. You can add it by running sudo apt-get install python3-venv

Once you have Python 3.6 with the venv extension installed on your system you can immediately create your virtual environment:

python3.6 -m venv venv

This explicitly calls the Python 3.6 interpreter. There are two venv words there intentionally; the first venv (after the -m) invokes the venv module, and the second venv is the name of the directory which is created to hold your virtual environment. You can call this something else if you want, but venv is traditional.

You'll now see a folder called venv. To activate the environment, run the command

source venv/bin/activate

You'll need to do this every time you want to run flask or zappa.

The final step in our setup is to run

pip install -r requirements.txt

The requirements.txt file can be found in the git repository that we've already cloned. We'll be talking about the packages named in that file throughout the guide, but once you've run the install command above then they'll all be available to import when we get to them.


If you're finding this guide useful, you may want to sign up to receive more of my writing at cloudconsultant.dev.