This article brings you relevant knowledge about Python, 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.
[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
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
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.
If the installation in pycharm fails, or pycharm is not used, you can directly Install using pip method.
First open cmd and directly enter pip install the third-party library you want to install
(here I still use numpy as an example)
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,=, , <h2> <a id="3_whl_55"></a>3. Download the whl file to local offline installation</h2><p>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 <a href="https://www.php.cn/link/7d7c61cdf50443a0079ebb5011209dbf">https://www.php.cn/link/7d7c61cdf50443a0079ebb5011209dbf</a> and find the library you need to download. Here I take matplotlib as an example: <br> First of all, you must pay attention to <mark> 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 </mark> will be reported. <br> 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. <br><img src="https://img.php.cn/upload/article/000/000/067/36cabce7a764653c64263a29550b7634-3.png" alt="Summary of common methods for installing third-party libraries in Python"><br> 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. <br> Save the downloaded whl file to the folder <code>Python37\Lib\site-packages</code>, open cmd under the file, enter <code>pip install whl file name</code> to install. <br><img src="https://img.php.cn/upload/article/000/000/067/c522c75f56fd335401a3847b6e4f524c-4.png" alt="Summary of common methods for installing third-party libraries in Python"></p><p><img src="https://img.php.cn/upload/article/000/000/067/d5fdf37a9f18234fe0c302793ed1f48c-5.png" alt="Summary of common methods for installing third-party libraries in Python"></p><h3><a id="31__65">##3.1 Supplement</a></h3>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. <p> For example, I once encountered this problem: <br>AttributeError: module 'matplotlib' has no attribute 'get_data_path'<code>, which troubled me for an afternoon and did not solve it. Finally, I found that a package that matplotlib depends on did not Installed. </code><br><img src="https://img.php.cn/upload/article/000/000/067/d5fdf37a9f18234fe0c302793ed1f48c-6.png" alt="Summary of common methods for installing third-party libraries in Python"></p><h2><a id="4_71">4. Other methods</a></h2><h3><a id="41_PythonPypi_72">4.1 Python official Pypi menu</a></h3> Directly search for third parties Library to install, address: <p>https://www.php.cn/link/4b6cf59a2a5b31fc68aa6f9524067ef8<a href="https://www.php.cn/link/4b6cf59a2a5b31fc68aa6f9524067ef8"></a></p><h3> <a id="42_pip_74"></a>4.2 Domestic mirror sources solve the problem of slow pip installation</h3><p>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. <br> Installation method: Open cmd and add the "-i" or "-index" parameter. Take numpy as an example: `pip install -i https://www.php.cn/link/a6455ffc4e47fd737db213366771ec0e numpy<br> Several commonly used domestic image sources: <br> Tsinghua: <a href="https://www.php.cn/link/a6455ffc4e47fd737db213366771ec0e">https://www.php .cn/link/a6455ffc4e47fd737db213366771ec0e</a><br> Alibaba Cloud: <a href="https://www.php.cn/link/4901e49e1e2d9c85659eef5b2ffbe12f">https://www.php.cn/link/4901e49e1e2d9c85659eef5b2ffbe12f</a><br> University of Science and Technology of China: <a href="https://www.php.cn/link/1cbbae823ca54abfb04302180fd84137">https://www. php.cn/link/1cbbae823ca54abfb04302180fd84137</a><br> Douban: <a href="https://www.php.cn/link/3c0093f155f7a0bf00afca91dc8fb9b8">https://www.php.cn/link/3c0093f155f7a0bf00afca91dc8fb9b8</a></p><h1> <a id="_83"></a>Summary</h1> <p>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! ! <mark>Check clearly the packages that need to be installed before installing the third-party libraries you need, otherwise the installation will not be successful</mark>! ! 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! </p><p>【Related recommendations: <a href="http://www.php.cn/course/list/31.html" target="_blank">Python3 video tutorial</a>】</p>
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!