Learn how to correctly install third-party libraries using pip

PHPz
Release: 2024-01-27 10:20:06
Original
926 people have browsed it

Learn how to correctly install third-party libraries using pip

Mastering skills: How to correctly use pip to install third-party libraries

Introduction: When programming in Python, using third-party libraries is a very common requirement. As a package management tool for Python, pip can easily install and manage third-party libraries. However, using pip correctly can be a bit confusing for beginners. This article will introduce in detail how to correctly use pip to install third-party libraries and provide specific code examples.

  1. Installing pip
    Before you begin, make sure pip is correctly installed on your system. If you are using a newer version of Python (3.4 or higher), pip is already installed by default. You can confirm whether the installation is successful by entering the following command in the terminal:

    pip --version
    Copy after login
    Copy after login

    If the installation is successful, you will see the version number information of pip.

If pip is not installed in your system, you can install it through the following steps:

  • First, download the get-pip.py file. The file can be found on the official website (https://pip.pypa.io/en/stable/installing/).
  • Then, run the get-pip.py file using the following command in the terminal:

    python get-pip.py
    Copy after login
  • Finally, verify that the installation was successful:

    pip --version
    Copy after login
    Copy after login

    If the installation is successful, you will see the version number information of pip.

  1. Installing third-party libraries
    Once you have pip installed, you can start installing third-party libraries. Usually, you just need to run a simple command in the terminal to complete the installation. The following are several common installation commands:
  • Install the latest version:

    pip install 包名
    Copy after login
  • Install the specified version:

    pip install 包名==版本号
    Copy after login
  • Install multiple packages:

    pip install 包名1 包名2 包名3
    Copy after login
  1. Manage dependencies
    Sometimes, a third-party library may depend on some other libraries . When using pip to install third-party libraries, pip will automatically resolve dependencies and install related libraries. For example, you want to install the pandas library, but it depends on the numpy library. You only need to run the following command, and pip will automatically install the numpy library:

    pip install pandas
    Copy after login
  2. Upgrade the third-party library
    Over time, the version of the third-party library may be updated. To make sure you are using the latest version, you can check if there are updates available using the following command:

    pip list --outdated
    Copy after login

    This will list all installed libraries that have updates. You can then use the following command to upgrade specific libraries:

    pip install --upgrade 库名
    Copy after login

    Alternatively, if you want to upgrade all libraries, you can use the following command:

    pip freeze --local | grep -v '^-e' | cut -d = -f 1 | xargs -n1 pip install -U
    Copy after login

    This will list all installed libraries and upgrade them one by one.

  3. Use the requirements.txt file to manage dependencies
    When your project requires multiple third-party libraries, it may be very tedious to install them one by one manually. At this time, you can use the requirements.txt file to manage dependencies. This file lists all third-party libraries required by the project and their version numbers. pip can automatically install all dependent libraries based on this file.

First, you need to create a text file and name it requirements.txt. Then, list the third-party libraries to be installed and their version numbers in the file in the following format:

包名==版本号
Copy after login

For example:

pandas==1.0.3
numpy==1.18.4
Copy after login

After saving the requirements.txt file, run the following in the terminal The command can automatically install all dependent libraries:

pip install -r requirements.txt
Copy after login

Summary:
By using pip correctly, installing and managing third-party libraries in Python development will become very simple and convenient. We introduced how to install pip and how to correctly install, manage and upgrade third-party libraries. At the same time, we also introduced how to use the requirements.txt file to manage project dependencies. I hope this article can help you master the skills of using pip and use third-party libraries more efficiently during the development process.

References:

  1. https://pip.pypa.io/en/stable/installing/
  2. https://pip.pypa.io/ en/stable/user_guide/

The above is the detailed content of Learn how to correctly install third-party libraries using pip. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!