맥북Air M1를 사용하여 코랩에서 딥러닝 학습을 돌려보던 중 GPU 사용량이 초과되어 당분간 코랩 GPU를 사용할 수 없게 되었습니다.
코랩을 사용하지 못하는 동안 임시로 주피터 노트북으로 맥북 GPU를 사용하기로 했습니다.
하지만 주피터 노트북의 파이썬 가상환경에서 코드를 돌리던 중 문제가 발생하였습니다.
💥 문제상황
딥러닝 전이학습을 공부하기 위해 torchvision.models의 efficientnet_b4를 사용하던 중 pth파일을 웹에서 다운로드 받아오는 함수를 실행하는 과정에서 SSL에러가 발생.
에러코드
URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)>
SSL 에러가 발생하는 이유는 '클라이언트의 브라우저가 SSL 인증서를 확인하지 못하거나 신뢰할 수 있다고 확인할 수 없기 때문' 이라고 합니다.
macOS에서 해당 문제를 해결하기 위해 서칭을 했습니다.
🎲 문제 해결
stackoverflow.com에서 해당 문제에 대한 명확한 답을 얻을 수 있었습니다.
1. Open the folder /Applications/Python 3.x (x is the version you are running).
현재 사용하고 있는 Python이 있는 경로를 찾아갑니다.
저의 경우엔 정확하게 위와 같은 경로에 있었습니다 "/Applications/Python 3.10"
2. Double click the Install Certificates.command. It will open a terminal and install the certificate.
해당 경로의 파일들중 "Install Certificates.command" 를 실행합니다.
터미널이 자동으로 실행되며 프로세스가 완료됨을 확인한 후 창을 닫으면 됩니다.
해당 파일은 SSL 모듈을 위한 기본 루트 인증서를 설치하고 업데이트하는 목적의 코드가 담겨있습니다.
코드내용
#!/bin/sh
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10 << "EOF"
# install_certifi.py
#
# sample script to install or update a set of default Root Certificates
# for the ssl module. Uses the certificates provided by the certifi package:
# https://pypi.org/project/certifi/
import os
import os.path
import ssl
import stat
import subprocess
import sys
STAT_0o775 = ( stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
| stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
| stat.S_IROTH | stat.S_IXOTH )
def main():
openssl_dir, openssl_cafile = os.path.split(
ssl.get_default_verify_paths().openssl_cafile)
print(" -- pip install --upgrade certifi")
subprocess.check_call([sys.executable,
"-E", "-s", "-m", "pip", "install", "--upgrade", "certifi"])
import certifi
# change working directory to the default SSL directory
os.chdir(openssl_dir)
relpath_to_certifi_cafile = os.path.relpath(certifi.where())
print(" -- removing any existing file or link")
try:
os.remove(openssl_cafile)
except FileNotFoundError:
pass
print(" -- creating symlink to certifi certificate bundle")
os.symlink(relpath_to_certifi_cafile, openssl_cafile)
print(" -- setting permissions")
os.chmod(openssl_cafile, STAT_0o775)
print(" -- update complete")
if __name__ == '__main__':
main()
EOF
참고한 링크입니다.
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer cer
import yfinance as yf msft = yf.Ticker("MSFT") msft.info I tried to print msft.info and got 'urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify ...
stackoverflow.com
끝내며
SSL 인증 관련 문제는 파이썬 코드 실행을 통해 웹에 접근할 때 주로 발생하는 듯 합니다.
이번 문제는 간단하게 해결되었지만 더 복잡한 인증을 요구하는 경우도 생길 수 있겠다는 생각이 드네요.
SSL 인증에 관한 내용도 차차 공부해서 정보 전달을 해드리도록 하겠습니다.
혹시 위 해결방법을 사용해도 해결되지 않는다면 댓글 남겨주세요!
'파이썬 > 심화' 카테고리의 다른 글
matplotlib Customizing: backend (0) | 2024.03.16 |
---|---|
[파이썬] in의 시간복잡도와 set의 자료구조에 대한 궁금증 (0) | 2024.01.12 |
댓글