From 678d56c1e2c9896b71e2861b292ab9f4236859bf Mon Sep 17 00:00:00 2001 From: Pedram Ashofteh-Ardakani Date: Sat, 22 Oct 2022 01:17:24 +0330 Subject: [PATCH] Docs: update sample application initiation in venv (#15506) * Docs: update sample application initiation in venv Until now, readers would install 'Flask' on their main system python environment, freeze their entire packages, and grep only the 'Flask' package! So they would be missing all the other dependencies in their requirements file. With this commit, a better practice is followed. First, create a clean virtual environment, then install 'Flask' and freeze everything into the requirements file. Also, I recommend using 'python3 -m pip freeze' instead of 'pip3 freeze' while in the virtual environment. This is because sometimes hidden quirks of 'pip' will ignore the actiavted environment and freeze the entire python packages in the system (e.g. in ArchLinux). * Docs: update python sample testing application In the previous commit, I forgot to update the 'Test the application' section so that the users activate the virtual environment first. This commit fixes that. Co-authored-by: Stefan Scherer --- language/python/build-images.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/language/python/build-images.md b/language/python/build-images.md index 5d4c138d8d..ec9f38a393 100644 --- a/language/python/build-images.md +++ b/language/python/build-images.md @@ -28,9 +28,11 @@ Let’s create a simple Python application using the Flask framework that we’l ```console $ cd /path/to/python-docker -$ pip3 install Flask -$ pip3 freeze | grep Flask >> requirements.txt -$ touch app.py +$ python3 -m venv .venv +$ source .venv/bin/activate +(.venv) $ python3 -m pip install Flask +(.venv) $ python3 -m pip freeze > requirements.txt +(.venv) $ touch app.py ``` Now, let’s add some code to handle simple web requests. Open this working directory in your favorite IDE and enter the following code into the `app.py` file. @@ -49,7 +51,9 @@ def hello_world(): Let’s start our application and make sure it’s running properly. Open your terminal and navigate to the working directory you created. ```console -$ python3 -m flask run +$ cd /path/to/python-docker +$ source .venv/bin/activate +(.venv) $ python3 -m flask run ``` To test that the application is working properly, open a new browser and navigate to `http://localhost:5000`.