Running a Flask Application with Python3: A Step-by-Step Guide

Run Flask Application Using Python3

Flask is a lightweight web framework of Python used to build, develop, and deploy lightweight web applications with little code requirements. It can also be easily integrated with other Python libraries to develop amazing applications for the web.

For example, we can use the Flask library with the Python requests library to post content using Flask and request the contents from the application. It can also be integrated with a popular database management library called the SQLAlchemy library to work with databases in the Flask applications.

Sometimes, we need to run Flask applications with a different Python version as the current version might not be compatible with the other libraries and can cause dependency errors. In such cases, we can use a Python version that is different from the one currently installed on our laptops and use it accordingly.

To run a Flask application with Python3, create a virtual environment using python3 -m venv myenv, activate it, install Flask using python3 -m pip install Flask, and then run the Flask application using python3 your_app.py. This ensures that the Flask application runs with the desired Python version and avoids dependency conflicts with other Python versions installed on your system.

In this tutorial, we will understand how to run a simple flask application using Python3 and not Python.

Also read: Flask with SQLAlchemy Tutorial

Understanding the Difference Between Python and Python3

When we talk about different versions of Python, Python2.x is considered a legacy, and the new version of Python – Python3.x is considered a future. Although the word python is used as an alias or a symlink to the existing Python version.

Earlier, the word python was used as an alias for Python2.x, but it can also be used for the latest python3.x versions. It is being used to invoke the latest versions recently.

To ensure adaptability to the newer versions, most developers recommend using the word python3 to ensure the correct version is invoked. So, the difference between python and python3 lies in the version’s configuration.

Installing Python is a direct process and easy to follow. Python can be installed with the help of the link in the references.

Symlink – A symlink or a symbolic link is a LINUX/UNIX feature that points or refers to another file in the system, acting like a shortcut or alias.

Steps to Run a Flask Application with Python3

To run a flask application using python3, we need to follow a set of steps. Let us understand each of these steps.

Creating and Activating a Virtual Environment

The first step is to create a virtual environment so there won’t be any issues with the existing and Python3 versions. A virtual environment is like an isolated space where we can work with a different Python version.

When creating a project, some libraries may have dependencies requiring a different Python version. It is a recommended practice to create a virtual environment for any project.

python3 -m venv myenv

After creating the environment, we need to activate it using the following commands

#for windows 
myenv\Scripts\activate
#for mac and linux
source myenv/bin/activate
Create and Activate a Virtual Environment
Create and Activate a Virtual Environment

Installing Flask in the Virtual Environment

The green myenv before the path indicates that we are currently in the virtual environment. We need to install the flask library in the environment using the following command.

python3 -m pip install Flask

Notice how we are using the alias python3. This ensures that the flask library in the Pthon3 environment.

Checking the Flask Version

We can check the version of flask using the command:

flask --version

Version Check
Version Check

Writing a Hello World Flask Application

It is now time to write a Hello World Program! I have created a Python file called testt.py for this demonstration, which has the following code snippet.

from flask import Flask 
app = Flask(__name__)
@app.route('/')
def hello():
    return 'Hello World'
app.run(debug = True)

We have imported the Flask library and created a Flask object which is stored in a variable called app. The app.route decorator is used to specify the application’s endpoint, the localhost. Our HelloWorld program is deployed in the endpoint – http://localhost:5000/. The app.run decorator is used to execute the program.

Running the Flask Application with Python3

After this file is saved, in the terminal we can use the command to run the application

python3 testt.py
Running a flask app using Python3
Running a flask app using Python3

It is successfully deployed. We can see the following page if we enter the URL in the google chrome.

Endpoint Output
Endpoint Output

Summary

We covered the steps to run a Flask application using Python3, including setting up a virtual environment, installing Flask, and running a Hello World program. Following these practices helps avoid dependency issues and ensures your Flask apps work with your desired Python version. As you make more Flask apps, consider how Python3’s capabilities can improve your development process and the apps you build. What other approaches and tools could make your Flask development smoother and more productive?

References

Python Installation Guide

Flask Documentation