pip Install Third-party Library Error
Tags: python, pip, github, programming
Published: 2022-03-28 03:49:13

Original: <https://lwebapp.com/en/post/pip-install-error-guide>

## Question

When we use Python to develop, we usually use some useful third-party libraries. It is recommended to use pip to install, such as installing pandas

```python
python -m pip install pandas
```

Under normal circumstances, there is no problem, but some small partners will encounter installation failures such as pip installation of third-party libraries and errors, pip install stuck and other installation failures.

For example, the following is the code that pip installs a third-party library to report an error

```sh
Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None))...
```

I will summarize here the solutions for the failure of Python to install third-party libraries.

## Solution

### Option 1

The Python environment factor reports an error, check whether Python and pip under your computer are installed normally

```sh
# Print the current Python version
python --version
```

If the Python version information is printed out normally after running the above command, it means that it is normal. If an error is reported, it may be that the environment variables are not configured when installing Python under Windows. You can configure the Python environment variables, or directly reinstall Python to the C drive, so that there will be no problem with environment variables.

```sh
# print pip version
python -m pip --version
```

If the pip version information is printed out normally after running the above command, it means that it is normal. If an error is reported, you can use this command to install and upgrade

```sh
python -m ensurepip --upgrade
```

If you can't run pip yet, you can also install pip manually

1. Open <https://bootstrap.pypa.io/get-pip.py>
2. Right click on the page - save as - save anywhere
3. Execute `python get-pip.py` on the command line of the directory where the get-pip.py file is located

Another advantage of manually installing pip is that you can directly use global pip to install dependent packages, such as

```sh
pip install pandas
```

### Option 2

Although pip is installed normally, sometimes it will prompt you that the version of pip is too low

```sh
WARNING: You are using pip version 22.0.3; however, version 22.0.4 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
```

Run the following command to upgrade pip

```sh
python -m pip install --upgrade pip
```

### Option 3

The third-party package name or version number is entered incorrectly. For example, I install a package called `padas` in the command line terminal

```sh
python -m pip install padas
```

The following error will appear

```sh
ERROR: Could not find a version that satisfies the requirement padas (from versions: none)
ERROR: No matching distribution found for padas
```

The error tells me that I typed `padas`, that the package and its version number cannot be found. At this time, you need to check whether the package name is entered correctly, the correct one should be `pandas`. (Of course, after the article is published, some developers may have released this package, which is only for demonstration)

There is also a problem that the version number may not exist. For example, I install a higher version of `pandas`

```sh
python -m pip install pandas==6.5
```

The following error will appear

```sh
ERROR: Could not find a version that satisfies the requirement pandas==6.5 (from versions: 0.1, 0.2, 0.3.0, 0.4.0, 0.4.1, 0.4.2, 0.4.3, 0.5.0, 0.6.0 , 0.6.1, 0.7.0, 0.7.1, 0.7.2, 0.7.3, 0.8.0, 0.8.1, 0.9.0, 0.9.1, 0.10.0, 0.10.1, 0.11.0, 0.12 .0, 0.13.0, 0.13.1, 0.14.0, 0.14.1, 0.15.0, 0.15.1, 0.15.2, 0.16.0, 0.16.1, 0.16.2, 0.17.0, 0.17.1 , 0.18.0, 0.18.1, 0.19.0, 0.19.1, 0.19.2, 0.20.0, 0.20.1, 0.20.2, 0.20.3, 0.21.0, 0.21.1, 0.22.0, 0.23 .0, 0.23.1, 0.23.2, 0.23.3, 0.23.4, 0.24.0, 0.24.1, 0.24.2, 0.25.0, 0.25.1, 0.25.2, 0.25.3, 1.0.0 , 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5, 1.1.0, 1.1.1, 1.1.2, 1.1.3, 1.1.4, 1.1.5, 1.2.0, 1.2 .1, 1.2.2, 1.2.3, 1.2.4, 1.2.5, 1.3.0, 1.3.1, 1.3.2, 1.3.3, 1.3.4, 1.3.5, 1.4.0rc0, 1.4.0 , 1.4.1)
ERROR: No matching distribution found for pandas==6.5
```

Obviously, this version number cannot be found, and it tells you all the version numbers that can be installed. We only need to select a version number that we need, or install the latest version by default without specifying a version number.

### Option 4

The network quality and communication speed are different in different place, and the pip installation of dependencies will also encounter network timeout problems, such as the following errors

```sh
raise ReadTimeoutError(self._pool, None, 'Read timed out.')
pip._vendor.requests.packages.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='pypi.org', port=443): Read timed out.
```

It indicates that the network has timed out, and there is a problem connecting to `pypi.org`. This is the website hosting the python dependencies, and all pip packages are published on it.

We can set a longer timeout, because the network in most places is not completely unconnectable, but the speed is a bit slow. Here the `--default-timeout` is set to `200s`

```sh
python -m pip --default-timeout=200 install pandas
```

Set a little more timeout, go have a cup of tea and wait for a while, and it may be downloaded.

## Option 5

If you haven't downloaded it after drinking tea, then consider changing the mirror source. For example, we switch to the mirror source of Tsinghua University.

```sh
# --index-url can be abbreviated as -i
python -m pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ pandas
```

In addition to the official source and Tsinghua mirror source

- Tsinghua: `https://pypi.tuna.tsinghua.edu.cn/simple/`
- Official: `https://pypi.org/`

There are other mirror sources you can try

- Aliyun: `https://mirrors.aliyun.com/pypi/simple/`
- Douban: `https://pypi.douban.com/simple/`
- Beijing Foreign Studies University `https://mirrors.bfsu.edu.cn/pypi/web/simple/`

## Option 6

In addition to switching mirror sources, pip officially provides a variety of ways to install dependent packages

1. Download the dependency package in advance, and then install it directly from the local path

For example, we can download the pandas distribution package file from the pip official website, and then execute the installation command in the directory where the local dependency package is located.

Install from `Source Distribution`

```sh
python -m pip install pandas-1.4.1.tar.gz
```

or install from `Built Distributions`

```sh
# Buildpacks for Windows platforms only
python -m pip install pandas-1.4.1-cp310-cp310-win_amd64.whl
```

> List of all pandas distributions: [pandas download files](https://pypi.org/project/pandas/#files)

2. To install from any VCS (version control systems), use the following command format

```sh
python -m pip install -e "vcs+protocol://repo_url/#egg=pkg&subdirectory=pkg_dir"
```

- vcs: version control system name
- protocol: protocol
- repo_url: repository url
- egg: package name
- subdirectory: If the package is not in the project root directory, specify the subdirectory name

For example, install the pandas library directly from github

```sh
python -m pip install git+https://github.com/pandas-dev/pandas.git#egg=pandas
```

This method will pull the latest code from github for local construction, which takes a long time, and is usually the development version.

> pip supports a lot of version control systems and protocols, see [VCS Support](https://pip.pypa.io/en/stable/topics/vcs-support/)

3. Installation from github also supports manual local installation, clone the github repository of the dependent package directly, and execute it in the project directory

```sh
python setup.py install
```

In this way, the library built by this project is directly installed. In essence, it is similar to the remote method of using github. It needs to be compiled locally. It is usually used in the local development stage, or if you want to try the latest features of the project.

However, if you have network problems through pip install, this way of installing through github usually also has certain network problems.

## Option 7

I have tried several of the above solutions, but none of them can perfectly meet my needs.

- Although a long timeout is set, sometimes the network is very slow, and no matter how long the timeout is, it is a waste of time.
- Compared with the official website, the mirror source has a slightly delayed synchronization time. The official `pypi.org` dependency package is the most stable. The same problem is also common in the npm package management of front-end development. The mirror source sometimes has unpredictable errors, and it is often fixed by switching to the official source (refer to [NPM Install Error Guide](https://lwebapp.com/en/post/npm-install-error-guide)).
- If you directly download the source package to build, because many Python libraries are written by foreigners, the website is not in your country, the download speed is very slow when visiting other countries' websites, downloading from github is just as slow (refer to [GitHub Clone Is Very Slow](https://lwebapp.com/en/post/github-cannot-be-accessed)).

We can consider some [more scientific ways to surf the Internet](https://youspots.blogspot.com/2021/06/the-best-vpn-recommendation-express-vpn.html) to speed up access to the official website. After the acceleration, directly use `pip install` to install any third-party library. Basically, it can be completed in a few seconds. There is no need to set a timeout period, no need to switch mirror sources, and no need to worry about the delay of the installation package version. If you want to download the source package from the pip official website or installing Python third-party libraries remotely using github is very fast.

In addition, there are the following benefits

- When you access some Python third-party library documents, you can also speed up access, such as pandas official website `https://pandas.pydata.org/`
- When you go to github to learn the source code of open source projects, and use `git clone` to pull github open source projects, the speed is significantly improved
- The technical documents searched by Google are often more accurate, and the top of the search results are the highly praised answers on stackoverflow, which are very useful
- Visit some other great tech sites

> Recommend a more scientific way on this [**Official Website ➜**](https://upghsbc.com/?a_fid=alexliu)
>
> If you don't understand, please refer to my [**Hard-earned Experience ➜**](https://youspots.blogspot.com/2021/06/the-best-vpn-recommendation-express-vpn.html)

## Reference

- [pip Install Third-Party Library Error](https://lwebapp.com/en/post/pip-install-error-guide)
- [Python official website](https://www.python.org/)
- [Python installation dependencies](https://packaging.python.org/en/latest/tutorials/installing-packages/)
- [pip Installation](https://pip.pypa.io/en/stable/installation/)
- [NPM Install Reports an Error and Freezes, NPM Installation Guide](https://lwebapp.com/en/post/npm-install-error-guide)
- [Python install Github package, offline package and online package](https://blog.csdn.net/tandelin/article/details/103664721)