Cx\u冻结PySide,praw,请求应用程序在冻结时停止工作

2024-09-27 21:31:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我在praw、cx\u freeze、pyside和requests方面遇到了问题,在冻结之前一切都正常,但是当我冻结时,requests出现了问题,我想: http://pastie.org/10614254

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

这是我的设置.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公司,无济于事,在这一点上我没有更多的想法


Tags: pyhttpsimportgithubtxtcomqtrequests
1条回答
网友
1楼 · 发布于 2024-09-27 21:31:01

对于某些冻结的应用程序,必须在冻结的应用程序中设置cacert(或通常的外部数据)路径。你知道吗

设置.py章节

首先需要将其包含在构建选项中,并手动指定安装目录。这是唯一一个进入设置.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,在运行时定义了一个路径,指向名为\u MEIPASS的数据目录(可以从sys.\u MEIPASS获得),允许您访问应用程序所需的所有数据。如果是cacert.pem公司,路径确定如下:

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

对于cx\u 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)

然后将此路径包括在“所有请求”模块获取和发布请求中,如下所示:

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

相关问题 更多 >

    热门问题