问题
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.org/en/latest/
Version Py2exe 0.9.2.2
Version Python 3.4.3
I tried to use other options of py2exe as like -i requests
I've even tried using the setup.py
form, but I can't make it work.
py -3.4 -m py2exe.build_exe script.py --bundle-files 3
24 missing Modules
------------------
? Cookie imported from requests.compat
? OpenSSL imported from requests.packages.urllib3.contrib.pyopenssl
? Queue imported from requests.packages.urllib3.connectionpool
? _abcoll imported from requests.packages.urllib3.packages.ordered_dict
? backports imported from requests.packages.urllib3.packages.ssl_match_hostname
? certifi imported from requests.certs
? cookielib imported from requests.compat
? dummy_thread imported from requests.packages.urllib3.packages.ordered_dict
? ndg imported from requests.packages.urllib3.contrib.pyopenssl
? netbios imported from uuid
? pyasn1 imported from requests.packages.urllib3.contrib.pyopenssl
? readline imported from cmd, code, pdb
? simplejson imported from requests.compat
? thread imported from requests.packages.urllib3.packages.ordered_dict
? urllib.getproxies imported from requests.compat
? urllib.proxy_bypass imported from requests.compat
? urllib.quote imported from requests.compat
? urllib.quote_plus imported from requests.compat
? urllib.unquote imported from requests.compat
? urllib.unquote_plus imported from requests.compat
? urllib.urlencode imported from requests.compat, requests.packages.urllib3.request
? win32api imported from platform
? win32con imported from platform
? win32wnet imported from uuid
Building 'dist\script.exe'.
回答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.
来源:https://stackoverflow.com/questions/32156802/py2exe-requests-module-missing