Skip to content Skip to sidebar Skip to footer

Py2exe "requests" Module Missing

I don't know why I can't import package requests. If I execute the script that needs requests library, it crashes obviously. Web of requests library: http://docs.python-requests.or

Solution 1:

If your program crashes with 'FileNotFoundError: [Errno 2] No such file or directory' the issue isn't missing modules, it's a missing file needed for SSL verification, 'cacert.pem'.

Someone explained how to solve this problem in this thread - Requests library: missing file after cx_freeze

I switched to cx_freeze from py2exe because I found it easier to deal with this missing file issue in cx_freeze.

Here's my full cx_freeze code, showing how to fully implement the solution described in the other StackOverflow thread, which will resolve this 'cacert.pem' issue. I'm sure the solution is the same in py2exe, you'd just need to find the py2exe equivalent of the cx_freeze 'include_files'. Maybe someone who knows could chime in to help.

Here's the setup.py file -

from cx_Freeze import setup, Executable
import sys
import requests.certs

base = None
if sys.platform == 'win32':
    base = 'Win32GUI'

build_exe_options = {"include_files":[(requests.certs.where(),'cacert.pem')]}

setup(name='MyProgramName',
      version='1.0',
      description='ProgramDescription',
      options={"build_exe":build_exe_options},
      executables=[Executable('myScript.py',base=base)])

And now everywhere in your script where you make a request to a SSL server, you must add a 'verify' argument, and direct it to the 'cacert.pem' file.

r = requests.post('https://wherever.com', verify='cacert.pem')

or if you don't want SSL verification

r = requests.post('https://wherever.com', verify=False)

The issue is that 'verify' is set to 'True' by default, so that sends it looking for a cacert.pem file which doesn't exist.

I kept the .pem file in the same directory as the executable and was able to link to it directly, verify='cacert.pem'. If this doesn't work for you, or you want to put it in another directory, there are solutions for getting the cwd of your exe file in the other StackOverflow thread.

Post a Comment for "Py2exe "requests" Module Missing"