Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ch02.ipynb #403

Closed
wants to merge 1 commit into from
Closed

Update ch02.ipynb #403

wants to merge 1 commit into from

Conversation

krouser
Copy link

@krouser krouser commented Oct 17, 2024

I've added this peice to the code to handle ssl error on VSCode:

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)

I've added this peice to the code to handle ssl error on VSCode:

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@rasbt
Copy link
Owner

rasbt commented Oct 19, 2024

Hi there, and thanks for adding this PR!

By the way, I don't think it's a VSCode issue per se. Usually, this code shouldn't be necessary.

I suspect what might have happened is that VSCode accidentally defaulted to an old Python version. Could you kindly double check which Python version you are using in VSCode and whether it's older then say 3.9?

Screenshot 2024-10-19 at 9 28 47 AM

PS: May I also ask which operating system you are using? In case this is still an issue with a newer Python version, depending on your operating system, there are also global ways to fix the certificate issue. Perhaps the easiest and most universal one is pip install certifi.

@krouser
Copy link
Author

krouser commented Oct 19, 2024

Screenshot 2024-10-19 204438
Thank you, its was a problem with the python version...

@rasbt
Copy link
Owner

rasbt commented Oct 19, 2024

Nice, I am glad it got resolved!

@rasbt rasbt closed this Oct 19, 2024
@vplusg
Copy link

vplusg commented Mar 11, 2025

I've added this peice to the code to handle ssl error on VSCode:

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)

I tried the solution above but it didn't work for me I am afraid.
ssl._create_default_https_context = ssl._create_stdlib_context
Not sure if there were some implict steps that weren't mentioned that I missed.

I had to create a context using SSLContext. Then using urllib I created a handler and an opener and then installed it globally only then
urllib.request.urlretrieve(url, file_path)
worked for me otherwise I continued to get
URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)>

Here is my code:

import os
import urllib.request
import ssl

# Creating an SSL context to disable SSL verification
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE

# Creating an HTTPS handler using the context
my_https_handler = urllib.request.HTTPSHandler(context=context)

# Creating opener with the HTTPS handler
my_opener = urllib.request.build_opener(my_https_handler)

# Install the opener globally
urllib.request.install_opener(my_opener)

print("Checking if file already exists")
file_path = "./the-verdict.txt"
# Now, urlretrieve will use the context
if not os.path.exists(file_path):
    print("File doesn't exist.")
    url = ("https://raw.githubusercontent.com/rasbt/"
           "LLMs-from-scratch/main/ch02/01_main-chapter-code/"
           "the-verdict.txt")
    print("Retrieving file ...")
    urllib.request.urlretrieve(url, file_path)
    print("Download complete to "+ file_path)
else:
    print("File exists")

# Reverting opener to the default opener
urllib.request.install_opener(urllib.request.build_opener())

@rasbt
Copy link
Owner

rasbt commented Mar 15, 2025

Thanks for sharing and sorry about the trouble. May I ask what version of Python you are/were using? I think Python versions below 3.10 may have some SSL issues and you'd either need to upgrade Python or update the underlying packages:

pip install --upgrade certifi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants