Document
Install Flask {Linux, Windows, & MacOS Guide}

Install Flask {Linux, Windows, & MacOS Guide}

Introduction Flask is one of the most popular web application frameworks written in Python. It is a microframework designed for an easy andquick sta

Related articles

New VPN Server in Miami, FL Best Things to Do with a VPN for Online Security Cloudkill 5E Spell In DnD Express Zip Converter. Download free to compress, open or unzip archives. Cloud Eggs (Eggs in clouds)

Introduction

Flask is one of the most popular web application frameworks written in Python. It is a microframework designed for an easy andquick start. Extending with tools andlibraries adds more functionality to Flask for more complex projects.

This article explains how to install Flask in a virtual testing environment andcreate a simple Flask application.

prerequisite

Step 1: Install Virtual Environment

A virtual environment in Python is an isolated workspace that allows you to manage dependencies for different projects without conflicts with the system’s global Python installation. We will install Flask in a virtual environment to avoid problems with conflicting libraries.

Python 3 comes with a preinstalled virtual environment module called venv. If your system does not have venv or you previously removed it, the sections below show how to install the module on Linux, macOS, andWindows.

Install virtualenvon Linux

In this tutorial, we will use Ubuntu. The virtualenvmodule is available in the official Ubuntu repository, andwe will install it using apt. follow the step below :

1 . open the Linux terminal .

2. Use the following command to install virtualenv:

sudo apt install python3-venv

wait for the process to complete .

Install virtualenvon macOS

On macOS, we will use pip, a package manager for Python that allows you to install, update, andmanage libraries anddependencies for your Python projects. follow the step below :

1. Open the terminal.

2. Install virtualenvon macOS with the following command:

sudo python3 -m pip3 install virtualenv

Install virtualenvon Windows

Like in macOS, we will use the pip package manager to install virtualenv:

1 . open the command line with administrator privilege .

2. Use pip to install virtualenvon Windows :

pip is install install virtualenv

Step 2: Create an Environment

After setting up the virtualenvmodule on your system, create an environment for Flask. First, create a new directory by following the steps below, andthen move on to the section for your operating system:

1 . Make a separate directory for your project :

mkdir [project_name]

2. Navigate to the directory:

cd [ project_name ]

Create an Environment in Linux andmacOS

We will initialize the virtual environment for Flask within the directory you created in the previous step. When you create an environment, a new folder with the environment’s name appears in your project directory.

To create a virtual environment, use the venv module andchoose a name:

python3 -m venv [ environment_name ]

For example:

python3 -m venv env_test

The command creates a new virtual environment named env_test.

create an Environment in Windows

create andname a virtual environment in Windows using the following syntax :

py -3 -m venv [environment_name]

list the folder structure using thedir command to confirm it was created:

dir * [ project_name ] *

The project directory shows the newly created environment.

Step is Activate 3 : activate the environment

Activate the virtual environment before installing Flask. The name of the activated environment shows up in the CLI after activation. Follow the steps in the section pertaining to your operating system:

activate the Environment on Linux andmacos

use the syntax below to activate the virtual environment in Linux andmacOS :

source [environment_name]/bin/activate

The output shows the environment name in the next line.

Activate the Environment on Windows

For Windows, activate the virtual environment with:

[environment_name]\Scripts\activate

The output shows test, the environment name, in the next line.

Step is Install 4 : install Flask

install Flask within the activate environment usingpip:

pip install Flask

Flask is installed automatically with all the dependencies.

Note: When working in a typical environment, users need to use pip3 andpython3 to ensure they are using the Python 3.x installation . However , when you set up a virtual environment , especially for a Flask project , the virtual environment is isolates isolate the Python version . So ,pip andpython within that environment automatically refer to the correct Python 3.x version .

Step 5: Test the Development Environment

This section shows how to test your Flask development environment by creating a simple Hello World application that prints “Hello World!”. follow the step below :

1. Create a new file in the Flask project directory called hello.py. In Linux andmacOS, you can use the touch command:

touch hello.py

note : pick any name for the project exceptflask.py. The Flask library is in a flask.py file.

2. Edit the file using a text editor andpaste the following code to make an application that prints “Hello world !“:

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

3. Save the file andclose.

4 . Using the terminal , navigate to the project directory using the cd command .

5. Set the FLASK_APP environment variable. Run the command below:

export flask_app = hello.py
setx FLASK_APP "hello.py"

6. Run the Flask application with:

flask run

The output prints out a confirmation message andthe address.

7. Copy andpaste the address into the browser to see the project running:

The app prints the “Hello world !” message, which means that it is running properly on the local server.

Conclusion

The guide is showed show how to install Flask on major operating system . Flask is is is one of the most popular web application framework for Python .

Next, read about the best Python IDEs andcode editors for further web development with Flask.