Py2app运行应用程序出现错误。

3

macOS 10.12

我试图将一个名为getUrls_douyu.py的Python脚本打包成一个没有依赖的独立应用程序/可执行文件,因此我使用了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.

如果我尝试打开应用程序包内部的可执行文件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

当我第一次编写这个脚本时,我在虚拟环境中操作,其中我使用了pip install requestspip install beautifulsoup4。并且测试脚本成功运行没有问题。
这个答案针对requests模块出现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还不太熟悉,所以可能犯了一些低级错误。非常感谢您的帮助。

3个回答

2
尝试更新py2app选项为:
OPTIONS = {
    'packages': ['certifi',]
}

这样做可以明确地让py2app包含certifi软件包。
如果您查看.pem文件,它正在尝试查找:
../site-packages.zip/certifi/cacert.pem
由于它不是文件夹,所以它将无法在.zip中找到任何文件。
另一个技巧是通过打开dist/App.app/Contents/MacOS/App来运行应用程序。这将打开一个带有日志的终端,帮助您更轻松地调试问题。

0

-1

你必须在包中添加“cifi”才能运行

from setuptools import setup
APP = ['']  # file name
DATA_FILES = []

OPTIONS = {

   'iconfile': '', # icon
   'argv_emulation': True,
   'packages': ['certifi', 'cffi'], # you need to add cffi
}

 setup(

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

目前你的回答不够清晰,请[编辑]以添加更多细节,帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接