Dependency Management and Virtual Environments in Python: pip, venv, virtualenv, pipenv (Updated for 2025)
Python has established itself as a powerful and flexible tool for developing projects of varying complexity. One of the key advantages of this programming language is its extensive ecosystem of libraries and tools. To effectively manage these libraries, it is necessary to master working with pip and virtual environments. In this article, we will explore in detail how to use pip to install packages and how to create isolated virtual environments using venv, virtualenv, and pipenv, which will optimize the development process and avoid dependency conflicts.
Using pip to Manage Python Packages
pip (Python Package Installer) is the standard package manager in Python, designed to install and manage third-party libraries available in the official PyPI (Python Package Index) repository. It allows you to easily add, update, and remove packages needed for your project.
Checking pip Installation
To make sure pip is installed on your system, open a terminal or command prompt and run the following command:
pip --version
If pip is installed, you will see information about its version and location. For example:
pip 24.0 from /usr/local/lib/python3.12/site-packages/pip
If pip is not present, you can install it by running the following command:
python -m ensurepip --upgrade
Basic pip Commands
Installing a Package:
pip install package_name
Example:
pip install requests
This command will install the latest version of the requests package from PyPI.
Installing a Specific Version of a Package:
pip install package_name==version_number
Example:
pip install pandas==2.2.0
This command will install version 2.2.0 of the pandas library.
Updating a Package to the Latest Version:
pip install --upgrade package_name
Example:
pip install --upgrade numpy
This command will update the numpy package to the newest version.
Uninstalling a Package:
pip uninstall package_name
Example:
pip uninstall matplotlib
This command will remove the matplotlib package from your system.
Viewing Installed Packages:
pip list
This command will display a list of all installed packages in your current Python environment.
Installing Multiple Packages at Once
To install multiple packages simultaneously, it is convenient to use a requirements.txt file, which lists all the necessary libraries and their versions.
Create a requirements.txt file with the following format:
requests==2.32.0
flask==3.0.0
pandas==2.2.0
Then execute the command:
pip install -r requirements.txt
This command will install all the packages specified in the requirements.txt file.
Saving a List of Installed Packages
To fix the current dependencies of the project, you can create a requirements.txt file with a list of all installed packages and their versions:
pip freeze > requirements.txt
Subsequently, to restore the environment, you can use this file:
pip install -r requirements.txt
Creating and Using Virtual Environments (venv)
Using virtual environments is a recommended practice when developing in Python. Virtual environments allow you to isolate the dependencies of different projects from each other, which prevents library version conflicts.
What is a Virtual Environment?
A virtual environment is an isolated environment that contains its own copy of the Python interpreter and a set of installed packages. This means that packages installed in the virtual environment do not affect the global Python installation or other projects.
Creating a Virtual Environment with venv
venv is a built-in Python module designed to create virtual environments.
Navigate to the project directory:
cd /path/to/project
Create a virtual environment:
python -m venv venv
Here, venv is the name of the folder where the virtual environment files will be stored. You can use any other name, but venv is commonly used.
Activating the Virtual Environment:
On Windows:
venv\Scripts\activate
On macOS and Linux:
source venv/bin/activate
After activation, a prefix with the environment name will appear in the command line:
(venv) user@machine:~/project$
This means that all packages will now be installed only in this environment.
Installing Packages in the Virtual Environment:
pip install flask
Checking Installed Packages in the Environment:
pip list
Deactivating the Virtual Environment
To exit the virtual environment, execute the command:
deactivate
After that, you will return to the system Python interpreter.
Benefits of Using Virtual Environments
- Project Isolation: Different projects may require different versions of libraries. Virtual environments prevent conflicts between these versions.
- Dependency Management: Using
requirements.txtand virtual environments simplifies the portability of projects between different machines. - Security: You can experiment with new libraries without risking damage to the global Python installation.
Tips for Working with Virtual Environments
- Create a virtual environment immediately after creating a new project.
- Store the
requirements.txtfile in the root directory of the project. - Use
.gitignoreto exclude thevenvfolder from the version control system (for example, Git). - In the IDE (for example, VS Code, PyCharm), configure automatic use of the virtual environment in the project.
Alternative Tools: virtualenv and pipenv
In addition to the built-in venv module, there are other tools for managing virtual environments and dependencies: virtualenv and pipenv.
Using virtualenv
What is virtualenv?
virtualenv is a third-party library that provides advanced features for creating and managing virtual environments. Unlike venv, virtualenv can be used with older versions of Python (before 3.3).
Installing virtualenv
pip install virtualenv
Creating a Virtual Environment
virtualenv env_name
Example:
virtualenv venv
Activating the Environment
On Windows:
venv\Scripts\activate
On macOS and Linux:
source venv/bin/activate
Deactivating the Environment
deactivate
Difference Between venv and virtualenv
| Parameter | venv (Built-in) | virtualenv (External) |
|---|---|---|
| Version Support | Python 3.3+ | Any Python Version |
| Functionality | Basic | Advanced |
| Installation | Built-in | Requires Installation |
virtualenv is often used in older projects or when needing support for different Python versions.
Using pipenv
What is pipenv?
pipenv is a tool that combines the management of virtual environments and dependencies. It automatically creates a virtual environment and uses the Pipfile and Pipfile.lock files to fix dependencies, making the project more structured and manageable.
Installing pipenv
pip install pipenv
Creating an Environment and Installing a Package
pipenv install requests
This command will automatically create a virtual environment and install the requests library.
Activating the Environment
pipenv shell
Exiting the Environment
exit
Removing the Environment
pipenv --rm
Benefits of Using pipenv
- Dependency management through a convenient and readable
Pipfile. - Secure fixation of dependency versions through
Pipfile.lock. - Automatic creation of virtual environments.
- Support for the
pipenv checkcommand to search for known vulnerabilities in dependencies.
Pipfile Example
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[packages]
requests = "*"
flask = "==3.0.0"
[dev-packages]
pytest = "*"
[requires]
python_version = "3.12"
For team development:
pipenv install
All dependencies will be installed automatically in the required versions.
Which Tool to Choose?
| Tool | Simple Project | Large Projects | Compatibility |
|---|---|---|---|
| venv | ✅ | ⚠️ Limited | Python 3.3+ |
| virtualenv | ✅ | ✅ | Any Version |
| pipenv | ✅ | ✅ Recommended | Python 3.6+ |
- For small projects without complex dependency management, use
venv. - For old projects or supporting multiple Python versions, choose
virtualenv. - For modern projects with team development,
pipenvis ideal.
Conclusion
Effective environment and dependency management is an important skill for every Python developer.
Key points to remember:
- Use
pipto install packages and manage dependencies. - Create virtual environments with
venv,virtualenv, orpipenvto isolate projects and avoid library conflicts. - When working in a team, use
pipenv.
The Future of AI in Mathematics and Everyday Life: How Intelligent Agents Are Already Changing the Game
Experts warned about the risks of fake charity with AI
In Russia, universal AI-agent for robots and industrial processes was developed