Configuring Ubuntu 18.04 for a Python Dev Environment with Flask, Gunicron, and VirtualEnv
Flask is a great Python framework for getting ideas up and running quickly on a lightweight app environment. In this tutorial we are going to get Python 3.6 configured with development system packages to start a new Flask web application project and run it with Green Unicorn (Gunicorn).
But first, what is Green Unicorn?
Green Unicorn (from here on out, we will refer to it as Gunicorn) is “…a Python WSGI HTTP Server for UNIX. It’s a pre-fork worker model. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy.”
You can find out more about Gunicorn, from their website, which is also where I pulled that quote. But for the sake of brevity, Gunicorn is an easy to use HTTP server for Python web apps.
System Package Installations
To begin, we’re going to install three packages.
python-virtualenv. You should also check which version of Python you have installed and it’s location, then run the following command below:
sudo apt-get install python3-dev python3-pip python3-virtualenv
If run correctly, the package manager should spit a bunch of output and report when everything is set up. From there, we’ll use
virtualenv, both packages we just installed, to form a directory and handle our app dependencies.
###CREATE A NEW DIRECTORY TO HOUSE YOUR VIRTUAL ENVS
mkdir -p ~/venvs
###SPECIFY PYTHON3 FOR THE INSTALLTION AND THEN ACTIVATE YOUR VIRTUAL ENV
python3 -m venv venvs/flaskproject
If all goes well, you should see your terminal prompt changes with the name of your virtual environment that was activated. In our case, we have
(flaskproject) now in our terminal prompt.
Using PIP for upgrades, Wheel, and Gunicorn
We’re going to use
pip to install a few dependencies and packages. Let’s start by updating our packages list and then installing
wheel. Then we will install Flask and Gunicorn as well.
###UPDATING AND INSTALLING WHEEL
pip install --upgrade && pip install wheel
###INSTALLING FLASK AND GUNICORN
pip install flask gunicorn
You should see a bunch of output expressing successful installations. If all goes well, you shouldn’t see any errors.
Testing Flask and Running it with Gunicorn
We need to create a new directory to hold our app files. This directory will live within our home folder, not within our
###CREATE A DIRECTORY AND MOVE INTO IT
###CREATE FILE FOR TEST APP CODE
app.py file, write the following code:
from flask import Flask, Response
app = Flask(__name__)
return Response("Your Python Flask Project is Working!"), 200
if __name__ == "__main__":
Now move to the directory above the
flaskproject folder, which in our case is the user home folder. You can also just type
cd ~ to get there.
From your home folder, we can use
Gunicorn to run your app with the following command:
###RUN THIS FROM YOUR HOME FOLDER ~/
If successful, we’ll get output of
Gunicorn starting and listening on
127.0.0.0:8000 and going to that address should show your app.
That’s it! You basically set up a quick Python test environment using PIP, Flask, and Gunicorn. From here, you can build more dynamic web apps using Flask and test them the same way.