Python Pip/Pipenv Explanation And Most Used Command
4 min readApr 9, 2024
pip
and pipenv
are both popular Python package management tools, but they serve slightly different purposes and operate in different ways. Here’s a breakdown of the differences:
pip
- Basic Package Installer:
pip
is the Python package installer and is part of the Python Packaging Authority (PyPA). It's used to install and manage Python packages from the Python Package Index (PyPI) and other package indexes. - Global and Local Installations: By default,
pip
installs packages globally (unless you use a virtual environment), which can lead to dependency conflicts between projects. - No Dependency Resolution: Until version 20.3,
pip
did not have a true dependency resolution system, which could lead to issues where installing or updating one package might break others. However, starting from version 20.3, pip introduced a new resolver that is much more robust and handles dependency resolution better.
pipenv
- Packaging Tool That Manages Environments:
pipenv
is designed to bring the best of all packaging worlds (bundlers, compendiums, etc.) to the Python world. It automatically creates and manages a virtual environment for your projects, as well as adds/removes packages from yourPipfile
as you install/uninstall packages. - Dependency Resolution and Locking:
pipenv
resolves dependencies for your project and locks the versions of these packages in aPipfile.lock
to ensure that the project is reproducible. This lock file includes the exact versions of dependencies that your project needs to run, which helps in avoiding the "works on my machine" problem. - Dev and Production Packages:
pipenv
allows you to differentiate between development and production dependencies in yourPipfile
, making it clear which packages are necessary for production and which are only needed for development purposes. - Graph Dependency Management:
pipenv
provides a command to visualize the project’s dependency graph, which can help in understanding the dependency relationships and resolving conflicts.
Here are the examples of Pipfile and Pipfile.lock:
Summary
- Use Case: Use
pip
for basic package installation and management. Usepipenv
for managing project-specific environments and dependencies with more complex needs such as dependencies resolution, environment separation, and production/development packages distinction. - Functionality:
pip
is more straightforward and manually managed, whereaspipenv
aims to provide an all-in-one solution for project environments and package management, making it easier to manage applications across different development and production environments.
In essence, while pip
can be used on its own for simple package management, pipenv
is preferred for more comprehensive dependency management and project environment isolation.
Pipenv Most Used Command
pipenv install --python version
Ex: pipenv install --python 3.12
Create a virtual environment with Python specific version
pipenv install package
pipenv install package==specific version
Install a package, solve the dependency, and update Pipfile.lock
pipenv shell
Activate the project's virtualenv
exit
Deactivate the virtual environment
pipenv lock
Update the virtual environment
pipenv run
run a command inside the virtualenv
pipenv --rm
Delete the virtual environment
pipenv requirements > requirements.txt
Export requirements as a text
pipenv install -r path/to/requirements.txt
Install packages using requirements.txt
Pip Most Used Command
pip install package
pip install package==specific version
Install a package
pip freeze
Display installed packages and version
FAQ
- What is the steps to create an virtual environment?
- Open the Terminal
- Navigate to your project folder
- Ensure the you already installed the required Python version. If not, you will receive an error when install virtual environments “Python your venrsion was not found on your system…”. There are several ways to install it:
- Using a Version Manager
- pyenv: This tool allows you to install multiple versions of Python and switch between them. To install
pyenv
, you can follow the instructions on the pyenv GitHub page. - asdf: Similar to
pyenv
,asdf
is a version manager capable of managing multiple runtime versions, including Python. Installation instructions can be found on the asdf website. - Direct Installation:
- Windows: You can download the Python installer from the official Python website.
- macOS: You can install Python 3.10 using Homebrew with the command
brew install python@3.10
. - Linux: The method will vary depending on your distribution, but you can generally install Python using your package manager (e.g.,
sudo apt-get install python3.10
for Ubuntu). - Use pipenv install — python version to create a virtual environment
- Why pipenv return an error “RuntimeError: Failed to lock Pipfile.lock”
pipven is project / folder based environment, so it may cause an error if the project already has a virtual environment. The solution is to remove the existing then create again. - Why the error “ModuleNotFoundError: No module named ‘pkg_resources’” happen when importing a module?
Try to activate the virtual environment and run pip install — upgrade setuptools