PyInstaller SSL error with requests module - missing module ssl (_ssl)

Viewed 2264

In my python script, I use the requests module to call an API to GET and POST data.

Python environment: anaconda3, python 3.7 / packages: requests 2.24.0, pyinstaller 3.6, openssl 1.1.1h...

I have the following problem with the EXE-file generated by PyInstaller: When I run this file, I get the following error message. This error doesn't occur when I run my script from Python:

Traceback (most recent call last):
  File "site-packages\PyInstaller\loader\rthooks\pyi_rth_certifi.py", line 13, in <module>
  File "c:\programdata\anaconda3\envs\...\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
  File "ssl.py", line 98, in <module>
ImportError: DLL load failed: The specified module could not be found.
[20188] Failed to execute script pyi_rth_certifi

If I follow to the excepted error lines (ssl.py):

import _ssl             # if we can't import it, let the error propagate

Why can't ssl be imported?

I searched a long time in serveral post on SO but all these answers didn't help, ex.:

Python Requests throwing SSLError

Python Requests - How to use system ca-certificates (debian/ubuntu)?

Twilio Python Module Errors After Compiling

Fixing SSL certificate error in exe compiled with py2exe (or PyInstaller)

Does anyone have an idea how to fix this? If you need more informations, please write a comment. THX


EDIT: (for Mooncrater's comment)

added Screenshot from python console, entered:

import _ssl

import _ssl


EDIT 2:

Tested "FIX" from SO Question: Python SSL Import Error in PyInstaller generated executable

-> this didn't fix my problem


EDIT 3: Answer by cbolwerk

THX for your answer, this works!

Python 3.7 anaconda environment - import _ssl DLL load fail error

An additional fix is to install the latest OpenSSL Lib, I used 1.1.1h package in my python script, on my PC was installed an older version and that causes the error too.

cbolwerk's answer I tested on a PC where NO OpenSSL is installed and , as I wrote, it works!

1 Answers

If you can import ssl in the python within your environment, it means that the ssl module is probably inside your environment. This answer mentions the files you can look for inside your environment. They are either inside your env/Library/bin or env/DLLs. I think you have these installed, but pyinstaller doesn't recognize them. To make sure that pyinstaller knows these files, you can add them to datas. This can be edited in the command when building the .exe file or in the .spec file that is probably created when you have run this command once. This link may be useful there.

In short, add the paths of the DLLs I mentioned to the datas (or binaries inside .spec actually) so that they are recognized by pyinstaller.

So either run the command

pyinstaller --onedir --add-data libcrypto-1_1-x64.dll;. --add-data libssl-1_1-x64.dll;. myscript.py

or change your .spec to something like

datas = [('/path/to/libcrypto-1_1-x64.dll', '.'), ('/path/to/libssl-1_1-x64.dll', '.'),
          (...) ]
...
a = Analysis(...,
             datas=datas,
             ...)
...

and then run

pyinstaller --onedir myscript.spec

This fixed DLL issues for me at least.

Related