Cx_Freezing PySide,大虾,请求应用程序在冻结时停止工作

Cx_Freezing a PySide, praw, requests application stops working when frozen

我在处理 praw、cx_freeze、pyside 和请求时遇到问题,在冻结之前一切正常,但是当我冻结时发生这种情况,我认为请求出现问题: http://pastie.org/10614254

这是我正在处理的项目:https://github.com/MrCappuccino/WallDit-QT

这是我的setup.py:https://gist.github.com/MrCappuccino/0f1b0571d29d47a95895

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

exe = Executable(
      script="WallDit_QT.py",
      base="Win32GUI",
      targetName="WallDit_QT.exe"
     )

#includefiles = ['README.txt', 'CHANGELOG.txt', 'helpers\uncompress\unRAR.exe', , 'helpers\uncompress\unzip.exe']
#build_exe_options = {"packages": ["os"], "includefiles": ['README.txt', 'CHANGELOG.txt']}

setup(name = 'WallDit_QT',
  version = '1.0',
  author = 'Disco Dolan',
  description ='Set your wallpaper interactively!',
  executables = [exe],
  options = {'build.exe': {"include_files":['cacert.pem', 'praw.ini', 'README.md']}},
  requires = ['PySide', 'cx_Freeze', 'praw', 'shutil', 'requests']
)

有人能帮忙吗?

我试过添加cacert.pem,无济于事,此时我没有更多的想法

对于某些冻结的应用程序,您必须在冻结的应用程序中设置 cacert(或一般的外部数据)路径。

Setup.py节

您首先需要将其包含在构建选项中并手动指定安装目录。这是 setup.py:

中唯一的部分
# notice how I say the folder the certificate is installed
{"include_files":[(requests.certs.where(),'cacert.pem')]}

对于您的情况,这将生成以下安装文件:

import requests
import sys
# more imports

setup(name = 'WallDit_QT',
    version = '1.0',
    author = 'Disco Dolan',
    description ='Set your wallpaper interactively!',
    executables = [exe],
    options = {
        'build.exe': {
            "include_files": [
                (requests.certs.where(),'cacert.pem'), 
                'praw.ini', 
                'README.md'
            ]
        }
    },
    requires = ['PySide', 'cx_Freeze', 'praw', 'shutil', 'requests']
)

申请部分

然后您需要在运行时在冻结的应用程序中获取证书路径。 对于 PyInstaller,在运行时定义了一个路径,指向名为 _MEIPASS 的数据目录(可以从 sys._MEIPASS 获得),允许您访问应用程序所需的所有数据。在cacert.pem的情况下,路径将确定如下:

cacertpath = os.path.join(sys._MEIPASS, "cacert.pem")

对于cx_Freeze,可以根据安装路径确定路径并将其与所需数据相结合。在这里,我们得到的路径如下:

cacertpath = os.path.join(datadir, 'cacert.pem')

您可以通过以下方式轻松获取冻结应用程序的数据目录:

datadir = os.path.dirname(sys.executable)

(请注意,这不适用于非冻结的应用程序,因此为确保它适用于冻结和非冻结的应用程序,Cx_Freeze recommends 编码如下):

def find_data_file(filename):
    if getattr(sys, 'frozen', False):
        # The application is frozen
        datadir = os.path.dirname(sys.executable)
    else:
        # The application is not frozen
        # Change this bit to match where you store your data files:
        datadir = os.path.dirname(__file__)

    return os.path.join(datadir, filename)

然后您将此路径包含在所有请求模块 GET 和 POST 请求中,如下所示:

request.get(url, headers=headers, verify=cacertpath)

示例 1

示例代码片段如下:

# load modules
import os
import sys

import requests

# define our path finder


def find_data_file(filename):
    if getattr(sys, 'frozen', False):
        # The application is frozen
        datadir = os.path.dirname(sys.executable)
    else:
        # The application is not frozen
        # Change this bit to match where you store your data files:
        datadir = os.path.dirname(__file__)

    return os.path.join(datadir, filename)


# get our cacert path and post our GET request
cacertpath = find_data_file('cacert.pem')
r = requests.get('https://api.github.com/events', verify=cacertpath)
# print the text from the request
print(r.text)

示例 2

您还可以通过执行以下操作告诉请求将来在哪里可以找到证书:

os.environ["REQUESTS_CA_BUNDLE"] = cacertpath

在这种情况下,我们将执行以下操作。这里的好处是 cacertpath 不需要在每个模块中显式定义(或从另一个模块导入),可以在环境中定义。

import os
import sys

import requests

def find_data_file(filename):
    if getattr(sys, 'frozen', False):
        # The application is frozen
        datadir = os.path.dirname(sys.executable)
    else:
        # The application is not frozen
        # Change this bit to match where you store your data files:
        datadir = os.path.dirname(__file__)

    return os.path.join(datadir, filename)


cacertpath = find_data_file('cacert.pem')
os.environ["REQUESTS_CA_BUNDLE"] = cacertpath

r = requests.get('https://api.github.com/events')
r.text