How to Fix pip SSL Certificate Verification Errors in Python

Having trouble installing Python packages with pip? Getting that frustrating SSL certificate verification error? You’re not alone. In this post, I’ll explain what’s causing this common issue and show you how to fix it quickly and securely.

The Problem

When trying to install Python packages using pip, you might encounter this error:

SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] 
certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)'))

This error occurs when pip cannot verify the SSL certificate of PyPI (Python Package Index) servers. It’s a security feature meant to protect you from potential man-in-the-middle attacks, but it can be frustrating when you’re just trying to install packages.

Understanding the Cause

This issue typically happens because:

  1. Your system’s SSL certificates are not up to date
  2. The SSL certificates are missing entirely
  3. Your system can’t locate the certificate store

The Solution

There are several ways to resolve this issue. I’ll present them in order of recommended approach:

1. Install System Certificates (Recommended)

The most secure and recommended solution is to install the system certificates:

pip install pip-system-certs

This package ensures pip uses your system’s certificate store, which is typically more secure and up-to-date.

2. Configure Trusted Hosts

If the above solution doesn’t work, you can explicitly tell pip to trust PyPI’s hosts:

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

Or use it directly in your pip install command:

pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org <package-name>

3. Update Your Certificates

On some systems, you might need to update your certificate store:

  • Windows: Update Windows and Python to the latest version
  • macOS: Run the “Install Certificates.command” in your Python folder
  • Linux: Update ca-certificates package:
  sudo apt-get update
  sudo apt-get install ca-certificates

Best Practices and Security Considerations

While the trusted-host approach works, it’s important to understand that it bypasses SSL verification. This should only be used in controlled environments where you’re certain about the security of your network.

Always prefer using proper SSL certificates when possible, as they provide:

  • Protection against man-in-the-middle attacks
  • Verification of package source authenticity
  • Secure encrypted communication

Alternative Solutions

If you’re working in a corporate environment, you might also:

  1. Configure pip to use your corporate proxy
  2. Set up a local PyPI mirror
  3. Use a custom certificate authority

Conclusion

SSL certificate errors can be frustrating, but they exist for a good reason – your security. The recommended approach is to install system certificates using pip-system-certs. If that doesn’t work, configuring trusted hosts is a quick fix, but remember to consider the security implications.

Remember: Security features like SSL verification are there to protect you and your code. While it might be tempting to disable them, it’s always better to fix the underlying certificate issues properly.

Have you encountered other pip-related issues? Let me know in the comments below!


Posting Images to Twitter Using the New API: A Guide

Understanding the TweepyV2Images Repository

“TweepyV2Images” is a GitHub repository designed to work with the new Twitter API. It uses Python and the Tweepy library, making it accessible for a wide range of users, from beginners to seasoned developers.

https://github.com/jorgez19/TweepyV2Images

Video Tutorial

For a more interactive guide, there’s a YouTube tutorial that comprehensively demonstrates the process from setup to posting images.

You have to install and configure tweepy correct before you can use this helpful script.

import tweepy

# Enter API tokens below
bearer_token = ''
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

# V1 Twitter API Authentication
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)

# V2 Twitter API Authentication
client = tweepy.Client(
    bearer_token,
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret,
    wait_on_rate_limit=True,
)

# Upload image to Twitter. Replace 'filename' with your image filename.
media_id = api.media_upload(filename="your_image.jpg").media_id_string
print(media_id)

# Text to be Tweeted
text = "Hello Twitter!"

# Send Tweet with Text and media ID
client.create_tweet(text=text, media_ids=[media_id])
print("Tweeted!")

I used this script today to post every few hours a new tweet. Please go to the github or yt channel and give them great man a like or comment for his work.