SoFunction
Updated on 2024-11-15

Python venv Virtual Environment Configuration Process Explained

I. Creating a virtual environment

python -m venv env

Create a virtual environment called env by executing the command. After the command is executed, an env folder appears, which is a brand new virtual environment containing the python parser specific to this project.

Note: The pre-installed python3 under ubuntu does not have the venv package under the standard library, you need to install it manually by executing the following command.

sudo apt install python3-venv

Open the env directory using vscode to see the following structure:

II. Use of virtual environments

Use the following command to "activate" the virtual environment in a Windows environment:

.\Scripts\activate

You can see that the name of the virtual environment (env) has appeared in front of the command prompt

If you are using the ubuntu operating system, the command is:

source ./bin/active

Install flask.

pip install flask

Use pip freeze to see what packages are installed in your virtual environment:

As you can see, pip installs not only Flask itself, but also all its dependencies.

III. Setting up vscode

The shortcut ctrl+shift+p opens the Commands panel and selects Python:Select Interpreter.

Select under Scripts and the configuration file .vscode/ will be automatically generated when you are done:.

{ "": "Scripts\\"}

This time ctrl+` opens the terminal and you can see that you have automatically entered the env virtual environment:

Create a project to test it out, create a new.

from flask import Flaskapp = Flask(__name__)@('/')def index(): return '<h1>Hello world!</h1>'

If you don't know the commands you can run flask --help to get help.

Follow the prompts and execute the following command to run the flask service:

set FLASK_APP= run

Take care here that you don't habitually put spaces on either side of the "=", it can cause problems.

After starting the service open your browser and type http://localhost:5000

This is the whole content of this article.