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.
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
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:
Then, run the get-pip.py file using the following command in the terminal:
python get-pip.py
Finally, verify that the installation was successful:
pip --version
If the installation is successful, you will see the version number information of pip.
Install the latest version:
pip install 包名
Install the specified version:
pip install 包名==版本号
Install multiple packages:
pip install 包名1 包名2 包名3
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
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
This will list all installed libraries that have updates. You can then use the following command to upgrade specific libraries:
pip install --upgrade 库名
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
This will list all installed libraries and upgrade them one by one.
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:
包名==版本号
For example:
pandas==1.0.3 numpy==1.18.4
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
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:
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!