gpt4 book ai didi

python - py2app 运行应用程序时出错

转载 作者:行者123 更新时间:2023-12-02 03:05:33 29 4
gpt4 key购买 nike

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.

如果我尝试打开 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 很陌生,所以我想我犯了一些愚蠢的错误。非常感谢任何帮助。

最佳答案

尝试将 py2app 选项更新为:

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

这样您就可以明确地让 py2app 包含 certifi 包。

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

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

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

另一个提示/技巧是通过打开来运行您的应用程序

dist/App.app/Contents/MacOS/App

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

关于python - py2app 运行应用程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50091130/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com