Activity 18: Research Virtual Environment and Python package manager
A Python Virtual Environment is an isolated space where you can work on your Python projects, separately from your system-installed Python. You can set up your own libraries and dependencies without affecting the system Python. We will use virtualenv or venv to create a virtual environment in Python.
What is a Virtual Environment?
Python's official documentation says:
"A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system"
In the realm of Python development, virtual environments have become an indispensable tool for managing project dependencies and isolating package installations. In this article, we’ll delve into why virtual environments are crucial and explore the fastest method to set them up.
Why Virtual Environments?
Imagine you’re working on multiple Python projects simultaneously, each requiring different versions of libraries or even Python itself. Without virtual environments, managing these dependencies can quickly become chaotic. Here’s where virtual environments come to the rescue:
Isolation: Virtual environments provide isolated environments for each project, ensuring that the dependencies for one project don’t interfere with those of another.
Dependency Management: They allow you to install, upgrade, or remove packages without affecting the global Python installation, making it easier to manage project-specific dependencies.
Portability: Virtual environments encapsulate all dependencies within a project folder, making it easy to share projects with others without worrying about conflicting dependencies.
Packages: Assembling the Furniture
When these modules are assembled together, they form a complete piece of furniture, akin to a chair or a table. This assembly of modules into a cohesive unit is similar to how packages in Python bring together related modules to provide broader functionality. A package is a collection of Python modules grouped together in a directory, which also contains a special __init__.py
file.
For example, a package for data processing might look like this:
You can use this package in your project by importing the necessary modules:
from data_processing import data_cleaning, data_visualization
cleaned_data = data_cleaning.clean(raw_data)
data_visualization.plot(cleaned_data)
Packages help organize modules into coherent groups, making it easier to manage and maintain your codebase.
Virtual Environments: The Room for Your Furniture
Now, imagine a room furnished with different pieces of furniture like tables, chairs, TVs, and beds. This room represents a virtual environment in Python. It’s a contained space where various packages (furniture sets) coexist without interfering with each other. Each room (virtual environment) can have its own unique combination of furniture (packages), tailored to specific needs or projects, ensuring a clean and isolated development space.
A Python virtual environment is a self-contained directory tree containing a specific Python installation, along with additional packages and dependencies. These environments are isolated from one another, meaning changes in one virtual environment do not affect others or the system-wide Python environment. This isolation is crucial for managing dependencies effectively and preventing conflicts between projects.
Why Use Virtual Environments?
Simply put, a virtual environment is a tool that allows you to create isolated environments for your Python projects. Each virtual environment acts as a sandbox, with its own set of packages and dependencies. This means you can install different versions of packages for different projects without worrying about conflicts.
In the world of Python development, managing dependencies can become a bit of a headache. Imagine you have multiple Python projects running on your system, each requiring different versions of the same packages. For instance, one project might need Pandas 1.5.0, while another requires Pandas 2.0.2. Managing these dependencies on a system-wide level could lead to conflicts and compatibility issues.
By using virtual environments, you can package each of these applications into separate environments, ensuring that they have their own space to run without interfering with each other. This isolation guarantees that each application can use the specific versions of packages it requires, allowing them to run smoothly without conflicts.
Creating a Python Virtual Environment
There are two primary tools available for creating virtual environments: virtualenv and venv. These tools are largely interchangeable, but they have slight differences in their compatibility and installation methods. Virtualenv supports older versions of Python and must be installed using the pip command. On the other hand, venv is integrated into Python 3.3 and above as part of the standard library, eliminating the need for separate installation.
Let’s walk through the process of creating and using a Python virtual environment. Having Python installed on your PC is a prerequisite.
1. Create a Project Folder: First, make a project folder to contain your project files and virtual environment.
$ mkdir my_project
2. Create a Virtual Environment:
Use the venv
module to create a virtual environment inside your project folder.
$ python3 -m venv my_project/venv
Here, my_project/venv
is the path where the virtual environment will be created. The venv
module is included in the Python standard library for Python 3.3 and higher.
3. Activate the Virtual Environment: To activate the virtual environment, run the following command:
$ my_project/venv/bin/activate
After activation, the virtual environment’s name will appear in parentheses at the start of the terminal prompt, indicating that you are now working inside the virtual environment. eg : (venv)
4. Check Python Version:
Ensure that the Python version used in the virtual environment is the same as the version used to create it.
(venv) $ python --version
Python 3.10.1
NOTE: It’s important to distinguish between a Python project folder and a virtual environment folder. Your Python project folder holds the source code for your project, while the virtual environment folder contains the Python interpreter, packages, and tools like pip. To maintain clarity and organization, it’s best practice to keep these two separate and never mix your project files within the virtual environment folder.
Installing Packages in a Virtual Environment
Inside a virtual environment, you can install packages using pip
, which is the package installer for Python. Let's check the pre-installed packages and upgrade pip
.
1. Check Pre-installed Packages:
(venv) $ pip list
You should see pip
and setuptools
listed.
2. Upgrade pip
:
Upgrade pip
to the latest version.
(venv) $ pip install --upgrade pip
3. Install a Package:
Let’s install the pandas
package inside the virtual environment.
(venv) $ pip install pandas
To install a specific version of a package, specify the version number.
(venv) $ pip install pandas==1.1.1
You can also install a range of versions using comparison operators.
(venv) $ pip install 'pandas<1.2'
Reproducing a Python Virtual Environment
It’s common to reproduce a virtual environment to ensure consistency across different development environments or team members. Here’s how you can do it.
1. Freeze Dependencies:
Generate a list of installed packages and their versions using pip freeze
.
(venv) $ pip freeze > requirements.txt
This command creates a requirements.txt
file containing all the packages and their exact versions.
2. Share requirements.txt
:
Distribute the requirements.txt
file to your team members. They can use it to recreate the same virtual environment.
3. Recreate the Virtual Environment:
Your colleague can create a new virtual environment and install the packages listed in requirements.txt
.
$ python3 -m venv new_project/venv
$ new_project/venv/bin/activate
(venv) $ pip install -r requirements.txt
Deactivating and Deleting a Virtual Environment
Once you are done working with a virtual environment, you can deactivate it or delete it.
1. Deactivate the Virtual Environment:
(venv) $ deactivate
This command returns you to the system-wide Python environment.
2. Delete the Virtual Environment:
To delete a virtual environment, simply remove its directory.
$ rm -rf my_project/venv
Python virtual environments are essential for managing dependencies and preventing conflicts between projects. By creating isolated environments for each project, you can ensure that each application has the specific packages and versions it needs to run smoothly. Remember to always set up a separate virtual environment for each Python project and install all required dependencies inside it. This practice will save you from many headaches and make your development process much more efficient.
Using the furniture analogy, we can understand virtual environments as rooms that keep our different sets of furniture (packages) separate and organized, ensuring they don’t interfere with each other. This isolation is key to maintaining clean and efficient development environments, allowing you to focus on building great software without worrying about dependency conflicts.
Here’s a formal way in creating a python virtual environment in my terminal:
STEP 1: Create a folder and name it
STEP 2: Open the folder and at the top, type “cmd
” to launch it on terminal
STEP 2: On terminal, create a python virtual environment
Use this command to create a virtual environment:
python -m venv myvenv
venv - means virtual environment and,
myvenv - means the name of the virtual environment to be created
The command python -m venv myvenv
creates a new virtual environment in a folder named "myvenv" inside your project directory. This environment will have its own Python interpreter and libraries.
Verified the myvenv
folder in the project folder which is named “virtual-environment
“
STEP 3: Activate the python virtual environment
use this command to activate the python virtual environment:
myvenv\Scripts\activate
Once activated, the terminal prompt changes to show the name of the environment (in this case, (myvenv)
), indicating that you are now working within the virtual environment.
STEP 4: Install dependencies
on this environment I’ve install flask
use this command to install dependencies:
pip install flask
on the command above shows flask as the dependency going to be installed
which is it’ll be like this:
pip install name_of_dependency
STEP 4: Add “myvenv
” on .gitignore
so, adding the myvenv
directory to .gitignore
is essential to prevent the virtual environment files from being tracked by version control tools like Git. Since the myvenv
directory contains environment-specific files that are not necessary for the application’s source code, it is a good practice to exclude it from version control to keep the repository clean and reduce clutter.
followed the steps on how to add myvenv
to the .gitignore
file
nul > .gitignore
After installing a package or dependency we need to always export it to a file so that if we needed for another project we could simply import it. Below are the instructions on how to import and export the file into “requirements.txt
”
Export Installed Packages to a File:
You can export a list of all installed packages along with their versions to a requirements file using:
pip freeze > requirements.txt
Install Dependencies from a File:
If you have a requirements file generated by pip freeze
, you can install all the dependencies listed in it using:
pip install -r requirements.txt
Open the .gitignore
file with a text editor or notepad.
- Add the following line below:
myvenv/
save the file:
Now we can directly open it in pycharm or any application for us to create the project.
reference:
https://www.freecodecamp.org/news/how-to-setup-virtual-environments-in-python/
https://realpython.com/python-virtual-environments-a-primer/
https://arsanatl.medium.com/understanding-python-virtual-environments-an-in-depth-guide-6ffddab02498
https://medium.com/@KiranMohan27/how-to-create-a-virtual-environment-in-python-be4069ad1efa