Introduction
Python projects often need different package versions. A virtual environment gives one project its own Python interpreter and package directory, so installing a package for one project does not change the global Python environment or another project.
In this lab, you will create a virtual environment with the built-in venv module, activate it, install a package inside it, and create a second environment to confirm that the environments are isolated from each other. All work happens in /home/labex/project.
Create a Project Virtual Environment
In this step, you will create a virtual environment for a Python project. A virtual environment is just a directory that contains a Python executable, activation scripts, and its own package installation area.
First move into the project workspace. Keeping the lab files under /home/labex/project makes the environment easy to find and keeps your work separate from system files.
cd /home/labex/project
Before creating the environment, check the Python version available on the system. The python3 --version command prints the interpreter version that venv will use.
python3 --version
You should see Python 3 output similar to this:
Python 3...
Now create a virtual environment named project-env. The -m venv option tells Python to run its built-in venv module as a command-line tool, and project-env is the directory that will hold the environment.
python3 -m venv project-env
List the new directory to see the structure created by venv.
ls project-env
The exact entries can vary slightly, but you should see files and folders such as these:
bin
include
lib
pyvenv.cfg
The pyvenv.cfg file records how the environment was created. Inspect it now.
cat project-env/pyvenv.cfg
Look for include-system-site-packages = false. That setting means this environment does not automatically share globally installed Python packages.
Activate the Environment and Install a Package
In this step, you will activate project-env and install a package inside it. Activation changes shell environment variables so that commands such as python and pip point to the virtual environment first.
Make sure you are still in the project workspace.
cd /home/labex/project
Use source to run the activation script in your current shell. This matters because a child script cannot change the parent shell, but source can update the shell you are typing in.
source project-env/bin/activate
After activation, the shell prompt usually starts with (project-env). Confirm that python now points inside the virtual environment by printing two paths from Python. sys.prefix is the active environment, while sys.base_prefix is the base Python installation.
python -c "import sys; print(sys.prefix); print(sys.base_prefix)"
The first line should be your virtual environment, and the second line should be the system Python location:
/home/labex/project/project-env
/usr...
Install a small package named colorama into the active environment. Running python -m pip uses the pip that belongs to the currently active Python interpreter.
python -m pip install colorama==0.4.6
When the installation finishes, pip should report that colorama was installed successfully.
Verify the package from Python by importing it and printing its version.
python -c "import colorama; print(colorama.__version__)"
The output should be:
0.4.6
You can leave the virtual environment with deactivate. This returns python and pip lookup behavior to the normal shell environment.
deactivate
Create a Second Isolated Environment
In this step, you will create another virtual environment and compare it with project-env. Separate environments can use the same base Python interpreter while keeping their installed packages separate.
Return to the project workspace.
cd /home/labex/project
Find the system python3 executable. The command -v command prints the path that the shell will run for a command name.
command -v python3
The path is usually:
/usr/bin/python3
Create a second environment named tools-env. The --prompt tools-env option sets the activation prompt text, which is helpful when you work with several environments.
python3 -m venv --prompt tools-env tools-env
Now compare the two environment prefixes. Each command runs the Python executable inside a different virtual environment and prints its active prefix.
project-env/bin/python -c "import sys; print(sys.prefix)"
tools-env/bin/python -c "import sys; print(sys.prefix)"
You should see two different directories:
/home/labex/project/project-env
/home/labex/project/tools-env
Finally, check whether colorama is available in tools-env. You installed colorama only in project-env, so the second environment should not find it.
tools-env/bin/python -c "import importlib.util; print(importlib.util.find_spec('colorama'))"
The expected output is:
None
This confirms that packages installed in one virtual environment do not automatically appear in another virtual environment.
Summary
You created a Python virtual environment, inspected its configuration, activated it, installed a package inside it, and created a second environment to confirm package isolation. You also practiced using python3 -m venv, source, python -m pip, and direct environment Python paths to understand which interpreter is running.



