【发布时间】:2018-10-09 23:52:51
【问题描述】:
macOS 10.12
我正在尝试将 python 脚本(称为getUrls_douyu.py)打包为独立的应用程序/可执行文件,没有依赖项,所以我使用的是 py2app。问题是,当我在构建后尝试运行我的应用程序时(从终端使用:open getUrls_douyu.app),没有任何反应,并且我收到以下错误提示:
控制台错误:
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 requests 和 pip install beautifulsoup4 并测试了脚本成功运行没有问题。
This answer 询问有关 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很陌生,所以我想我犯了一些愚蠢的错误。非常感谢任何帮助。
【问题讨论】:
标签: python python-requests py2app