Python Cx_冻结PySide、praw请求应用程序在冻结时停止工作

Python Cx_冻结PySide、praw请求应用程序在冻结时停止工作,python,qt,python-requests,pyside,cx-freeze,Python,Qt,Python Requests,Pyside,Cx Freeze,在冻结前,我遇到了praw、cx_冻结、pyside和请求方面的问题,一切正常,但当我冻结这种情况时,请求出现了问题,我认为: 这是我正在处理的项目: 这是我的setup.py: 有人能帮忙吗 我已尝试添加cacert.pem,但没有效果,此时我没有更多想法对于某些冻结的应用程序,您必须在冻结的应用程序内设置cacert(或一般的外部数据)路径 Setup.py部分 首先需要将其包含在构建选项中,并手动指定安装目录。这是进入setup.py的唯一部分: # notice how I say t

在冻结前,我遇到了praw、cx_冻结、pyside和请求方面的问题,一切正常,但当我冻结这种情况时,请求出现了问题,我认为:

这是我正在处理的项目:

这是我的setup.py:

有人能帮忙吗


我已尝试添加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")
cacertpath = os.path.join(datadir, 'cacert.pem')
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)
# 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)
对于cx_Freeze,可以通过安装路径并将其与所需数据连接来确定路径。在这里,我们得到如下路径:

cacertpath = os.path.join(sys._MEIPASS, "cacert.pem")
cacertpath = os.path.join(datadir, 'cacert.pem')
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)
# 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)
您可以通过以下方式轻松获取冻结应用程序的数据目录:

datadir = os.path.dirname(sys.executable)
os.environ["REQUESTS_CA_BUNDLE"] = cacertpath
(请注意,这不适用于非冻结的应用程序,因此,为了确保它同时适用于冻结和非冻结的应用程序,请按如下方式对其进行编码):

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

cacertpath = os.path.join(sys._MEIPASS, "cacert.pem")
cacertpath = os.path.join(datadir, 'cacert.pem')
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)
# 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)
示例1

示例代码段如下所示:

cacertpath = os.path.join(sys._MEIPASS, "cacert.pem")
cacertpath = os.path.join(datadir, 'cacert.pem')
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)
# 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

您还可以通过执行以下操作告诉请求将来在何处查找证书:

datadir = os.path.dirname(sys.executable)
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

我对您的代码块有点困惑,除了最后一个之外,所有这些都应该放在我的setup.py中吗?还有,你所说的数据文件是什么意思?这就澄清了一点吗?除了“datadir=os.path.dirname(file)”这一部分之外,我什么都懂。我如何知道我的数据文件存储在哪里,以及数据文件是什么意思?
\uuuuuu file\uuuu
是一个Python内置关键字,用于未冻结的模块。因此,os.path.dirname(
\uuuuuuuuuuuuuuuuu文件
)给出了正在解释的当前文件的系统路径。如果应用程序被冻结,则会删除
\uuuu file\uuu
变量,但sys.executable会为您提供冻结应用程序的路径。我尝试过您的方法,但它似乎无法正常工作,我收到以下错误消息:updated walldit.py: