Keywords: pip | SSL verification | certificate failure | PyPI | Python package management
Abstract: This article provides a comprehensive analysis of common causes for SSL certificate verification failures when using pip to install Python packages, including PyPI domain changes, firewall/proxy settings, system clock desynchronization, and expired CA certificates. Through detailed code examples and configuration instructions, multiple solutions are presented, such as using --trusted-host parameters, updating pip versions, configuring custom CA certificates, and creating pip configuration files, to help developers completely resolve pip SSL verification issues.
Problem Background and Phenomenon Analysis
When using pip to install Python packages, SSL certificate verification failures frequently occur, manifesting as:
Collecting dedupe
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)'),)': /simple/dedupe/
Could not find a version that satisfies the requirement dedupe (from versions: ) No matching distribution found for dedupe
Even when using the --trusted-host parameter to attempt bypassing SSL verification, the problem persists. This is primarily because the --trusted-host option does not actually bypass SSL/TLS verification, but only marks hosts as trusted when they lack valid HTTPS.
Root Cause Deep Analysis
Impact of PyPI Domain Changes
In 2018, PyPI underwent significant domain migration from pypi.python.org to pypi.org. Many old configurations and scripts still use the old domain, leading to SSL verification failures. The main domains currently used by PyPI include:
pypi.org- Primary domainfiles.pythonhosted.org- File hosting domain
Network Environment Factors
Firewall or proxy settings may block access to PyPI domains. Some enterprise proxies even dynamically replace certificates for HTTPS connections, causing certificate chain verification failures. Additionally, system clock desynchronization can break SSL verification processes since certificate validity checks rely on accurate time.
CA Certificate Issues
pip relies on the CA certificate bundle provided by the certifi package for SSL verification. If the pip version is too old, its built-in CA certificates may have expired. The certificate verification flow in pip is as follows:
pip → requests → urllib3 → certifi (CA bundle)
In newer pip versions, pip/_vendor/certifi/cacert.pem is primarily used as the certificate source.
Solutions and Implementation Steps
Method 1: Using Correct trusted-host Parameters
Ensure correct PyPI domains are used, and specify all relevant domains simultaneously:
python -m pip install --trusted-host files.pythonhosted.org --trusted-host pypi.org --trusted-host pypi.python.org <packagename>
If a proxy is required, add the --proxy [user:passwd@]proxyserver:port parameter.
Method 2: Updating pip Version
For Anaconda users:
conda update pip
# Or using conda-forge channel
conda config --add channels conda-forge
conda update pip
For system Python users:
# User-level upgrade to avoid affecting system packages
python -m pip install --user --trusted-host files.pythonhosted.org --trusted-host pypi.org --trusted-host pypi.python.org --upgrade pip
Or use the official PyPA script:
curl -LO https://bootstrap.pypa.io/get-pip.py && python get-pip.py --user
Method 3: Configuring Custom CA Certificates
Download the latest Mozilla CA certificate bundle:
curl -LO https://curl.haxx.se/ca/cacert.pem
Use the --cert parameter to specify a custom certificate:
pip --cert ~/cacert.pem install <packagename>
Or create a pip configuration file ~/.pip/pip.conf (Unix) or %APPDATA%\pip\pip.ini (Windows):
[global]
cert = /path/to/your/cacert.pem
Method 4: Permanent trusted-host Configuration
Add permanently trusted hosts in the pip configuration file:
[global]
trusted-host = pypi.python.org
pypi.org
files.pythonhosted.org
Advanced Debugging Techniques
Enabling Verbose Logging
Use the -vvv parameter to obtain detailed debugging information:
pip install -vvv <packagename>
This helps identify specific SSL error types, such as TLS version mismatch issues.
Environment Isolation Best Practices
Strongly recommend using pip in virtual environments to avoid conflicts with system packages. For Anaconda users, it is advised to:
- Use pip in isolated conda environments
- Avoid mixing conda and pip in the same environment
- If mixing is necessary, ensure conda is used first for base packages, then pip for specific packages
Summary and Recommendations
pip SSL certificate verification failure is a common but solvable problem. It is recommended to try solutions in the following priority order:
- First attempt to update pip to the latest version
- Check network environment to ensure firewall and proxy settings are correct
- Use correct
--trusted-hostparameter combinations - Configure custom CA certificates or permanently trusted hosts
- Operate in virtual environments to avoid system-level conflicts
Through systematic troubleshooting and implementation, pip SSL verification issues can be completely resolved, ensuring smooth Python package management.