I can't use pip on WSL Ubuntu

Question:

If I try to download Python packages with pip, following error messages come out.

nblizz@NBLIZZ-PC:~# pip3 install flask-restful
Collecting flask-restful
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', OSError(0, 'Error'))': /simple/flask-restful/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', OSError(0, 'Error'))': /simple/flask-restful/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', OSError(0, 'Error'))': /simple/flask-restful/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', OSError(0, 'Error'))': /simple/flask-restful/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', OSError(0, 'Error'))': /simple/flask-restful/
  Could not find a version that satisfies the requirement flask-restful (from versions: )
No matching distribution found for flask-restful

None of these solutions work.

  • Re-install pip
  • Re-install WSL
  • sudo pip3 install [package]
  • pip3 install [package] with root account
  • sudo -H pip3 install [package]
  • pip install –user [package]

How do I install pip packages without errors?

Asked By: EnDelt64

||

Answers:

I use google to search: pip ProtocolError

Someone said that:
SOLVED The issue was that my dorm firewall is blocking pip (pypi.python.org). Issuing the same command at work installs python packages correctly.

see: PIP not working – proxy – Connection aborted

If this solution still not work for you, please tell more about your machine like

pip --version, pip3 --version, python3 -V, python -V, cat /etc/issue

Answered By: Waket Zheng

Update your pip: python -m pip install –upgrade pip; If its already updated proceed with any of the below two methods:

Method – I
You can download binary files from the below link
https://www.lfd.uci.edu/~gohlke/pythonlibs/

Once the desired module (.whl file) is downloaded, open the command prompt on the downloaded location (you can do that by pressing shift + right click; select open command prompt/powershell here) and type pip install your-package.whl

Method – II
Go to https://pypi.org/ and search for the desired package.
Once downloaded, copy the package in the site-packages directory manually.

There are standard locations for site-package:

  • Unix(pure): prefix/lib/pythonX.Y/site-packages
  • Unix(non-pure):exec-prefix/lib/pythonX.Y/site-packages
  • Windows:prefixLibsite-packages

To verify the download, by either of these methods, use the import statement on your console.

Answered By: Ronnie

The cause was my Kaspersky application. It blocks every pip/curl executions.

Answered By: EnDelt64

I had a similar but not identical problem with pip in WSL. (There is a diffrent error message).

$ pip3 install pytest
Collecting pytest
  Cache entry deserialization failed, entry ignored
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f3b23344eb8>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',)': /simple/pytest/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f3b23a7e748>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',)': /simple/pytest/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f3b23a7e940>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',)': /simple/pytest/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f3b23a7e390>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',)': /simple/pytest/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f3b23a7eb00>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',)': /simple/pytest/
  Could not find a version that satisfies the requirement pytest (from versions: )
No matching distribution found for pytest

It turned out the WSL could’t connect to any DNS server. ping 8.8.8.8 -c 1 was successful, but ping www.google.com -c 1 resulted in Temporary failure in name resolution. The reason for this was that /etc/resolv.conf was corrupted. I fixed it by replacing the corrupted content with the content below:

# This file was automatically generated by WSL. To stop automatic generation of this file, remove this line.
8.8.8.8
4.4.4.4

(Use e.g. nano for editing: sudo nano /etc/resolv.conf). I used Google’s DNS servers above. WSL should update the DNS server automatically if the first line is included. After this pip worked normally again for me.

Answered By: ax7ster

I faced the same issue on WSL1 using conda environments. Eventually I found out that the solution is to have the same python version in conda’s base environment as in the conda environment from which the pip install commands are being triggered.

sudo pip install should be avoided (even if as it seemingly solves the NewConnectionError error), as it will install packages globally and will mess up conda environments.

Answered By: swimmer

Two issue that typcially get overlooked for me.

Add public dns entry to /etc/resolv.conf
nameserver 8.8.8.8

This will be overwritten on next startup unless you create a /etc/wsl.conf file with the below

[network]
generateResolvConf = false

The first issue is typically only a problem if wsl and windows have a time drift.

From Powershell…
Get-Date; wsl date

If your output doesn’t match give it good old reboot. Usually fixes the issue for me.

Answered By: pressureChief
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.