2015-12-06 30 views
1

我遇到了麻烦与praw,cx_freeze,pyside和请求,在冻结一切正常工作之前,但是当我冻结这种情况发生时,请求的东西出错我想: http://pastie.org/10614254Cx_Freezing一个PySide,praw,请求应用程序停止工作时冻结

这是我的工作项目: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,无济于事,在这一点上我没有更多的想法

回答

1

对于一些冷冻的应用程序,你必须设置(一般或外部数据)的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获取)的数据目录,允许您访问应用程序所需的所有数据。

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

对于cx_Freeze,路径可以从安装的路径来确定,并且具有期望的数据加入其:在cacert.pem的情况下,如下所述路径将被确定。在这里,我们得到的路径,如下所示:

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) 

然后,包括这条道路所有请求国防部ULE 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 
+0

我对你的代码块有点困惑,除了最后一个应该在我的setup.py中去的所有这些吗?你也是什么意思的数据文件? –

+0

我会编辑一下。 –

+0

这是否澄清了一点? –