DEV Community

Install Flask and create your first web application

Sahil Rajput on November 05, 2018

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...
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