The standard package manager for Python is Show
In this tutorial, you’ll learn how to:
You can do a lot with Getting Started With pipSo, what exactly does 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 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, Finding pip on Your SystemThe Python 3 installer gives you the option to install You can verify that The The `pip`5 command on Linux systems and macOS shows you where the `pip`9 binary file is located. On Windows and Unix systems, Instead of running your system Running pip as a ModuleWhen you run your system Notice that you use Sometimes you may want to be more explicit and limit packages to a specific project. In situations like this, you should run Using pip in a Python Virtual EnvironmentTo avoid installing packages directly into your system Python installation, you can use a virtual environment. A virtual environment provides an isolated Python interpreter for your project. Any packages that you use inside this environment will be independent of your system interpreter. This means that you can keep your project’s dependencies separate from other projects and the system at large. Using
Python 3 has the built-in `pip`8 module for creating virtual environments. This module helps you create virtual environments with an isolated Python installation. Once you’ve activated the virtual environment, then you can install packages into this environment. The packages that you install into one virtual environment are isolated from all other environments on your system. You can follow these steps to create a virtual environment and verify that you’re using the Here you create a virtual environment named `pip`8 by using Python’s built-in `pip`8 module. Then you activate it with the `pip`2 command. The parentheses (`pip`3) surrounding your `pip`8 name indicate that you successfully activated the virtual environment. Finally, you check the version of the Reinstalling pip When Errors OccurWhen you run the Operating System Error Message Windows`pip`2 `pip`3 Linux`pip`4 macOS`pip`5 Error messages like these indicate that something went wrong with the installation of Getting errors like the ones shown above can be frustrating because Although the error messages for various systems differ, they all point to the same problem: Your system can’t find The output of this command will show a list of locations (directories) on your disk where the operating system looks for executable programs. Depending on your system, locations can be separated by a colon (`pip`4) or a semicolon (`pip`5). By default, the directory that contains the
The If Another way to fix your This script will install the latest version of If none of the methods above work, then it might be worth trying to download the latest Python version for your current platform. You can follow the Python 3 Installation & Setup Guide to make sure that Installing Packages With pipPython is considered a language. This means that the Python standard library contains an extensive set of packages and modules to help developers with their coding projects. At the same time, Python has an active community that contributes an even more extensive set of packages that can help you with your development needs. These packages are published to the Python Package Index, also known as PyPI (pronounced Pie Pea Eye). PyPI hosts an extensive collection of packages, including development frameworks, tools, and libraries. Many of these packages provide friendly interfaces to the Python standard library’s functionality. Using the Python Package Index (PyPI)One of the many packages that PyPI hosts is called `pip`06. The `pip`06 library helps you to interact with web services by abstracting the complexities of HTTP requests. You can learn all about `pip`06 on its official documentation site. When you want to use the `pip`06 package in your project, you must first install it into your environment. If you don’t want to install it in your system Python site-packages, then you can create a virtual environment first, as shown above. Once you’ve created the virtual environment and activated it, then your command-line prompt shows the name of the virtual environment inside the parentheses. Any To install packages, In this example, you run The `pip`18 command always looks for the latest version of the package and installs it. It also searches for dependencies listed in the package metadata and installs them to ensure that the package has all the requirements that it needs. It’s also possible to install multiple packages in a single command: By chaining the packages `pip`19 and `pip`20 in the `pip`21 command, you install both packages at once. You can add as many packages as you want to the `pip`21 command. In cases like this, a `pip`23 file can come in handy. Later in this tutorial, you’ll learn how to use a `pip`23 file to install many packages at once. You can use the `pip`25 command to display the packages installed in your environment, along with their version numbers: The To get more information about a specific package, you can look at the package’s metadata by using the The output of this command on your system will list the package’s metadata. The `pip`32 line lists packages, such as `pip`33, `pip`34, `pip`35, and `pip`36. These were installed because `pip`06 depends on them to work correctly. Now that you’ve installed `pip`06 and its dependencies, you can import it just like any other regular package in your Python code. Start the interactive Python interpreter and import the `pip`06 package: After starting the interactive Python interpreter, you imported the `pip`06 module. By calling `pip`41, you verified that you were using the `pip`06 module within your virtual environment. Using a Custom Package IndexBy default, Using A custom package index must comply with PEP 503 – Simple Repository API to work with For example, to install the `pip`19 tool from the TestPyPI package index, you can run the following command: With the If you need to use an alternative index permanently, then you can set the With the If you want to add a `pip`57 file, then you can choose one of the locations that `pip`64 listed. A `pip`57 file with a custom package index looks like this: When you have a Installing Packages From Your GitHub RepositoriesYou’re not limited to packages hosted on PyPI or other package indexes. With the `pip`72 scheme, you can point to a Git repository that contains an installable package. You can verify that you installed the package correctly by running an interactive Python interpreter and importing `pip`19: After starting the interactive Python interpreter, you import the `pip`19 module. By calling `pip`75, you verify that you’re using the `pip`19 module that’s based in your virtual environment. Installing packages from a Git repository can be helpful if the package isn’t hosted on PyPI but has a remote Git repository. The remote repository you point Installing Packages in Editable Mode to Ease DevelopmentWhen working on your own package, installing it in an editable mode can make sense. By doing this, you can work on the source code while still using your command line like you would in any other package. A typical workflow is to first clone the repository and then use With the commands above, you installed the `pip`19 package as an editable module. Here’s a step-by-step breakdown of the actions you just performed:
The If you hadn’t used the
Using the In the next section, you’ll learn how requirements files can help with your Using Requirements FilesThe `pip`21 command always installs the latest published version of a package, but sometimes your code requires a specific package version to work correctly. You want to create a specification of the dependencies and versions that you used to develop and test your application so that there are no surprises when you use the application in production. Pinning RequirementsWhen you share your Python project with other developers, you may want them to use the same versions of external packages that you’re using. Maybe a specific version of a package contains a new feature that you rely on, or the version of a package that you’re using is incompatible with former versions. These external dependencies are also called requirements. You’ll often find Python projects that pin their requirements in a file called `pip`23 or similar. The requirements file format allows you to specify precisely which packages and versions should be installed. Running `pip`01 shows that there’s a `pip`02 command that outputs the installed packages in requirements format. You can use this command, redirecting the output to a file to generate a requirements file: This command creates a `pip`23 file in your working directory with the following content: Remember that `pip`27 displayed above is a placeholder format for the package versions. Your `pip`23 file will contain real version numbers. The `pip`02 command dumps the name and version of the currently installed packages to standard output. You can redirect the output to a file that you can later use to install your exact requirements into another system. You can name the requirements file whatever you want. However, a widely adopted convention is to name it `pip`23. When you want to replicate the environment in another system, you can run `pip`21, using the `pip`09 switch to specify the requirements file: In the command above, you tell Now you’re ready to share your project! You can submit `pip`23 into a version control system like Git and use it to replicate the same environment on other machines. But wait, what happens if new updates are released for these packages? Fine-Tuning RequirementsThe problem with hardcoding your packages’ versions and dependencies is that packages are updated frequently with bug and security fixes. You probably want to leverage those updates as soon as they’re published. The requirements file format allows you to specify dependency versions using comparison operators that give you some flexibility to ensure packages are updated while still defining the base version of a package. Open `pip`23 in your favorite editor and turn the equality operators (`pip`16) into greater than or equal to operators (`pip`17), like in the example below: You can change the to Next, you can upgrade the packages in your requirements file by running the `pip`12 command with the `pip`0 switch or the `pip`24 shorthand: If a new version is available for a listed package, then the package will be upgraded. In an ideal world, new versions of packages would be backward compatible and would never introduce new bugs. Unfortunately, new versions can introduce changes that’ll break your application. To fine-tune your requirements, the requirements file syntax supports additional . Imagine that a new version, `pip`25, of `pip`06 is published but introduces an incompatible change that breaks your application. You can modify the requirements file to prevent `pip`25 or higher from being installed: Changing the version specifier for the Separating Production and Development DependenciesNot all packages that you install during the development of your applications will be production dependencies. For example, you’ll probably want to test your application, so you need a test framework. A popular framework for testing is `pip`31. You want to install it in your development environment, but you don’t want it in your production environment, because it isn’t a production dependency. You create a second requirements file, `pip`32, to list additional tools to set up a development environment: Having two requirements files will demand that you use Notice that you use the same `pip`09 switch to install the production `pip`23 file. Now, in your development environment, you only have to run this single command to install all requirements: Because `pip`32 contains the `pip`42 line, you’ll install not only `pip`31 but also the pinned requirements of `pip`23. In a production environment, it’s sufficient to install the production requirements only: With this command, you install the requirements listed in `pip`23. In contrast to your development environment, your production environment won’t have `pip`31 installed. Freezing Requirements for ProductionYou created the production and development requirement files and added them to source control. These files use flexible version specifiers to ensure that you leverage bug fixes published by your dependencies. You’ve also tested your application and are now ready to deploy it to production. You know that all the tests pass and the application works with the dependencies that you used in your development process, so you probably want to ensure that you deploy identical versions of dependencies to production. The current version specifiers don’t guarantee that the identical versions will be deployed to production, so you want to freeze the production requirements before releasing your project. After you’ve finished development with your current requirements, a workflow to create a new release of your current project can look like this: Step Command Explanation 1`pip`31 Run your tests and verify that your code is working properly. 2`pip`48 Upgrade your requirements to versions that match the constraints in your `pip`23 file. 3`pip`31 Run your tests and consider downgrading any dependency that introduced errors to your code. 4`pip`51 Once the project works correctly, freeze the dependencies into a `pip`52 file. With a workflow like this, the `pip`52 file will contain exact version specifiers and can be used to replicate your environment. You’ve ensured that when your users install the packages listed in `pip`52 into their own environments, they’ll be using the versions that you intend them to use. Freezing your requirements is an important step to ensure that your Python project works the same way for your users in their environments as it did in yours. Uninstalling Packages With pipOnce in a while, you’ll have to uninstall a package. Either you found a better library to replace it, or it’s something that you don’t need. Uninstalling packages can be a bit tricky. Notice that when you installed Before you uninstall a package, make sure to run the `pip`30 command for that package: Notice the last two fields, `pip`32 and `pip`62. The `pip`30 command tells you that `pip`06 requires `pip`33, `pip`34, `pip`35, and `pip`36. You probably want to uninstall those too. Notice that `pip`06 isn’t required by any other package. So it’s safe to uninstall it. You should run the `pip`30 command against all of the `pip`06 dependencies to ensure that no other libraries also depend on them. Once you understand the dependency order of the packages that you want to uninstall, then you can remove them using the `pip`72 command: The `pip`72 command shows you the files that will be removed and asks for confirmation. If you’re sure that you want to remove the package because you’ve checked its dependencies and know that nothing else is using it, then you can pass a `pip`74 switch to suppress the file list and confirmation dialog: Here you uninstall `pip`36. Using the `pip`74 switch, you suppress the confirmation dialog asking you if you want to uninstall this package. In a single call, you can specify all the packages that you want to uninstall: You can pass in multiple packages to the `pip`77 command. If you didn’t add any additional switches, then you’d need to confirm uninstalling each package. By passing the `pip`74 switch, you can uninstall them all without any confirmation dialog. You can also uninstall all the packages listed in a requirements file by providing the `pip`79 option. This command will prompt a confirmation request for each package, but you can suppress it with the `pip`74 switch: Remember to always check the dependencies of packages that you want to uninstall. You probably want to uninstall all dependencies, but uninstalling a package used by others will break your working environment. In consequence, your project may not work correctly anymore. If you’re working in a virtual environment, it can be less work to just create a new virtual environment. Then you can install the packages that you need instead of trying to uninstall the packages that you don’t need. However, `pip`77 can be really helpful when you need to uninstall a package from your system Python installation. Using `pip`77 is a good way to declutter your system if you accidentally install a package system-wide. Exploring Alternatives to pipThe Python community provides excellent tools and libraries for you to use beyond Here are some other package management tools that are available for Python: Tool Description Conda Conda is a package, dependency, and environment manager for many languages, including Python. It comes from Anaconda, which started as a data science package for Python. Consequently, it’s widely used for data science and machine learning applications. Conda operates its own index to host compatible packages. Poetry Poetry will look very familiar to you if you’re coming from JavaScript and npm. Poetry goes beyond package management, helping you build distributions for your applications and libraries and deploying them to PyPI. Pipenv Pipenv is another package management tool that merges virtual environment and package management in a single tool. Pipenv: A Guide to the New Python Packaging Tool is a great place to start learning about Pipenv and its approach to package management. Only ConclusionMany Python projects use the In this tutorial, you learned how to:
In addition, you’ve learned about the importance of keeping dependencies up to date and alternatives to By taking a closer look at |