PyInstaller 'no module named certifi' erro

2019-09-07 07:19发布

I'm trying to compile a Python program using PyInstaller. When I try to use a function that uses ssl, a get an Import Error:

ImportError: No module named certifi

I've modified the .spec file in order to include the cacert.pem file from Python ssl library:

# -*- mode: python -*-
a = Analysis(['main.py'],
             pathex=['D:\\Projects\\soundcloud'],
             hiddenimports=[],
             hookspath=None)
a.datas.append(('cacert.pem', 'cacert.pem', 'DATA'))
pyz = PYZ(a.pure)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=1,
          name=os.path.join('build\\pyi.win32\\main', 'main.exe'),
          debug=False,
          strip=None,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=None,
               upx=True,
               name=os.path.join('dist', 'main'))

But then I get the following error:

Traceback (most recent call last):
File "...\soundcloud\build\pyi.win32\main\out00-PYZ.pyz\api", line 186,
in run_api_thread
File "...\soundcloud\build\pyi.win32\main\out00-PYZ.pyz\api", line 40, 
in authenticate
File "...\soundcloud\build\pyi.win32\main\out00-PYZ.pyz\soundcloud.client", 
line 60,
in exchange_token
File "...\soundcloud\build\pyi.win32\main\out00-PYZ.pyz\soundcloud.request", 
line 173, in make_request
File "...\soundcloud\build\pyi.win32\main\out00-PYZ.pyz\requests.api", 
line 84, in post
File "...\soundcloud\build\pyi.win32\main\out00-PYZ.pyz\requests.api", 
line 39, in request
File "D:\Projects\soundcloud\build\pyi.win32\main\out00-PYZ.pyz\requests.sessions"
line 200, in request
File "D:\Projects\soundcloud\build\pyi.win32\main\out00-PYZ.pyz\requests.models",
line 537, in send
SSLError: [Errno 185090050] _ssl.c:336: 
error:0B084002:x509 certificate routines:X509_load_cert_crl_file:system lib

What is the right way to include the certificate in to make ssl work when compiled with PyInstaller?

1条回答
Lonely孤独者°
2楼-- · 2019-09-07 07:52

The problem was solved with by changing the environment variable:

os.environ['REQUESTS_CA_BUNDLE'] = os.path.join(os.getcwd(), 'cacert.pem')
查看更多
登录 后发表回答