Chat Zalo Chat Messenger Phone Number Đăng nhập
Install Flask {Linux, Windows, & MacOS Guide} | phoenixNAP KB

Install Flask {Linux, Windows, & MacOS Guide} | phoenixNAP KB

Introduction

Flask is one of the most popular web application frameworks written in Python. It is a microframework designed for easy and quick start. The extension with tools and libraries adds more functionality to Flask for more complex projects.

This article explains how to install Flask in a virtual test environment and create a simple Flask application.

Prerequisites

  • Installed Python 2.7 or Python 3.5 and later CLI
  • with

  • administrator privileges

Step 1:

Install

Virtual Environment Install Flask in a virtual environment to avoid problems with conflicting libraries. Check the Python version before you begin:

  • Python 3 comes with a virtual environment module called venv pre-installed. If you have Python 3 installed, go to step 2.
  • Python 2 users must install the virtualenv module. If you have Python 2, follow the instructions in step 1.

Install virtualenv on Linux

Package managers on Linux provide virtualenv

.

  • For Debian/Ubuntu:

1. Start by opening the Linux terminal.

2. Use apt to install virtualenv

on Debian, Ubuntu and other related distributions: sudo apt install python-virtualenv

  • For CentOS / Fedora / Red Hat:

1. Open the Linux terminal.

2. Use yum to install virtualenv on

CentOS, Red Hat, Fedora and related distributions: sudo yum

install python-virtualenv

Install virtualenv on MacOS

1. Open the terminal.

2. Install virtualenv on Mac using pip: sudo python2 -m

pip install virtualenv Install virtualenv on

Windows

1. Open the command line with administrator privileges.

2. Use pip to install virtualenv on Windows: py -2 -m

pip install virtualenv

Step 2: Create an environment

1. Create a separate directory for your project:

mkdir <project name>

2. Go to directory:

cd <project name>

3. Within the directory, create the virtual environment for Flask. When you create the environment, a new folder appears in the project directory with the name of the environment.

Create an environment

on Linux and MacOS

For Python 3:To create a virtual environment for Python 3, use the venv module and name it: python3 -m venv <environment name> For

  • Python 2:For Python 2

, use the virtualenv module to create a virtual environment and name it

  • :

python -m

virtualenv <environment name

>

Enumerating the directory structure with the ls command displays the newly created

environment: Create an environment in

Windows

For Python 3:

Create and name a

virtual environment in Python 3 with: py -3 -m venv <environment name> For Python 2:

  • For Python 2,

create the

virtual environment with the

virtualenv module

  • :

py -2 -m virtualenv <environment name>

List the folder structure using the dir command

: dir *<project name>*

The project directory displays the newly created

environment:

Step 3:

Activate the environment

Activate the virtual environment before installing Flask. The name of the activated environment appears in the CLI after activation.

Activate the environment on Linux and MacOS

Activate the virtual environment on Linux and MacOS with:

.

<environment name>/bin/activate Activate your environment in Windows

For Windows,

activate the virtual environment with: <environment name>Scriptsactivate Step 4: Install Flask Install Flask

within the activated environment using pip: pip install Flask Flask is

automatically installed with all dependencies.

Step 5: Test your environment

of development 1. Create a simple Flask application to test the newly created development environment.

2. Create a file in the Flask project folder named hello.py.

3. Edit the file using a text editor and add the following code to create an application that prints

“Hello world!”: from flask import Flask app = Flask(__name__) @app.route(‘/’) def hello_world(): return ‘Hello world!’

4. Save the file and close it.

5. Using the console, navigate to the project folder using the cd command.

6. Set the environment variable FLASK_APP.

For

  • Linux and Mac

:export FLASK_APP=hello.py

  • For Windows:

setx FLASK_APP “hello.py”

7. Run the Flask application with: flask

run

The output prints a confirmation message and the address

.

8. Copy and paste the address into the browser to see the project running:

Conclusion

Flask’s web apps are easy to set up 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.

Contact US