我有一个使用外部模块的简单python 2.7脚本("请求").. 我使用py2exe并在运行exe时遇到问题。
test.py code :::
import requests
r = requests.get('https://api.github.com/')
print r.status_code
print r.text
setup.py code ::::
from distutils.core import setup
import py2exe
setup(console=['youtube.py'], options = {'py2exe': { 'packages':['requests']}})
py2exe适用于内部模块..但是外部模块的错误.. 错误日志::
D:\dist>youtube.exe
Traceback (most recent call last):
File "youtube.py", line 2, in <module>
File "requests\api.pyc", line 69, in get
File "requests\api.pyc", line 50, in request
File "requests\sessions.pyc", line 465, in request
File "requests\sessions.pyc", line 573, in send
File "requests\adapters.pyc", line 431, in send
requests.exceptions.SSLError: [Errno 2] No such file or directory
答案 0 :(得分:1)
问题是函数requests.certs.where
在编译时为名为cacert.pem
的文件返回错误的路径。使用requests.utils.DEFAULT_CA_BUNDLE_PATH
设置requests.certs.where()
,然后由其他各种函数导入该变量。要解决此问题,您可以将C:\Python27\Lib\site-packages\requests\cacert.pem
复制到包含您的exe的目录,然后将该位置硬编码到您的requests.utils
文件
from os.path import join, abspath
DEFAULT_CA_BUNDLE_PATH = join(abspath('.'), 'cacert.pem')
或来自您的主模块
import requests
from os.path import join, abspath
requests.utils.DEFAULT_CA_BUNDLE_PATH = join(abspath('.'), 'cacert.pem')
答案 1 :(得分:0)
对此有何回应,但在您的请求中尝试设置verify = False:
import requests
r = requests.get('https://api.github.com/', verify=False)
print r.status_code
print r.text