What if I can't install a python module? Don't be afraid, I have a coup!

Before we introduced how to install third-party packages in Python:

Python is famous for its rich third-party packages. Almost all the functions you want can be installed through the pip command, avoiding the embarrassment of having to reinvent the wheel yourself.

However, there are two common problems with pip installation, the first is slow download, and the second is the inability to install. The reason for the slowness is that the resource server is placed on the other side of the ocean, and it cannot be installed mostly because of version or system compatibility issues.

Today's article is prepared to solve these two problems.

1. What if the download is slow?

In order to speed up the download speed, some universities and large factories in China have mirrored pypi (the resource server storing the third python package). For example, Tsinghua University, China University of Science and Technology, Ali, etc. are commonly used sources. When we install, we only need to  specify the address of the domestic mirror through the  -i parameter, for example:

# Install flask as an example

pip install Flask -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn

The speed will take off immediately, who knows who uses it!

Several commonly used mirror source addresses:

Tsinghua https: //pypi.tuna.tsinghua.edu.cn / simple /

USTC HTTPS: //pypi.mirrors.ustc.edu.cn / the Simple /

Alibaba Cloud http: //mirrors.aliyun.com / pypi / simple /

Watercress HTTP: / /pypi.douban.com/simple /

If you don't want to input this long string of addresses every time, you can also write the url into the pip configuration file, so that each time you execute pip, you don't need to specify the source address.

Write the following content to the pip.ini file

[global]

trusted-host = pypi.tuna.tsinghua.edu.cn

index-url = https://pypi.tuna.tsinghua.edu.cn/simple/

2. What should I do if the installation fails?

Although pip installation is very simple, sometimes there are some minor problems, such as the package cannot be installed normally. There may be network problems, version problems, system compatibility problems, etc. Especially on the Windows platform, the probability of a problem is greater.

For example, an error is reported when installing the MySQL driver:

(An error is reported when installing the MySQL driver in Windows, you must have encountered it)

What about easy-to-use Python? How difficult is it to install a package?

Programming is like Daguai upgrade, you will always encounter a variety of problems, but as long as you do n’t give up, there is always a way to meet the last big boss, and the pit you have encountered has long been stepped on.

No, any packages that Windows cannot install normally can be solved through the following website

For example, MySQ driver package, corresponding to more than 2.7, 3.5, 3.6, 3.7, 3.8 versions, also divided into 32-bit and 64-bit.

3. How to install

The first step: open the website (this website must be collected):

https://www.lfd.uci.edu/~gohlke/pythonlibs/

Step 2: Find and download the corresponding version of the whl file on the page

Step 3: Execute the offline installation command in the directory where the downloaded file is located

pip install installation file name

pip install mysqlclient-1.4.6-cp37-cp37m-win32.whl

Prompt Successfully installed! You're done!

Finally, I would like to add, what if the package you want to download is not found on this website? For example, the mitmproxy library cannot be successfully installed directly using pip? The reason why it cannot be installed is because some libraries it depends on do not support direct pip installation. You can solve it by finding the corresponding whl file on this website and installing it in advance.

For example, when I installed mitmproxy, there was an installation error during the installation of brotlipy. This library can be found at https://www.lfd.uci.edu. After installing the dependent libraries, you can install the libraries you want normally.

With the above two methods, you can solve 99% of your Python package installation problems!

Guess you like

Origin www.cnblogs.com/7758520lzy/p/12692326.html