Introduction
In this lab, you will learn the essential practices for managing Python virtual environments. We will cover how to create an isolated environment for a project, install specific dependencies, remove unnecessary packages, and ultimately, delete the environment to free up disk space. These skills are fundamental for maintaining a clean and organized development workflow.
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
Installing and Isolating Dependencies
With the virtual environment active, we can now install the project's dependencies. A requirements.txt file was created for you, which lists the necessary packages.
Install the packages using pip:
pip install -r requirements.txt
Pip will download and install flask and requests and their dependencies into the venv.
To see the packages installed specifically in this environment, use pip list:
pip list
You will see flask, requests, and other packages that were just installed.
Package Version
------------------ --------
certifi ...
charset-normalizer ...
click ...
Flask 2.2.2
idna ...
itsdangerous ...
Jinja2 ...
MarkupSafe ...
pip ...
requests 2.28.1
setuptools ...
urllib3 ...
Werkzeug ...
Now, let's see the power of isolation. Deactivate the environment to return to your system's global context.
deactivate
The (venv) prefix in your prompt will disappear. Now, run pip list again.
pip list
You will notice that flask and requests are no longer in the list, because they were installed only inside the virtual environment, keeping your global Python environment clean.
Removing Unused Packages
As a project evolves, some dependencies may become obsolete. It's good practice to remove them to keep the environment lean and to update your requirements.txt file.
First, re-activate the virtual environment to manage its packages.
source venv/bin/activate
Let's assume our project no longer needs the requests package. We can uninstall it using pip. The -y flag automatically confirms the uninstallation.
pip uninstall -y requests
Verify that the package has been removed by listing the installed packages again.
pip list
You will see that requests and its dependencies (like urllib3, certifi, etc., if they are no longer needed by any other package) are gone.
After removing a package, you should update your requirements.txt file to reflect the current state of the environment. You can do this by "freezing" the current package list into the file.
pip freeze > requirements.txt
You can view the updated file to confirm the change:
cat requirements.txt
The file will now only list flask and its dependencies.
click==...
Flask==2.2.2
itsdangerous==...
Jinja2==...
MarkupSafe==...
Werkzeug==...
Finally, let's deactivate the environment for now.
deactivate
Deleting the Entire Virtual Environment
When a project is complete, or if you want to start over with a fresh environment, the simplest cleanup method is to delete the entire virtual environment directory.
First, ensure you are not inside the my_web_app directory, as it's generally not a good idea to delete a directory you are currently in. Let's move to the parent directory.
cd /home/labex/project
Before deleting it, you can check how much disk space the virtual environment is using with the du (disk usage) command.
du -sh my_web_app/venv
The output will show the total size of the directory.
30M my_web_app/venv
Now, remove the entire venv directory. The rm -rf command recursively and forcefully removes the directory and all its contents. Be careful with this command.
rm -rf my_web_app/venv
Verify that the directory is gone by listing the contents of my_web_app.
ls my_web_app
You will see that only the requirements.txt file remains. The virtual environment has been completely cleaned up.
requirements.txt
Summary
In this lab, you have learned the complete lifecycle of managing a Python virtual environment. You practiced creating an environment with venv, activating it, installing dependencies from a requirements.txt file, observing package isolation, removing unneeded packages, and finally, cleaning up by deleting the entire environment directory. These are crucial skills for any Python developer to maintain clean, reproducible, and conflict-free project setups.



