运行应用程序的py2app错误

时间:2018-04-29 20:46:32

标签: python python-requests py2app

macOS 10.12

我正在尝试将python脚本(称为getUrls_douyu.py)打包为一个没有依赖项的独立应用程序/可执行文件,所以我正在使用py2app。问题是,当我尝试在构建之后运行我的应用程序(从终端使用:open getUrls_douyu.app)时,没有任何反应,我得到以下错误提示:

image of error prompt

控制台错误:

Detected missing constraints for <private>.  It cannot be placed because there are not enough constraints to fully define the size and origin. Add the missing constraints, or set translatesAutoresizingMaskIntoConstraints=YES and constraints will be generated for you. If this view is laid out manually on macOS 10.12 and later, you may choose to not call [super layout] from your override. Set a breakpoint on DETECTED_MISSING_CONSTRAINTS to debug. This error will only be logged once.

如果我尝试open getUrls_douyu.app/Contents/MacOS/getUrls_douyu(应用包内的可执行文件),我会收到不同的错误:

IOError: Could not find a suitable TLS CA certificate bundle, invalid path: /Users/<REDACTED>/getUrls_douyu/dist/getUrls_douyu.app/Contents/Resources/lib/python2.7/site-packages.zip/certifi/cacert.pem

但是我检查了cacert.pem确实存在,所以由于某种原因证书无效?我的.py脚本使用requests模块从网页上获取内容,我认为必定是问题。这是我的完整python脚本:

import requests
from bs4 import BeautifulSoup

html = requests.get('https://www.douyu.com/directory/all').text
soup = BeautifulSoup(html, 'html.parser')
urls = soup.select('.play-list-link')

output = '';
output += '[' #open json array
for i, url in enumerate(urls):
    channelName = str(i);
    channelUrl = 'http://douyu.com' + url.get('href')
    output += '{'
    output += '\"channelName\":' + '\"' + channelName.encode('utf-8') + '\",'
    output += '\"channelUrl\":' + '\"' + channelUrl.encode('utf-8') + '\"'
    output += '},'

output = output[:-1]
output += ']'

print output

当我第一次构建这个脚本时,我是在virtualenv中完成的,我已经完成了pip install requestspip install beautifulsoup4并测试了脚本成功运行没有问题。

This answer关于请求模块的cacert.pem错误的问题对我没有帮助。在应用给定的解决方案时,这是我的python脚本:

import requests
from bs4 import BeautifulSoup
import sys, os

def override_where():
    """ overrides certifi.core.where to return actual location of cacert.pem"""
    # change this to match the location of cacert.pem
    return os.path.abspath("cacert.pem")


# is the program compiled?
if hasattr(sys, "frozen"):
    import certifi.core

    os.environ["REQUESTS_CA_BUNDLE"] = override_where()
    certifi.core.where = override_where

    # delay importing until after where() has been replaced
    import requests.utils
    import requests.adapters
    # replace these variables in case these modules were
    # imported before we replaced certifi.core.where
    requests.utils.DEFAULT_CA_BUNDLE_PATH = override_where()
    requests.adapters.DEFAULT_CA_BUNDLE_PATH = override_where()

html = requests.get('https://www.douyu.com/directory/all').text
soup = BeautifulSoup(html, 'html.parser')
urls = soup.select('.play-list-link')

output = '';
output += '[' #open json array
for i, url in enumerate(urls):
    channelName = str(i);
    channelUrl = 'http://douyu.com' + url.get('href')
    output += '{'
    output += '\"channelName\":' + '\"' + channelName.encode('utf-8') + '\",'
    output += '\"channelUrl\":' + '\"' + channelUrl.encode('utf-8') + '\"'
    output += '},'

output = output[:-1]
output += ']'

print output

我想我已经正确设置了我的py2app setup.py文件......

from setuptools import setup

APP = ['getUrls_douyu.py']
DATA_FILES = []
OPTIONS = {}

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)

抱歉这个冗长的问题!我是python的新手,所以我想我犯了一些愚蠢的错误。任何帮助都非常感激。

2 个答案:

答案 0 :(得分:0)

尝试将py2app选项更新为:

OPTIONS = {
    'packages': ['certifi',]
}

这样你明确地让py2app包含certifi包。

如果查看.pem文件,它会尝试查找:

  

../ site-packages.zip/certifi/cacert.pem

它将无法在.zip中找到任何文件,因为它不是文件夹。

另一个提示/技巧是打开

来运行您的应用
  

DIST / App.app /内容/ MacOS的/应用

这将打开一个带有日志的终端,帮助您更轻松地调试问题。

答案 1 :(得分:0)

一种快速的解决方法:“ python setup.py py2app --packages = wx”,

  

作者https://bitbucket.org/ronaldoussoren/py2app/issues/252/py2app-creates-broken-bundles-with