No results found
We couldn't find anything using that term, please try searching for something else.
Introduction Flask is one of the most popular web application frameworks written in Python. It is a microframework designed for an easy andquick sta
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
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.
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 .
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
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
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 ]
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 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.
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:
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.
For Windows, activate the virtual environment with:
[environment_name]\Scripts\activate
The output shows test, the environment name, in the next line.
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 .
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.