DEV Community

Sahil Rajput
Sahil Rajput

Posted on • Updated on

How to Install Flask Install Flask and create your first web application

There are tons of Python web frameworks and Flask is one of them but it is not a full stack web framework.
It is “a microframework for Python based on Werkzeug, Jinja 2 and good intentions.” Includes a built-in development server, unit tesing support, and is fully Unicode-enabled with RESTful request dispatching and WSGI compliance.

Installation

To install flask you can go here or just follow below steps:

Step1: Install virtual environment

If you are using Python3 than you don't have to install virtual environment because it already come with venv module to create virtual environments.
If you are using Python 2, the venv module is not available. Instead, install virtualenv.

On Linux, virtualenv is provided by your package manager:

//Debian, Ubuntu
$ sudo apt-get install python-virtualenv
//CentOS, Fedora
$ sudo yum install python-virtualenv
//Arch
$ sudo pacman -S python-virtualenv
Enter fullscreen mode Exit fullscreen mode

If you are on Mac OS X or Windows, download get-pip.py, then:

$ sudo python2 Downloads/get-pip.py
$ sudo python2 -m pip install virtualenv
Enter fullscreen mode Exit fullscreen mode

On Windows, as an administrator:

\Python27\python.exe Downloads\get-pip.py
\Python27\python.exe -m pip install virtualenv
Enter fullscreen mode Exit fullscreen mode

Step 2: Create an environment

Create a project folder and a venv folder within:

mkdir myproject
cd myproject
python3 -m venv venv
Enter fullscreen mode Exit fullscreen mode

pic1

On Windows:

py -3 -m venv venv
Enter fullscreen mode Exit fullscreen mode

If you needed to install virtualenv because you are on an older version of Python, use the following command instead:

virtualenv venv
Enter fullscreen mode Exit fullscreen mode

On Windows:

\Python27\Scripts\virtualenv.exe venv
Enter fullscreen mode Exit fullscreen mode

Activate the environment

Before you work on your project, activate the corresponding environment:

. venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

pic2

On Windows:

venv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

Your shell prompt will change to show the name of the activated environment.

Step 3: Install Flask

Within the activated environment, use the following command to install Flask:

$ pip install Flask
Enter fullscreen mode Exit fullscreen mode

screenshot 2018-11-05 at 9 22 44 pm

Flask is now installed: Check out the Quickstart or go to the Documentation.

Create a applcation

So, let's build the most simplest hello world application.
Follow these steps:

  1. As, you are already present in the myproject folder. Create a file `hello.py' and write the below code.

    • Import the Flask class. An instance of this class will be our WSGI application.

      from flask import Flask 
      
    • Next we create an instance of this class. The first argument is the name of the application’s module or package. If you are using a single module (as in this example), you should use __ name __ because depending on if it’s started as application or imported as module the name will be different ('main' versus the actual import name). This is needed so that Flask knows where to look for templates, static files, and so on.

      app = Flask(__ name __)
      
    • We then use the route() decorator to tell Flask what URL should trigger our function.The function is given a name which is also used to generate URLs for that particular function, and returns the message we want to display in the user’s browser.

      @app.route('/')
      def hello_world():
          return 'Hello, World!'
      

    Make sure to not call your application flask.py because this would conflict with Flask itself.

    To run the application you can either use the flask command or python’s -m switch with Flask. Before you can do that you need to tell your terminal the application to work with by exporting the FLASK_APP environment variable:

    $ export FLASK_APP=hello.py
    $ flask run
    //Or you can use
    $ export FLASK_APP=hello.py
    $ python -m flask run
    

    screenshot 2018-11-05 at 11 07 58 pm

Top comments (12)

Collapse
 
lankjan007 profile image
Dmitri • Edited

Hi Sahil,

I just wanted to add an error I ran into while attempting this tutorial.

I received an error 'NameError: name 'name' is not defined'

after checking stackoverflow, I found a solution and changed (name) to (__name__) and this worked for me.

I hope this helps someone else with here,

Thanks!

Collapse
 
sahilrajput profile image
Sahil Rajput

Hi Dmitri,

I have written __ name __ but somehow it changed to 'name'.
Thank you for pointing out the mistake.

Collapse
 
sanbaner28 profile image
sanbaner28

I'm getting an Error here -

(venv) Sandips-MacBook-Pro:Sandip_TSConsole_Coral_Prototype_GIT sanbaner$ export FLASK_APP=hello.py
(venv) Sandips-MacBook-Pro:Sandip_TSConsole_Coral_Prototype_GIT sanbaner$
(venv) Sandips-MacBook-Pro:Sandip_TSConsole_Coral_Prototype_GIT sanbaner$ flask run

  • Serving Flask app "hello.py"
  • Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
  • Debug mode: off Usage: flask run [OPTIONS]

Error: Could not import "hello".
(venv) Sandips-MacBook-Pro:Sandip_TSConsole_Coral_Prototype_GIT sanbaner$

Collapse
 
sanbaner28 profile image
sanbaner28

Working fine now !

Collapse
 
aadyajha12 profile image
Aadya 😬🥰

hi how did u solve this? i get the same error as well.

Collapse
 
octavflorescu profile image
Octav Florescu
flask run --host=0.0.0.0

to see it in the network

Collapse
 
tekiegirl profile image
Jacqui Read

Thank you for this. You may wish to add the command for Windows CMD and Windows Powershell in the last section (as you have in the previous sections). I was using Powershell so had to look it up when I realised what you had put was for Unix.

Windows CMD:

set FLASK_APP=hello
flask run

Windows PowerShell:

$env:FLASK_APP = "hello"
flask run

Collapse
 
janative profile image
Janative

Hello,
When trying to do this: *venv\Scripts\activate * I get the error:
venv\Scripts\activate : File C:\Users\My\Desktop\CS50\project\venv\Scripts\Activate.ps1 cannot be loaded because running scripts is disabled on this system. For more
information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1

  • venv\Scripts\activate
  • ~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : SecurityError: (:) [], PSSecurityException
    • FullyQualifiedErrorId : UnauthorizedAccess

Pls, why is it disabled and am I safe to change execution policy?
Thank you.

Collapse
 
felpsknows profile image
Felipe Alvarado S

It works!

Collapse
 
dmahely profile image
Doaa Mahely

Hi Sahil, thanks for sharing!
Why is it necessary to create a virtual environment to run Flask?

Collapse
 
sahilrajput profile image
Sahil Rajput

It is a good approach to create virtual environment whenever you start a new project. So that no two libraries cause conflict with each other.

For example:
If one project uses Django==1.0.1 and other uses Django==3.0.0. so you have to create the virtual environment for both the project so that you can run them smoothly else one of them will cause problem

Collapse
 
celedonio2151 profile image
celedonio2151

When I want to install I get:
Requirement already satisfied: Flask in user / lib / python3 / dist-packages (1.1.2)
Help