Python Flask Quickstart
Flask is a popular Python web framework, meaning it is a third-party Python library used for developing web applications.
Prerequisites
Before you start proceeding with this tutorial, I am assuming that you have hands-on experience on HTML and Python. If you are not well aware of these concepts, then I will suggest you to go through on short tutorials on HTML and Python.
Installation
Python Version
I’ll be using the latest version of Python 3. Flask supports Python 3.5 and newer, Python 2.7, and PyPy.
Virtual environments
Use a virtual environment to manage the dependencies for your project, both in development and in production.
What problem does a virtual environment solve? The more Python projects you have, the more likely it is that you need to work with different versions of Python libraries, or even Python itself. Newer versions of libraries for one project can break compatibility in another project.
Virtual environments are independent groups of Python libraries, one for each project. Packages installed for one project will not affect other projects or the operating system’s packages.
Python 3 comes bundled with the venv module to create virtual environments.
Create an environment
Create a project folder and a env folder within, here I have my project named as demoProject and env is just folder within in which installing of flask will be done.
On Mac and Linux:
Activate the environment
Before you work on your project, activate the corresponding environment:
Your shell prompt will change to show the name of the activated environment as shown below.
Install Flask
Within the activated environment, use the following command to install Flask:
Flask is now installed.
Create and run a minimal Flask app
In Sublime Text IDE , create a new file in your project folder named app.py
In app.py , add code to import Flask and create an instance of the Flask object. If you type the code below (instead of using copy-paste)
Also in app.py , add a function that returns content, in this case a simple string, and use Flask's app.route decorator to map the URL route / to that function:
Tip: You can use multiple decorators on the same function, one per line, depending on how many different routes you want to map to the same function.
Save the app.py file
In the terminal, run the app by entering python3 -m flask run (macOS/Linux) or python -m flask run (Windows), which runs the Flask development server. The development server looks for app.py by default. When you run Flask, you should see output similar to the following:
Also, if you want to run the development server on a different IP address or port, use the host and port command-line arguments, as with —host=0.0.0.0 —port=80 .
To open your default browser to the rendered page, Ctrl+click the http://127.0.0.1:5000/ URL in the terminal.
Another ways to run the server
1.In this way you need to tell your terminal the application to work with by exporting the FLASK_APP environment variable, But one problem can exist i.e when you change in the python file and reload the page you will not be able to see that change which you made while in presence of running server.
To see the change that you have to restart the server. Hence you will see the change.
2. In this way you will be able to make change in your files while running of your server.
3. Add below lines in the your app.py file and run by the command python app.py .
This was the first part of the Get your hands on Flask.Whole series will be coming soon.
Name already in use
flask / docs / installation.rst
- Go to file T
- Go to line L
- Copy path
- Copy permalink
- Open with Desktop
- View raw
- Copy raw contents Copy raw contents
Copy raw contents
Copy raw contents
We recommend using the latest version of Python. Flask supports Python 3.7 and newer.
These distributions will be installed automatically when installing Flask.
-
implements WSGI, the standard Python interface between applications and servers. is a template language that renders the pages your application serves. comes with Jinja. It escapes untrusted input when rendering templates to avoid injection attacks. securely signs data to ensure its integrity. This is used to protect Flask’s session cookie. is a framework for writing command line applications. It provides the flask command and allows adding custom management commands.
These distributions will not be installed automatically. Flask will detect and use them if you install them.
-
provides support for :doc:`signals` . enables support for :ref:`dotenv` when running flask commands. provides a faster, more efficient reloader for the development server.
You may choose to use gevent or eventlet with your application. In this case, greenlet>=1.0 is required. When using PyPy, PyPy>=7.3.7 is required.
These are not minimum supported versions, they only indicate the first versions that added necessary features. You should use the latest versions of each.
Use a virtual environment to manage the dependencies for your project, both in development and in production.
What problem does a virtual environment solve? The more Python projects you have, the more likely it is that you need to work with different versions of Python libraries, or even Python itself. Newer versions of libraries for one project can break compatibility in another project.
Virtual environments are independent groups of Python libraries, one for each project. Packages installed for one project will not affect other projects or the operating system’s packages.
Python comes bundled with the :mod:`venv` module to create virtual environments.
Create an environment
Create a project folder and a :file:`.venv` folder within:
Activate the environment
Before you work on your project, activate the corresponding environment:
Your shell prompt will change to show the name of the activated environment.
Within the activated environment, use the following command to install Flask:
3. Installation¶
Flask depends on some external libraries, like Werkzeug and Jinja2. Werkzeug is a toolkit for WSGI, the standard Python interface between web applications and a variety of servers for both development and deployment. Jinja2 renders templates.
So how do you get all that on your computer quickly? There are many ways you could do that, but the most kick-ass method is virtualenv, so let’s have a look at that first.
You will need Python 2.6 or newer to get started, so be sure to have an up-to-date Python 2.x installation. For using Flask with Python 3 have a look at python3-support .
3.1. virtualenv¶
Virtualenv is probably what you want to use during development, and if you have shell access to your production machines, you’ll probably want to use it there, too.
What problem does virtualenv solve? If you like Python as much as I do, chances are you want to use it for other projects besides Flask-based web applications. But the more projects you have, the more likely it is that you will be working with different versions of Python itself, or at least different versions of Python libraries. Let’s face it: quite often libraries break backwards compatibility, and it’s unlikely that any serious application will have zero dependencies. So what do you do if two or more of your projects have conflicting dependencies?
Virtualenv to the rescue! Virtualenv enables multiple side-by-side installations of Python, one for each project. It doesn’t actually install separate copies of Python, but it does provide a clever way to keep different project environments isolated. Let’s see how virtualenv works.
If you are on Mac OS X or Linux, chances are that the following command will work for you:
It will probably install virtualenv on your system. Maybe it’s even in your package manager. If you use Ubuntu, try:
If you are on Windows and don’t have the easy_install command, you must install it first. Check the pip and setuptools on Windows section for more information about how to do that. Once you have it installed, run the same commands as above, but without the sudo prefix.
Once you have virtualenv installed, just fire up a shell and create your own environment. I usually create a project folder and a venv folder within:
Now, whenever you want to work on a project, you only have to activate the corresponding environment. On OS X and Linux, do the following:
If you are a Windows user, the following command is for you:
Either way, you should now be using your virtualenv (notice how the prompt of your shell has changed to show the active environment).
And if you want to go back to the real world, use the following command:
After doing this, the prompt of your shell should be as familiar as before.
Now, let’s move on. Enter the following command to get Flask activated in your virtualenv:
A few seconds later and you are good to go.
3.2. System-Wide Installation¶
This is possible as well, though I do not recommend it. Just run pip with root privileges:
(On Windows systems, run it in a command-prompt window with administrator privileges, and leave out sudo .)
3.3. Living on the Edge¶
If you want to work with the latest version of Flask, there are two ways: you can either let pip pull in the development version, or you can tell it to operate on a git checkout. Either way, virtualenv is recommended.
Get the git checkout in a new virtualenv and run in development mode:
This will pull in the dependencies and activate the git head as the current version inside the virtualenv. Then all you have to do is run git pull origin to update to the latest version.
3.4. pip and setuptools on Windows¶
Sometimes getting the standard “Python packaging tools” like pip , setuptools and virtualenv can be a little trickier, but nothing very hard. The crucial package you will need is pip — this will let you install anything else (like virtualenv). Fortunately there is a “bootstrap script” you can run to install.
If you don’t currently have pip , then get-pip.py will install it for you.
It should be double-clickable once you download it. If you already have pip , you can upgrade them by running:
Most often, once you pull up a command prompt you want to be able to type pip and python which will run those things, but this might not automatically happen on Windows, because it doesn’t know where those executables are (give either a try!).
To fix this, you should be able to navigate to your Python install directory (e.g C:Python27 ), then go to Tools , then Scripts , then find the win_add2path.py file and run that. Open a new Command Prompt and check that you can now just type python to bring up the interpreter.
Finally, to install virtualenv, you can simply run:
Then you can be off on your way following the installation instructions above.
How To Install Flask
Flask is one of the most popular web application frameworks written in Python. It is a microframework designed for an easy and quick start. Extending with tools and libraries adds more functionality to Flask for more complex projects.
This article explains how to install Flask in a virtual testing environment and create a simple Flask application.

- Installed Python 2.7 or Python 3.5 and newer
- CLI with administrator privileges
Note: Python 2 has reached the end-of-life maintenance status. It officially no longer has support as of 2020. Follow one of our guides on installing Python 3: How to install Python 3 on CentOS 7, How to install Python 3 on CentOS 8, How to install Python 3 on Ubuntu, How to install Python on Windows.
Step 1: Install Virtual Environment
Install Flask in a virtual environment to avoid problems with conflicting libraries. Check Python version before starting:
- Python 3 comes with a virtual environment module called venv preinstalled. If you have Python 3 installed, skip to Step 2.
- Python 2 users must install the virtualenv module. If you have Python 2, follow the instructions outlined in Step 1.
Install virtualenv on Linux
The package managers on Linux provides virtualenv.
- For Debian/Ubuntu:
1. Start by opening the Linux terminal.
2. Use apt to install virtualenv on Debian, Ubuntu and other related distributions:
- For CentOS/Fedora/Red Hat:
1. Open the Linux terminal.
2. Use yum to install virtualenv on CentOS, Red Hat, Fedora and related distributions:
Install virtualenv on MacOS
1. Open the terminal.
2. Install virtualenv on Mac using pip :
Install virtualenv on Windows
1. Open the command line with administrator privileges.
2. Use pip to install virtualenv on Windows:
Note: To install pip on Windows, follow our How to install pip on Windows guide.
Step 2: Create an Environment
1. Make a separate directory for your project:
2. Move into the directory:
3. Within the directory, create the virtual environment for Flask. When you create the environment, a new folder appears in your project directory with the environment’s name.
Create an Environment in Linux and MacOS
- For Python 3:
To create a virtual environment for Python 3, use the venv module and give it a name:
- For Python 2:
For Python 2, use the virtualenv module to create a virtual environment and name it:
Listing the directory structure with the ls command shows the newly created environment:

Create an Environment in Windows
- For Python 3:
Create and name a virtual environment in Python 3 with:
- For Python 2:
For Python 2, create the virtual environment with the virtualenv module:
List the folder structure using the dir command:
The project directory shows the newly created environment:

Step 3: Activate the Environment
Activate the virtual environment before installing Flask. The name of the activated environment shows up in the CLI after activation.
Activate the Environment on Linux and MacOS
Activate the virtual environment in Linux and MacOS with:

Activate the Environment on Windows
For Windows, activate the virtual environment with:

Step 4: Install Flask
Install Flask within the activated environment using pip :
Flask is installed automatically with all the dependencies.
Note: pip is a Python package manager. To install pip follow one of our guides: How to install pip on CentOS 7, How to install pip on CentOS 8, How to install pip on Debian, or How to install pip on Ubuntu.
Step 5: Test the Development Environment
1. Create a simple Flask application to test the newly created development environment.
2. Make a file in the Flask project folder called hello.py.
3. Edit the file using a text editor and add the following code to make an application that prints «Hello world!«:
Note: Pick any name for the project except flask.py. The Flask library is in a flask.py file.
4. Save the file and close.
5. Using the console, navigate to the project folder using the cd command.
6. Set the FLASK_APP environment variable.
- For Linux and Mac:
- For Windows:
Note: Windows users must restart the console to set the environment variable. Learn more about setting environment variables by reading one of our guides: How to set environmet variables in Linux, How to set environment variables in MacOS, How to set environment variables in Windows.
7. Run the Flask application with:

The output prints out a confirmation message and the address.
8. Copy and paste the address into the browser to see the project running:

Flask web applications are easy to configure and run. It is one of the most popular web application frameworks for Python.
Read about the best Python IDEs and code editors to choose the best environment for further web development with Flask.