Fixed: Connection error [SSL CERTIFICATE_VERIFY_FAILED]

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
6 min

banner

# Table of Contents

  1. Connection error: [SSL CERTIFICATE_VERIFY_FAILED]
  2. Add trusted hosts to your pip.conf or pip.ini file
  3. Using a command to update your pip.ini file
  4. Disable SSL certificate validation when using requests
  5. Upgrade your version of pip
  6. Create a virtual environment
  7. Specifying a certificate when running the command
  8. Try running pip install in verbose mode

# [Fixed] Connection error: [SSL CERTIFICATE_VERIFY_FAILED]

The "connection error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed" occurs when a firewall is preventing you from reaching the pypi servers when trying to install a package.

To solve the error, run the pip install command with the --trusted-host option.

shell
Downloading/unpacking <package> Getting page https://pypi.python.org/simple/<package>/ Could not fetch URL https://pypi.python.org/simple/<package>/: connection error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:598) Will skip URL https://pypi.python.org/simple/<package>/ when looking for download links for <package> Getting page https://pypi.python.org/simple/ Could not fetch URL https://pypi.python.org/simple/: connection error: HTTPSConnectionPool(host='pypi.python.org', port=443): Max retries exceeded with url

If you got the error when using the requests module, click on the following subheading:

Open your shell and run one of the following commands, depending on your operating system and your version of Python.

Note that the commands contain <package-name> placeholder. Make sure to replace the placeholder with the name of the package you're trying to install.

shell
pip install <package-name> --trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org # ๐Ÿ‘‡๏ธ pip3 (for Python 3) pip3 install <package-name> --trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org # ๐Ÿ‘‡๏ธ If you don't have pip in PATH python -m pip install <package-name> --trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org # ๐Ÿ‘‡๏ธ For Python3 python3 -m pip install <package-name> --trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org # ๐Ÿ‘‡๏ธ py alias (Windows) py -m pip install <package-name> --trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org

install with trusted host option

If you get a permissions error, try prefixing the command with sudo, or use the --user option.

shell
pip install <package-name> --user --trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org

The --trusted-host option marks a host as trusted.

This helps when you try to reach the pypi servers from behind a firewall.

Alternatively, you can add the trusted hosts to your pip.conf (macOS and Linux) or pip.ini (Windows) file, so you don't have to do it every time you need to install a package.

# Add trusted hosts to your pip.conf or pip.ini file

The location of your pip.conf (macOS and Linux) or pip.ini (Windows) is going to be different depending on your operating system.

This section of the docs shows the different locations of the config file under "User".

For Linux, the location is $HOME/.config/pip/pip.conf.

For macOS, the location is $HOME/Library/Application Support/pip/pip.conf or $HOME/.config/pip/pip.conf.

For Windows, the location is %APPDATA%\pip\pip.ini.

shell
# ๐Ÿ‘‡๏ธ Windows %APPDATA%\pip\pip.ini # ๐Ÿ‘‡๏ธ Same as the following (Windows) C:\Users\<username>\AppData\Roaming\pip\pip.ini # ---------------------------------------------------- # ๐Ÿ‘‡๏ธ macOS $HOME/Library/Application Support/pip/pip.conf # ๐Ÿ‘‡๏ธ macOS $HOME/.config/pip/pip.conf # ---------------------------------------------------- # ๐Ÿ‘‡๏ธ Linux $HOME/.config/pip/pip.conf

The pip.conf (Linux or macOS) or pip.ini file should look as follows.

pip.conf
[global] trusted-host = pypi.python.org pypi.org files.pythonhosted.org

# Using a command to update your pip.ini file

An alternative to manually editing your pip.conf or pip.ini file is to use a command.

shell
pip config set global.trusted-host "pypi.python.org pypi.org files.pythonhosted.org" --trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org

add trustd hosts to pip conf ini

The command automatically adds the trusted hosts to your pip.conf or pip.ini file and prints the location of the file.

This is quite convenient because finding the pip.conf or pip.ini file on your operating system might be more difficult than expected.

If you are unable to find the pip.conf or pip.ini file and want to add the trusted hosts manually, run the pip config -v list command.

shell
pip config -v list pip3 config -v list

get location of pip conf

Once you set the trusted-host variable in the pip.conf or pip.ini file, you don't have to use the --trusted-host option every time you pip install a module.

shell
pip install numpy pip3 install numpy # ๐Ÿ‘‡๏ธ If pip is not set up in PATH python -m pip install numpy python3 -m pip install numpy # ๐Ÿ‘‡๏ธ py alias (Windows) py -m pip install numpy

# Disable SSL certificate validation when using requests

When using the requests library, you might get the error:

shell
requests.exceptions.SSLError: [Errno 1] \_ssl.c:503: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed requests.exceptions.SSLError: HTTPSConnectionPool(host='google.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)'),))

If you got the error when using the requests module, requests was unable to verify the SSL certificate of the site.

Set the verify keyword argument to False to disable the SSL certificate validation for the request.

Note that you should only disable SSL certificate validation during local development or testing as it could make your application vulnerable to man-in-the-middle attacks.

main.py
import requests def make_request(): try: url = 'https://reqres.in/api/users' # ๐Ÿ‘‡๏ธ Set verify to False response = requests.get(url, verify=False, timeout=30) parsed = response.json() print(parsed) except Exception as e: print(e) make_request()

The code sample sets the verify keyword arguments to False which disables SSL certificate validation.

main.py
response = requests.get(url, verify=False, timeout=30)

If you have a .pem certificate file locally, set the verify keyword argument to the path to the .pem file.

main.py
requests.get('https://github.com', verify='/path/to/certfile')

For example, your call to requests.get might look similar to the following.

main.py
# http://curl.haxx.se/ca/cacert.pem certificate_file = 'cacert.pem' r = requests.get('https://github.com', verify=certificate_file)

If you don't have a certificate .pem file, you can try using the one from curl: http://curl.haxx.se/ca/cacert.pem.

Click on the link to download the .pem file and update the path.

# Try to set the --default-timeout parameter when running pip install

Another solution to the "ReadTimeoutError: HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed out." error is to set the --default-timeout parameter when issuing the pip install command.

shell
pip install --default-timeout=100 numpy # or pip3 pip3 install --default-timeout=100 numpy

You can also specify the default timeout of pip commands by setting the PIP_DEFAULT_TIMEOUT environment variable.

For example, if you are on macOS or Linux, issue the following command.

shell
# For macOS and Linux export PIP_DEFAULT_TIMEOUT=100

And if you are on Windows and use CMD (Command Prompt), issue the following command.

shell
# Windows (with CMD) SET PIP_DEFAULT_TIMEOUT=100

If you are on Windows and use PowerShell, issue the following command instead.

shell
$env:PIP_DEFAULT_TIMEOUT="100"

# Upgrade your version of pip

If the error persists, run one of the following commands to upgrade your versions of pip, setuptools and wheel.

shell
pip install setuptools wheel pip --upgrade # ๐Ÿ‘‡๏ธ For Python 3 pip3 install setuptools wheel pip --upgrade # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python -m pip install setuptools wheel pip --upgrade # ๐Ÿ‘‡๏ธ For Python 3 python3 -m pip install setuptools wheel pip --upgrade # ๐Ÿ‘‡๏ธ Using py alias (Windows) py -m pip install setuptools wheel pip --upgrade

If you get a permissions error, run the command with the --user option or prefix it with sudo.

shell
# ๐Ÿ‘‡๏ธ If you get a permissions error pip install setuptools wheel pip --upgrade --user sudo pip install setuptools wheel pip --upgrade

# Create a virtual environment

To solve the error:

  1. Create a virtual environment.
  2. Activate the virtual environment.
  3. Run the pip install command with the virtual environment active.
shell
# ๐Ÿ‘‡๏ธ Use the correct version of Python when creating VENV python -m venv venv # ๐Ÿ‘‡๏ธ Activate on Unix or MacOS source venv/bin/activate # ๐Ÿ‘‡๏ธ Activate on Windows (cmd.exe) venv\Scripts\activate.bat # ๐Ÿ‘‡๏ธ Activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # ๐Ÿ‘‡๏ธ Install the specific package in the virtual environment pip install numpy

If the python -m venv venv command doesn't work, try the following 2 commands:

  • python3 -m venv venv
  • py -m venv venv

Make sure to use the correct command to activate your virtual environment depending on your operating system and your shell.

Your virtual environment will use the version of Python that was used to create it.

You can create a pip.conf (Linux, macOS) or pip.ini (Windows) file in the venv folder with the following contents.
venv/pip.conf
[global] trusted-host = pypi.python.org pypi.org files.pythonhosted.org

Try installing a module after that's done.

# Specifying a certificate when running the command

If you have a certificate stored in a local file, use the --cert option when running the pip install command.

shell
pip --cert </path/to/cert>.pem install <package_name>

For example, the complete command might look similar to the following.

shell
pip --cert /etc/ssl/certs/MY_CERT.pem install requests

Make sure to correct the path to your .pem file and replace requests with the name of the module you're trying to install.

If you don't have a certificate .pem file, you can try using the one from curl: http://curl.haxx.se/ca/cacert.pem.

Click on the link to download the .pem file and update the path.

If none of the suggestions helped, try running the pip install command in verbose mode.

# Try running pip install in verbose mode

If none of the suggestions helped, try running the pip install command in verbose mode.

shell
pip install numpy -vvv pip3 install numpy -vvv python -m pip install numpy -vvv

The -v option stands for verbose mode and can be used up to 3 times.

When the pip install command is run in verbose mode, the command shows more output and how the error occurred.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev