Home>Article>Backend Development> Summary of common methods for installing third-party libraries in Python

Summary of common methods for installing third-party libraries in Python

WBOY
WBOY forward
2022-11-24 17:40:41 10627browse

This article brings you relevant knowledge aboutPython, which mainly introduces the common methods of installing third-party libraries. Let’s take a look at them together. I hope it will be helpful to everyone.

Summary of common methods for installing third-party libraries in Python

[Related recommendations:Python3 video tutorial]

In the study of pyhton, I believe everyone will usually encounter third-party libraries The installation problem is a headache for many beginners. Here I will make some simple summaries on how to install third-party libraries correctly and efficiently without taking detours

Installation method

1 . Install through pychram

This is the simplest and most direct method in my opinion, but it is also the easiest to report errors.

Open File-Settings-Project-Python Interpreter

Summary of common methods for installing third-party libraries in Python
From here you can see the third-party libraries and versions you have installed, and then click the plus sign below to proceed Add to. Here I take numpy as an example. Search for the library you want at the top, and then select the version you want at the bottom right. If not checked, it will default to the latest version. Click Install Packages to install it.

Summary of common methods for installing third-party libraries in Python

2. pip installation method

If the installation in pycharm fails, or pycharm is not used, you can directly Install using pip method.
First open cmd and directly enterpip install the third-party library you want to install(here I still use numpy as an example)
Summary of common methods for installing third-party libraries in Python
Here, I will also add some information about Some methods of updating and uninstalling pip:
(1) Upgrading pip itself

py -m pip install --upgrade pip

(2) pip installation/uninstallation/upgrade

pip install 包名 #安装pip uninstall 包名 #卸载pip install --upgrade 包名 #升级

(3) pip viewing installed Package

pip list

(4) pip checks which packages need to be updated:

pip list --outdated

(5) pip checks the details of a package:

pip show 包名

(6) pip installation specification Version of the package:

pip install 包名==版本号 例如: pip install numpy==1.20.3pip install 'matplotlib>3.4'pip install 'matplotlib>3.4.0,=, , 

3. Download the whl file to local offline installation

If the first two methods fail, then my most recommended method is the third Although it is not as convenient as the first two, it is effective in personal testing and will not report errors! Enter the website //m.sbmmt.com/link/7d7c61cdf50443a0079ebb5011209dbf and find the library you need to download. Here I take matplotlib as an example:
First of all, you must pay attention to before installing this Some libraries that it depends on before the package must be installed first before installing the one you want, otherwise an error will be reported.
At the same time, after updating a certain library, you must also pay attention to updating the libraries it depends on at the same time, otherwise errors will occur.
Summary of common methods for installing third-party libraries in Python
Then select and download based on your python version and number of bits. Here, because my python version is 3.7.9 and the number of bits is 64 bits, I chose matplotlib‑3.5.1 ‑cp37‑cp37m‑win_amd64.whl.
Save the downloaded whl file to the folder Python37\Lib\site-packages, open cmd under the file, enter pip install whl file name to install.
Summary of common methods for installing third-party libraries in Python

Summary of common methods for installing third-party libraries in Python

##3.1 Supplement

During my installation process, most of the failed problems were due to no It is caused by installing the corresponding dependent package, so after reporting an error, be sure to check whether the corresponding package is installed and update it at the same time.

For example, I once encountered this problem:
AttributeError: module 'matplotlib' has no attribute 'get_data_path', which troubled me for an afternoon and did not solve it. Finally, I found that a package that matplotlib depends on did not Installed.
Summary of common methods for installing third-party libraries in Python

4. Other methods

4.1 Python official Pypi menu

Directly search for third parties Library to install, address:

//m.sbmmt.com/link/4b6cf59a2a5b31fc68aa6f9524067ef8

4.2 Domestic mirror sources solve the problem of slow pip installation

Under Windows systems, when installing third-party libraries, you often encounter timeouts or slow downloads. This is Because the Python server is abroad, sometimes the network speed is too slow when downloading with pip. In this case, it is recommended to use a domestic mirror source for installation.
Installation method: Open cmd and add the "-i" or "-index" parameter. Take numpy as an example: `pip install -i //m.sbmmt.com/link/a6455ffc4e47fd737db213366771ec0e numpy
Several commonly used domestic image sources:
Tsinghua: https://www.php .cn/link/a6455ffc4e47fd737db213366771ec0e
Alibaba Cloud: //m.sbmmt.com/link/4901e49e1e2d9c85659eef5b2ffbe12f
University of Science and Technology of China: https://www. php.cn/link/1cbbae823ca54abfb04302180fd84137
Douban: //m.sbmmt.com/link/3c0093f155f7a0bf00afca91dc8fb9b8

Summary

For most novices, the installation of Python third-party libraries is definitely a stumbling block on their way to learning. I have also had a headache with this problem. But as far as my personal installation is concerned, I have personally tested the first three methods. The first and second methods are the simplest and most direct, but they are error-prone because you are not familiar with the dependencies between various packages, so I strongly recommend that if the first two installations are unsuccessful, you must try the third one! ! Check clearly the packages that need to be installed before installing the third-party libraries you need, otherwise the installation will not be successful! ! I don’t use the fourth method often, because I think the first three methods are enough to install the libraries you need. Of course, if you need it, you can learn more about the fourth method in detail, but I won’t cover it here. Too many introductions!

【Related recommendations: Python3 video tutorial

The above is the detailed content of Summary of common methods for installing third-party libraries in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete