Creating and Activating a Virtual Environment
First, let's create an isolated environment for a sample project. Using a virtual environment ensures that the packages for this project do not conflict with packages for other projects or the system-wide Python installation. We will use the venv module, which is the standard tool included with Python.
The setup script has already created a project directory for us at /home/labex/project/my_web_app. Let's navigate into it.
cd my_web_app
Now, create a virtual environment named venv inside this directory. It's a common convention to name the environment directory venv.
python3 -m venv venv
This command creates a venv directory containing a new Python installation. You can see its contents by running:
ls -F venv/
You should see subdirectories like bin/, include/, lib/, and a configuration file.
bin/ include/ lib/ lib64@ pyvenv.cfg
To start using this environment, you need to "activate" it.
source venv/bin/activate
After running this command, you'll notice that your shell prompt changes to include (venv), indicating that the virtual environment is active. Any Python or pip commands you run now will use the executables and packages within this environment.
You can verify this by checking the path to the Python executable:
which python
The output will point to the Python binary inside your venv directory.
/home/labex/project/my_web_app/venv/bin/python