Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 23375

Real Python: Using Python's pip to Manage Your Projects' Dependencies

$
0
0

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.
  • pipmanages 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:


Using Python's pip to Manage Your Projects' Dependencies

Interactive Quiz

Using Python's pip to Manage Your Projects' Dependencies

In 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:

Windows PowerShell
PS> where pip3
Copied!

The where command on Windows will show you where you can find the executable of pip3. If Windows can’t find an executable named pip3, then you can also try looking for pip without the three (3) at the end.

Shell
$ whichpip3
Copied!

The which command on Linux systems and macOS shows you where the pip3 binary file is located.

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:

Shell
$ python-mpip
Copied!

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 ]


Viewing all articles
Browse latest Browse all 23375

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>