Bootstrap FreeKB - Flask - Running Flask app in Python virtual environment
Flask - Running Flask app in Python virtual environment

Updated:   |  Flask articles

A Python virtual environment can be used to test your Flask app in an isolated environment. This should only be done for testing and development purposes. In production, the Flask app should run on a server, such as a uWSGI or gunicorn server.

In this example, a directory named my_virtual_environment will be created.

# Python version 2
python -m virtualenv my_virtual_environment 

# Python version 3
python3 -m venv my_virtual_environment 

 

Activate the virtual environment.

source my_virtual_environment/bin/activate

 

The pip list command should show there are only 2 packages in the Python virtual environment, pip and setuptools.

~]$ pip list
pip (9.0.3)
setuptools (39.2.0)

 

Let's use the pip install command to upgrade pip.

pip install --upgrade pip

 

And then use the pip install command to install Flask in your virtual environment.

pip install Flask

 

And now the pip list command should contain quite a few package.

~]$ pip list
click (8.0.4)
dataclasses (0.8)
Flask (2.0.3)
importlib-metadata (4.8.3)
itsdangerous (2.0.1)
Jinja2 (3.0.3)
MarkupSafe (2.0.1)
pip (9.0.3)
setuptools (39.2.0)
typing-extensions (4.1.1)
Werkzeug (2.0.3)
zipp (3.6.0)

 

Notice in this example that Flask version 2.0.3 was installed. When using a Flask version below 2.2 you'll want to set the FLASK_APP environment variable to contain the name of your main file, which is typically main.py.

In your virtual environment, there should be a file named "activate".

my_virtual_environment/bin/activate

 

I like to add the following to the bottom of the “activate” file so that I don’t need to export FLASK_APP.

export FLASK_APP=main.py

 

I also like to add unset FLASK_APP at the end of the deactivate function in the “activate” file so that when I’m done testing and I deactivate the Python virtual environment, the FLASK_APP variable gets unset.

deactivate() {
  . . .
  unset FLASK_APP
}

 

Let's say main.py contains the following. This super simple app should just return {"greeting":"Hello World"}

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
  return {"greeting": "Hello World"}

 

Now all you have to do is to use the flask run command to run your Flask app. By default, this will run your Flask app in the foreground. Also by default, the URL to submit a request to your Flask app will be http://127.0.0.1:5000/.

~]$ flask run
 * Serving Flask app 'app.py' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

 

Unlike Flask run on wUSGI or gunicorn, when running Flask in a virtual environment, the flask run command will not use the host and port in app.run. 

if __name__ == "__main__":
  app.run(host='10.11.12.13', port=12345)

 

So instead, you can go with command line options if you want to run the app on a specific host and port.

flask run --host 10.11.12.13 --port 12345

 

On the other hand, the python command will be able to use the host and port in app.run.

python app.py

 

Open up another SSH connection to the server you are running your Flask app on and submit a GET request to the URL (most likely http://127.0.0.1:5000/) and {"greeting":"Hello World"} should be returned.

~]$ curl --request GET --url http://127.0.0.1:5000/
{"greeting":"Hello World"}

 

Back in your Python virtual environment, press CTRL + C to stop your Flask app.

Last but not least, use the deactivate command to deactivate your Python virtual environment.

deactivate

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter a09f39 in the box below so that we can be sure you are a human.