py2app运行应用程序时出错

2024-04-26 02:57:09 发布

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

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(app bundle中的可执行文件),我会得到一个不同的错误:

^{pr2}$

但我查过了卡塞特·佩姆是否确实存在,因此由于某种原因证书无效?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 requests和{},并测试了脚本是否成功运行,没有任何问题。在

This answer关于卡塞特·佩姆请求模块出错对我没有帮助。以下是应用给定解决方案时的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了设置.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新手,所以我想我犯了一些愚蠢的错误。非常感谢任何帮助。在


Tags: topyimport脚本outputhtmlopenwhere
2条回答

尝试将py2app选项更新为:

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

这样,就显式地让py2app包含certifi包。在

如果您查看.pem文件,它试图找到:

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

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

另一个提示/窍门是通过打开来运行你的应用程序

dist/App.app/Contents/MacOS/App

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

快速解决方法:“python设置.pypy2app包=wx“

from the author https://bitbucket.org/ronaldoussoren/py2app/issues/252/py2app-creates-broken-bundles-with

相关问题 更多 >