Back to articles list
- 4 minutes read

Setting up a Local Development Environment

How This Tutorial Is Organized

The tutorial is divided into four articles:

  1. Introduction – Dive into web development with Flask. This article explains the basics of web development with Flask
  2. Part I – Setting up a local development environment (You are here)
  3. Part II – Application development
  4. Part III – Deployment to Heroku


Step 1: Create Virtual Environment for All Required Dependencies

The code presented in this article was tested against Python 2.7. So, if you haven’t yet installed it, go ahead and get Python. Then, download pip. Pip is a Python package manager – a tool for installing Python packages from the Python Package Index. In most cases, you shouldn’t install packages globally. Instead you should dive in a virtual environment concept. It is a tool to create isolated Python environments to give access to only the packages that a particular application can use. Thanks to virtual environments, you can use different versions of packages with no conflicts. In other words, it is a directory with a copy of the Python interpreter, a copy of the pip installer and copy of everything needed to run a particular Python program.

For the initial setup, we’re going to use Virtualenvwrapper (it provides a set of wrappers to manage virtual environments):

  1. Install virtualenvwrapper

    $ pip install virtualenvwrapper

  2. Create a virtual environment for your app. I’ll name mine todoapp.

    mkvirtualenv todoapp

  3. In order to use the created virtual environment type:

    workon todoapp

Step 2: Install Required Packages: Flask, psycopg2, SQLAlchemy

Install Flask:

$ pip install Flask

To integrate our app with PostgreSQL we need to install:

  • Psycopg2 – a Python adapter for Postgres
  • Flask-SQLAlchemy – Flask wrapper for SQLAlchemy (a powerful relational database framework that offers a high level ORM and low level access to a database’s native SQL functionality for Python.)

pip install psycopg2 Flask-SQLAlchemy

Step 3: Create requirements.txt File With All Dependencies Listed

Our application must have a requirements.txt that contains all the package dependencies with the exact versions.

$ pip freeze > requirements.txt.

Step 4: Install PostgreSQL 9.3 and Create Local Database

We’ll need to get a Postgres database set up to store our todo list. We’ll also add Python ORM SQLAlchemy to our app. Once you have Postgres installed, create a database and name it todoapp to use as a local database.

  1. Switch to postgres account.

    sudo su - postgres

  2. Run PostgreSQL command line client.


  3. Create a database user with a password.

    create user patrycja with password 'mypassword';

  4. Create a database instance.

    create database todoapp owner patrycja encoding 'utf-8';

Step 5: Organize the Structure of the Application

The structure for our web app will be as follows:

The file is the Python code that will import the app and execute.

The file contains a view function.

The file contains SQLAlchemy models.

The static folder contains all the css and javascript files.

The templates folder contains Jinja2 templates.

For our needs, the structure is simplistic. If you want to know how to structure a large Flask application, take a look at this tutorial

Step 6: Make Flask Say “Hello”

In a create an instance of the Flask application as follows:

from flask import Flask

app = Flask(__name__)

from views import *

if __name__ == '__main__':

Notice that the view module (module with all the view functions – the ones with route() decorator) should be imported after the application object is created. When you look below, view the module import app object from the todoapp module. If our views module in were imported at the top of the file, we would have a circular import.


from todoapp import app

def index():
return '

Hello World!


Run the application:

(todoapp) $ python
 * Running on

Open the browser and type in the address bar. You will see the the Hello greeting.

Flask Hello World

« Previous Part Next Part »

go to top