py2exe“请求"缺少模块 [英] py2exe "requests" module missing
问题描述
我不知道为什么不能导入包requests
.
如果我执行需要requests
库的脚本,它显然会崩溃.
Web请求库:
http://docs.python-requests.org/en/latest/ >
Py2exe版本0.9.2.2
版本Python 3.4.3
我尝试使用py2exe的其他选项,例如-i requests
,甚至尝试使用setup.py
形式,但是我无法使其正常工作.
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'.
如果程序崩溃, 'FileNotFoundError:[Errno 2]没有这样的文件或目录' 问题不是缺少模块,而是SSL验证所需的文件"cacert.pem".
有人在此线程中解释了如何解决此问题- 请求库:cx_freeze后缺少文件
我从py2exe切换到cx_freeze,因为我发现在cx_freeze中更容易处理此丢失的文件问题.
这是我完整的cx_freeze代码,显示了如何完全实现另一个StackOverflow线程中描述的解决方案,它将解决此"cacert.pem"问题.我确定py2exe中的解决方案相同,您只需要找到与cx_freeze'include_files'等效的py2exe.也许知道的人可以帮忙.
这是setup.py文件-
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)])
现在在脚本中向SSL服务器发出请求的任何地方,都必须添加"verify"参数,并将其定向到"cacert.pem"文件.
r = requests.post('https://wherever.com', verify='cacert.pem')
或者如果您不想进行SSL验证
r = requests.post('https://wherever.com', verify=False)
问题是默认情况下'verify'设置为'True',以便发送它查找不存在的cacert.pem文件.
我将.pem文件保存在与可执行文件相同的目录中,并能够直接链接到该文件,并验证='cacert.pem'.如果这对您不起作用,或者您想将其放在另一个目录中,则可以使用一些解决方案在另一个StackOverflow线程中获取exe文件的cwd.
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'.
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.
这篇关于py2exe“请求"缺少模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!