Using Python's pip to Manage Your Projects' Dependencies
by:
blow post content copied from Real Python
click here to view original post
pip
is the standard package manager for Python, used to install and manage libraries that aren’t part of the Python standard library. You use pip
to manage dependencies and install packages from the Python Package Index (PyPI).
You can verify if you have pip
by using commands like where pip3
on Windows or which pip3
on Linux and macOS. To install packages listed in a requirements.txt
file, use the command pip install -r requirements.txt
. This ensures your environment replicates the specified dependencies, maintaining consistency across different setups.
By the end of this tutorial, you’ll understand that:
pip
stands for “pip installs packages”, indicating its primary function.pip
manages Python packages that aren’t part of the standard library.- You should use
pip
whenever you need external Python packages for your projects. - You can install and uninstall packages with
pip
. - You use requirements files to manage projects’ dependencies.
You can do a lot with pip
, but the Python community is very active and has created some neat alternatives to pip
. You’ll learn about those later in this tutorial.
Get Your Cheat Sheet: Click here to download a free pip cheat sheet that summarizes the most important pip commands.
Take the Quiz: Test your knowledge with our interactive “Using Python's pip to Manage Your Projects' Dependencies” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
Using Python's pip to Manage Your Projects' DependenciesIn this quiz, you'll test your understanding of Python's standard package manager, pip. You'll revisit the ideas behind pip, important commands, and how to install packages.
Getting Started With pip
So, what exactly does pip
do? pip
is a package manager for Python. That means it’s a tool that allows you to install and manage libraries and dependencies that aren’t distributed as part of the standard library. The name pip was introduced by Ian Bicking in 2008:
I’ve finished renaming pyinstall to its new name: pip. The name pip is [an] acronym and declaration: pip installs packages. (Source)
Package management is so important that Python’s installers have included pip
since versions 3.4 and 2.7.9, for Python 3 and Python 2, respectively. Many Python projects use pip
, which makes it an essential tool for every Pythonista.
The concept of a package manager might be familiar to you if you’re coming from another programming language. JavaScript uses npm for package management, Ruby uses gem, and the .NET platform uses NuGet. In Python, pip
has become the standard package manager.
Finding pip
on Your System
The Python installer gives you the option to install pip
when installing Python on your system. In fact, the option to install pip
with Python is checked by default, so pip
should be ready for you to use after installing Python.
Note: On some Linux (Unix) systems like Ubuntu, pip
comes in a separate package called python3-pip
, which you need to install with sudo apt install python3-pip
. It’s not installed by default with the interpreter.
You can verify that pip
is available by looking for the pip3
executable on your system. Select your operating system below and use your platform-specific command accordingly:
On Windows and Unix systems, pip3
may be found in more than one location. This can happen when you have multiple Python versions installed. If you can’t find pip
in any location on your system, then you may consider reinstalling pip.
Instead of running your system pip
directly, you can also run it as a Python module. In the next section, you’ll learn how.
Running pip
as a Module
When you run your system pip
directly, the command itself doesn’t reveal which Python version pip
belongs to. This unfortunately means that you could use pip
to install a package into the site-packages of an old Python version without noticing. To prevent this from happening, you should run pip
as a Python module:
$ python -m pip
Notice that you use python -m
to run pip
. The -m
switch tells Python to run a module as an executable of the python
interpreter. This way, you can ensure that your system default Python version runs the pip
command. If you want to learn more about this way of running pip
, then you can read Brett Cannon’s insightful article about the advantages of using python -m pip
.
Note: Depending on how you installed Python, your Python executable may have a different name than python
. You’ll see python
used in this tutorial, but you may have to adapt the commands to use something like py
or python3
instead.
Read the full article at https://realpython.com/what-is-pip/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
December 22, 2024 at 07:30PM
Click here for more details...
=============================
The original post is available in Real Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Post a Comment