The Flask Mega-Tutorial Part I: Hello, World!

Welcome! You are about to start on a journey to learn how to create web applications with Python and the Flask framework. The video above will give you an overview of the contents of this tutorial. In this first chapter, you are going to learn how to set up a Flask project. By the end of this chapter you are going to have a simple Flask web application running on your computer!

For your reference, below is a list of the articles in this series.

Note 1: If you are looking for the legacy version of this tutorial, it's here.

Note 2: If you would like to support my work on this blog, or just don't have patience to wait for weekly articles, I am offering the complete version of this tutorial packaged as an ebook or a set of videos. For more information, visit courses.miguelgrinberg.com.

All the code examples presented in this tutorial are hosted on a GitHub repository. Downloading the code from GitHub can save you a lot of typing, but I strongly recommend that you type the code yourself, at least for the first few chapters. Once you become more familiar with Flask and the example application you can access the code directly from GitHub if the typing becomes too tedious.

At the beginning of each chapter, I'm going to give you three GitHub links that can be useful while you work through the chapter. The Browse link will open the GitHub repository for Microblog at the place where the changes for the chapter you are reading were added, without including any changes introduced in future chapters. The Zip link is a download link for a zip file including the entire application up to and including the changes in the chapter. The Diff link will open a graphical view of all the changes that were made in the chapter you are about to read.

The GitHub links for this chapter are: Browse, Zip, Diff.

Installing Python

If you don't have Python installed on your computer, go ahead and install it now. If your operating system does not provide you with a Python package, you can download an installer from the Python official website. If you are using Microsoft Windows along with WSL or Cygwin, note that you will not be using the Windows native version of Python, but a Unix-friendly version that you need to obtain from Ubuntu (if you are using WSL) or from Cygwin.

To make sure your Python installation is functional, you can open a terminal window and type python3, or if that does not work, just python. Here is what you should expect to see:

$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> _

The Python interpreter is now waiting at an interactive prompt, where you can enter Python statements. In future chapters you will learn what kinds of things this interactive prompt is useful for. But for now, you have confirmed that Python is installed on your system. To exit the interactive prompt, you can type exit() and press Enter. On the Linux and Mac OS X versions of Python you can also exit the interpreter by pressing Ctrl-D. On Windows, the exit shortcut is Ctrl-Z followed by Enter.

Installing Flask

The next step is to install Flask, but before I go into that I want to tell you about the best practices associated with installing Python packages.

In Python, packages such as Flask are available in a public repository, from where anybody can download them and install them. The official Python package repository is called PyPI, which stands for Python Package Index (some people also refer to this repository as the "cheese shop"). Installing a package from PyPI is very simple, because Python comes with a tool called pip that does this work (in Python 2.7 pip does not come bundled with Python and needs to be installed separately).

To install a package on your machine, you use pip as follows:

$ pip install <package-name>

Interestingly, this method of installing packages will not work in most cases. If your Python interpreter was installed globally for all the users of your computer, chances are your regular user account is not going to have permission to make modifications to it, so the only way to make the command above work is to run it from an administrator account. But even without that complication, consider what happens when you install a package as above. The pip tool is going to download the package from PyPI, and then add it to your Python installation. From that point on, every Python script that you have on your system will have access to this package. Imagine a situation where you have completed a web application using version 0.11 of Flask, which was the most current version of Flask when you started, but now has been superseeded by version 0.12. You now want to start a second application, for which you'd like to use the 0.12 version, but if you replace the 0.11 version that you have installed you risk breaking your older application. Do you see the problem? It would be ideal if it was possible to install Flask 0.11 to be used by your old application, and also install Flask 0.12 for your new one.

To address the issue of maintaining different versions of packages for different applications, Python uses the concept of virtual environments. A virtual environment is a complete copy of the Python interpreter. When you install packages in a virtual environment, the system-wide Python interpreter is not affected, only the copy is. So the solution to have complete freedom to install any versions of your packages for each application is to use a different virtual environment for each application. Virtual environments have the added benefit that they are owned by the user who creates them, so they do not require an administrator account.

Let's start by creating a directory where the project will live. I'm going to call this directory microblog, since that is the name of the application:

$ mkdir microblog
$ cd microblog

If you are using a Python 3 version, virtual environment support is included in it, so all you need to do to create one is this:

$ python3 -m venv venv

With this command, I'm asking Python to run the venv package, which creates a virtual environment named venv. The first venv in the command is the name of the Python virtual environment package, and the second is the virtual environment name that I'm going to use for this particular environment. If you find this confusing, you can replace the second venv with a different name that you want to assign to your virtual environment. In general I create my virtual environments with the name venv in the project directory, so whenever I cd into a project I find its corresponding virtual environment.

Note that in some operating systems you may need to use python instead of python3 in the command above. Some installations use python for Python 2.x releases and python3 for the 3.x releases, while others map python to the 3.x releases.

After the command completes, you are going to have a directory named venv where the virtual environment files are stored.

If you are using any version of Python older than 3.4 (and that includes the 2.7 release), virtual environments are not supported natively. For those versions of Python, you need to download and install a third-party tool called virtualenv before you can create virtual environments. Once virtualenv is installed, you can create a virtual environment with the following command:

$ virtualenv venv

Regardless of the method you used to create it, you should have your virtual environment created. Now you have to tell the system that you want to use it, and you do that by activating it. To activate your brand new virtual environment you use the following command:

$ source venv/bin/activate
(venv) $ _

If you are using a Microsoft Windows command prompt window, the activation command is slightly different:

$ venv\Scripts\activate
(venv) $ _

When you activate a virtual environment, the configuration of your terminal session is modified so that the Python interpreter stored inside it is the one that is invoked when you type python. Also, the terminal prompt is modified to include the name of the activated virtual environment. The changes made to your terminal session are all temporary and private to that session, so they will not persist when you close the terminal window. If you work with multiple terminal windows open at the same time, it is perfectly fine to have different virtual environments activated on each one.

Now that you have a virtual environment created and activated, you can finally install Flask in it:

(venv) $ pip install flask

If you want to confirm that your virtual environment now has Flask installed, you can start the Python interpreter and import Flask into it:

>>> import flask
>>> _

If this statement does not give you any errors you can congratulate yourself, as Flask is installed and ready to be used.

A "Hello, World" Flask Application

If you go to the Flask website, you are welcomed with a very simple example application that has just five lines of code. Instead of repeating that trivial example, I'm going to show you a slightly more elaborate one that will give you a good base structure for writing larger applications.

The application will exist in a package. In Python, a sub-directory that includes a __init__.py file is considered a package, and can be imported. When you import a package, the __init__.py executes and defines what symbols the package exposes to the outside world.

Let's create a package called app, that will host the application. Make sure you are in the microblog directory and then run the following command:

(venv) $ mkdir app

The __init__.py for the app package is going to contain the following code:

app/__init__.py: Flask application instance

from flask import Flask

app = Flask(__name__)

from app import routes

The script above simply creates the application object as an instance of class Flask imported from the flask package. The __name__ variable passed to the Flask class is a Python predefined variable, which is set to the name of the module in which it is used. Flask uses the location of the module passed here as a starting point when it needs to load associated resources such as template files, which I will cover in Chapter 2. For all practical purposes, passing __name__ is almost always going to configure Flask in the correct way. The application then imports the routes module, which doesn't exist yet.

One aspect that may seem confusing at first is that there are two entities named app. The app package is defined by the app directory and the __init__.py script, and is referenced in the from app import routes statement. The app variable is defined as an instance of class Flask in the __init__.py script, which makes it a member of the app package.

Another peculiarity is that the routes module is imported at the bottom and not at the top of the script as it is always done. The bottom import is a workaround to circular imports, a common problem with Flask applications. You are going to see that the routes module needs to import the app variable defined in this script, so putting one of the reciprocal imports at the bottom avoids the error that results from the mutual references between these two files.

So what goes in the routes module? The routes are the different URLs that the application implements. In Flask, handlers for the application routes are written as Python functions, called view functions. View functions are mapped to one or more route URLs so that Flask knows what logic to execute when a client requests a given URL.

Here is your first view function, which you need to write in the new module named app/routes.py:

app/routes.py: Home page route

from app import app

def index():
    return "Hello, World!"

This view function is actually pretty simple, it just returns a greeting as a string. The two strange @app.route lines above the function are decorators, a unique feature of the Python language. A decorator modifies the function that follows it. A common pattern with decorators is to use them to register functions as callbacks for certain events. In this case, the @app.route decorator creates an association between the URL given as an argument and the function. In this example there are two decorators, which associate the URLs / and /index to this function. This means that when a web browser requests either of these two URLs, Flask is going to invoke this function and pass the return value of it back to the browser as a response. If this does not make complete sense yet, it will in a little bit when you run this application.

To complete the application, you need to have a Python script at the top-level that defines the Flask application instance. Let's call this script microblog.py, and define it as a single line that imports the application instance:

microblog.py: Main application module

from app import app

Remember the two app entities? Here you can see both together in the same sentence. The Flask application instance is called app and is a member of the app package. The from app import app statement imports the app variable that is a member of the app package. If you find this confusing, you can rename either the package or the variable to something else.

Just to make sure that you are doing everything correctly, below you can see a diagram of the project structure so far:


Believe it or not, this first version of the application is now complete! Before running it, though, Flask needs to be told how to import it, by setting the FLASK_APP environment variable:

(venv) $ export FLASK_APP=microblog.py

If you are using Microsoft Windows, use set instead of export in the command above.

Are you ready to be blown away? You can run your first web application, with the following command:

(venv) $ flask run
 * Serving Flask app "microblog"
 * Running on (Press CTRL+C to quit)

After the server initializes it will wait for client connections. The output from flask run indicates that the server is running on IP address, which is always the address of your own computer. This address is so common that is also has a simpler name that you may have seen before: localhost. Network servers listen for connections on a specific port number. Applications deployed on production web servers typically listen on port 443, or sometimes 80 if they do not implement encryption, but access to these ports require administration rights. Since this application is running in a development environment, Flask uses the freely available port 5000. Now open up your web browser and enter the following URL in the address field:


Alternatively you can use this other URL:


Do you see the application route mappings in action? The first URL maps to /, while the second maps to /index. Both routes are associated with the only view function in the application, so they produce the same output, which is the string that the function returns. If you enter any other URL you will get an error, since only these two URLs are recognized by the application.

Hello, World!

When you are done playing with the server you can just press Ctrl-C to stop it.

Congratulations, you have completed the first big step to become a web developer!

Before I end this chapter, I want to mention one more thing. Since environment variables aren't remembered across terminal sessions, you may find tedious to always have to set the FLASK_APP environment variable when you open a new terminal window. Starting with version 1.0, Flask allows you to register environment variables that you want to be automatically imported when you run the flask command. To use this option you have to install the python-dotenv package:

(venv) $ pip install python-dotenv

Then you can just write the environment variable name and value in a .flaskenv file in the top-level directory of the project:

.flaskenv: Environment variables for flask command


Doing this is optional. If you prefer to set the environment variable manually, that is perfectly fine, as long as you always remember to do it.


  • #76 Shubham Saurabh said 2018-02-15T06:57:33Z

    Guys if you want to start your server with another hostname or port just use the command as follows

    flask run -h -p

    eg: flask run -h -p 8080

  • #77 jacko said 2018-02-15T16:59:21Z

    Miguel thank you for putting the time and energy into making this resource. Great resource for newbies like me but difficul to understand how to overcome weird niggling issues that come up as a result of what It think might be as a result of a mistype somewhere. I ran into the following issues:

    I ran into an issue with set FLASK_APP = microblog.py, it worked initially but the cmd line locked up on me after I left the app running for some time, when I closed it and activated the venv again it lost the path. I reset the path but now get a "Error: No such command 'app'." error

    When I had the application running I got to page 2 of your tutorial and was made the instructed changes to microblog.py and to the index.html. When launched localhost:5000/index showed a "Internal Server Error".

  • #78 Miguel Grinberg said 2018-02-15T17:57:56Z

    @jacko: the best thing you can do is to compare your version of the application against mine. That is going to point you to any mistakes you may have made. For #1 I don't really understand the error. Are you trying to run "app"? For #2, that is very likely a typo in the application, if you look at the terminal you should see a stack trace and error information.

  • #79 Miguel Grinberg said 2018-02-15T19:04:43Z

    @Shubham: There are better ways to deploy your application to be used on your intranet or on the internet than using the flask development web server.

  • #80 David Bird said 2018-02-16T12:05:06Z

    The server works fine ... ... but I am wondering why the index page does not update when I change the routes.py to:

    from app import app

    @app.route('/') @app.route('/index') def index(): return "Hey Bro!"

    After saving this, when I hit refresh, I still get the Hello World. Why is that ?

  • #81 Miguel Grinberg said 2018-02-16T17:46:00Z

    @David: not sure, could be that your browser is caching the page instead of asking the server for a new copy. The following page gives you instructions for all platforms and all web browsers on how to refresh the page bypassing the cache: https://en.wikipedia.org/wiki/Wikipedia:Bypass_your_cache. Give that a try, hopefully that'll fix the problem.

  • #82 Mark said 2018-02-18T23:16:21Z

    On debian stretch

    First python3 did not come with venv. python3-venv had to be installed I got stuck at "pip install flask". It installed it to python 2.7. "pip3 install flask" installed it to python 3.5.

    errors: error: invalid command 'bdist_wheel' Failed building wheel for itsdangerous Failed building wheel for MarkupSafe

    pip3 install wheel seemed to fix this but when I uninstall flask and reinstall it, it treats it like it is already set and good. with the previous errors.

    This is a lot of issues from the very start and I hope that after I delete the entire dir, and start from scratch again that these major issues will stop. I can't make flask install from the beginning instead of accepting the broken install, after uninstalling. It just says requirements already met.

    You obviously put a lot of time and effort into this. It is a shame it isn't working on debian at all.

  • #83 Miguel Grinberg said 2018-02-19T02:00:52Z

    @Mark: You are correct on the first problem. Debian and derivatives have a strange deployment strategy, where they have a separate package for the venv package. This is why I send you to the installation documentation for your operating system for instructions on how to properly install Python.

    Your second problem is your own fault, though. You seem to be attempting to install Flask globally instead of inside a virtual environment. This is mentioned in the tutorial, once you activate your virtual environment, the names of the Python interpreter and utilities such as pip is normalized, and you don't have to append a "3" anymore.

  • #84 Christian Pascual said 2018-02-22T06:48:00Z

    Had a small obstacle come up while I was doing this step on my Windows machine.

    Instead of "set FLASK_APP=microblog.py," I had to consult Stack Overflow for an alternative answer.

    The following code was able to correctly set the FLASK_APP variable:

    $env:FLASK_APP = "microblog.py"

  • #85 Miguel Grinberg said 2018-02-22T17:00:01Z

    @Christian: That is because you are using the PowerShell, which I explicitly decided not to cover in this tutorial. My Windows instructions are for the standard command prompt.

  • #86 samw said 2018-03-01T21:33:04Z

    Perhaps the best introduction to virtual environments I have read -- and explanations of venv are all over the place. (Usually when I try to do it right I end up hearing Michael Gambon telling Daniel Craig that it all "ends in tears." -- Layer Cake) And yours is an interesting approach to establishing a flask. I did forget to move up in the tree from my app directory before saving the file microblog.py and running flask run. (I had to read more carefully your superlative instructions.) The other oddity was the following (in terminal):

    samw@Inspiron-1545 ~/flasks/microblog $ python3 -m venv venv The virtual environment was not created successfully because ensurepip is not available. On Debian/Ubuntu systems, you need to install the python3-venv package using the following command.

    apt-get install python3-venv

    You may need to use sudo with that command. After installing the python3-venv package, recreate your virtual environment.

    Failing command: ['/home/samw/flasks/microblog/venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']

    I do not remember this being the case. (Have been using Pycharm and gotten spoiled, it seems.) I did as instructed. It worked. Thanks. I expect to pony up and buy.

  • #87 extreeme said 2018-03-06T07:34:10Z

    Thank you for your brain-friendly tutorials.

    But when coding along with this tutorial, I came up with an error

    "Value Error: source code string cannot contain null bytes"

    I'm using Windows 8.1, Python 3. I bet this error is somehow related to the python interpreter or the venv.

    Did anybodee have the same issue? Please help.

  • #88 Miguel Grinberg said 2018-03-06T17:18:55Z

    @extreeme: you need to look at the line of code that produced this error. This is likely an import line. The module that is being imported is probably corrupted. My suggestion is that you reinstall Python and your virtual environment.

  • #89 Yitao said 2018-03-07T08:58:46Z

    The flask mega series is wonderful I think. I also bought the book 'Flask Web development (1st edition)', now 2nd edition is coming soon, and I downloaded its code from github as well as the code of Flask mega series. But I found there's some differences in source code between them. For example, to import a module at same package, you can use absolutely import like 'from app.api import abcde', but also you can use relatively import 'from . import abcde'. There's some other differences in details, such as how to apply the configuration. (Of course the main structure of projects look the same). So I'd like to know is there any best practise for a flask project, like a template available at any time, will you organize the code into a unified style in future?

  • #90 Miguel Grinberg said 2018-03-07T17:28:18Z

    @Yitao: The way you import your modules or how you structure your application are not a concern of Flask, you can use what works best for you. What a lot of people do not understand is that this is Flask's greatest strength, by not forcing you to follow a specific way of development it is able to fit in well in pretty much any type of project or organization.

  • #91 Manan said 2018-03-10T09:52:12Z

    Hello Miguel,I followed the steps precisely but when I type in the command flask run,an error shows up - Error: The file/path provided (microblog.py) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py How do I resolve this?

  • #92 zach said 2018-03-10T14:38:08Z

    I receive "GET / HTTP/1.1" 404.

  • #93 Miguel Grinberg said 2018-03-10T19:18:05Z

    @Manan: where did you put your microblog.py source file? Should be in the top-level directory (the current directory from where you run "flask run").

  • #94 Miguel Grinberg said 2018-03-10T19:22:23Z

    @zach: this could be a lot of different problems. Please compare your code against mine on GitHub to find the mistake.

  • #95 anthony said 2018-03-12T15:30:58Z

    Hi Miguel, huge fan of the tutorials and have really learned a lot from your writing. Came across a small problem and was wondering if you could help clarify.

    Why is it that "flask run" will work, but using "python microblog.py" will lead to an error displaying: "Importerror: no module named app"? Trying to get this framework to work with AWS and many of the tutorials require that the app run when calling "python microblog.py".

  • #96 Miguel Grinberg said 2018-03-12T17:30:20Z

    @anthony: I'm not sure, but my guess is that the directory where microblog.py is located is not in the Python path. Try setting the PYTHONPATH environment variable to your microblog directory.

  • #97 GNK said 2018-03-13T12:00:37Z

    Wonderful tutorial. Thanks a lot.

  • #98 Ian said 2018-03-13T12:05:01Z


    Great blog and book! I'm really enjoying working my way through it. It took me a while to understand the super function. I originally implemented it myself using current_user. Perhaps the book could explain why this isn't the best solution due to bringing in the extra dependency.

    Secondly, I just want to confirm if this is OK: super().init(args, *kwargs)

    From what I read, this is the cleaner way of doing things in Python 3.

  • #99 Miguel Grinberg said 2018-03-13T16:51:55Z

    @Ian: The super() function is a standard feature of the Python language. It is well documented at https://docs.python.org/3/library/functions.html?highlight=super#super. And you are correct, super has a simplified syntax in Python 3 that requires no arguments.

  • #100 Gavin Lindridge said 2018-03-15T18:22:59Z

    Im finding this guide wonderful, thanks.

    A little question, is it normal to have to retype into the terminal each time you open a terminal - "export FLASK_APP=microblog.py"

    I assume i'm misunderstanding something as I thought that this would be stored somewhere in the Virtualenv?

Leave a Comment