{"id":61477,"date":"2024-03-30T11:42:17","date_gmt":"2024-03-30T11:42:17","guid":{"rendered":"https:\/\/www.askpython.com\/?p=61477"},"modified":"2025-04-10T20:32:11","modified_gmt":"2025-04-10T20:32:11","slug":"run-flask-app-python3","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/flask\/run-flask-app-python3","title":{"rendered":"Running a Flask Application with Python3: A Step-by-Step Guide"},"content":{"rendered":"\n<p>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. <\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>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. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>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.<\/em><\/p>\n<\/blockquote>\n\n\n\n<p>In this tutorial, we will understand how to run a simple flask application using Python3 and not Python.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/flask\/flask-postgresql\" data-type=\"post\" data-id=\"8689\">Flask with SQLAlchemy Tutorial<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Understanding the Difference Between Python and Python3<\/strong><\/h2>\n\n\n\n<p>When we talk about different versions of Python, Python2.x is considered a legacy, and the new version of Python &#8211; Python3.x is considered a future. Although the word <code>python<\/code> is used as an alias or a symlink to the existing Python version.<\/p>\n\n\n\n<p>Earlier, the word <code>python<\/code> 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. <\/p>\n\n\n\n<p>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&#8217;s configuration. <\/p>\n\n\n\n<p>Installing Python is a direct process and easy to follow. Python can be installed with the help of the link in the references.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Symlink &#8211;  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.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Steps to Run a Flask Application with Python3<\/strong><\/h2>\n\n\n\n<p>To run a flask application using python3, we need to follow a set of steps. Let us understand each of these steps. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating and Activating a Virtual Environment<\/strong><\/h3>\n\n\n\n<p>The first step is to create a virtual environment so there won&#8217;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. <\/p>\n\n\n\n<p>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. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npython3 -m venv myenv\n<\/pre><\/div>\n\n\n<p>After creating the environment, we need to activate it using the following commands<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#for windows \nmyenv\\Scripts\\activate\n#for mac and linux\nsource myenv\/bin\/activate\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"144\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/image-122-1024x144.png\" alt=\"Create and Activate a Virtual Environment\" class=\"wp-image-61485\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/image-122-1024x144.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/image-122-300x42.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/image-122-768x108.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/image-122-1536x216.png 1536w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/image-122.png 1697w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Create and Activate a Virtual Environment<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Installing Flask in the Virtual Environment<\/strong><\/h3>\n\n\n\n<p>The green <code>myenv<\/code> before the path indicates that we are currently in the <a href=\"https:\/\/www.askpython.com\/python\/examples\/virtual-environments-in-python\" data-type=\"post\" data-id=\"11337\">virtual environment<\/a>. We need to install the flask library in the environment using the following command.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npython3 -m pip install Flask\n<\/pre><\/div>\n\n\n<p>Notice how we are using the alias <code>python3<\/code>. This ensures that the flask library in the Pthon3 environment. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Checking the Flask Version<\/strong><\/h3>\n\n\n\n<p>We can check the version of flask using the command:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nflask --version\n\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"623\" height=\"75\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/image-126.png\" alt=\"Version Check\" class=\"wp-image-61494\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/image-126.png 623w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/image-126-300x36.png 300w\" sizes=\"auto, (max-width: 623px) 100vw, 623px\" \/><figcaption class=\"wp-element-caption\">Version Check<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Writing a Hello World Flask Application<\/strong><\/h3>\n\n\n\n<p>It is now time to write a Hello World Program! I have created a Python file called <code>testt.py<\/code> for this demonstration, which has the following code snippet.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom flask import Flask \napp = Flask(__name__)\n@app.route(&#039;\/&#039;)\ndef hello():\n    return &#039;Hello World&#039;\napp.run(debug = True)\n<\/pre><\/div>\n\n\n<p>We have imported the Flask library and created a Flask object which is stored in a variable called <code>app<\/code>. The <code>app.route<\/code> decorator is used to specify the application&#8217;s endpoint, the localhost. Our HelloWorld program is deployed in the endpoint &#8211; <code>http:\/\/localhost:5000\/<\/code>. The <code>app.run<\/code> decorator is used to execute the program. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Running the Flask Application with Python3<\/strong><\/h3>\n\n\n\n<p>After this file is saved, in the terminal we can use the command to run the application<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npython3 testt.py\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1008\" height=\"183\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/image-124.png\" alt=\"Running a flask app using Python3\" class=\"wp-image-61488\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/image-124.png 1008w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/image-124-300x54.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/image-124-768x139.png 768w\" sizes=\"auto, (max-width: 1008px) 100vw, 1008px\" \/><figcaption class=\"wp-element-caption\">Running a flask app using Python3<\/figcaption><\/figure>\n\n\n\n<p>It is successfully deployed. We can see the following page if we enter the URL in the google chrome.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"429\" height=\"150\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/image-125.png\" alt=\"Endpoint Output\" class=\"wp-image-61490\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/image-125.png 429w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/image-125-300x105.png 300w\" sizes=\"auto, (max-width: 429px) 100vw, 429px\" \/><figcaption class=\"wp-element-caption\">Endpoint Output<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Summary<\/strong><\/h2>\n\n\n\n<p>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&#8217;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?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>References<\/strong><\/h2>\n\n\n\n<p><a href=\"https:\/\/www.python.org\/downloads\/\" target=\"_blank\" rel=\"noopener\">Python Installation Guide<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/flask.palletsprojects.com\/en\/3.0.x\/\" target=\"_blank\" rel=\"noopener\">Flask Documentation<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":55,"featured_media":63890,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[],"class_list":["post-61477","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-flask"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/61477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/55"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=61477"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/61477\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/63890"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=61477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=61477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=61477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}