macOS 10.12
我试图将一个名为getUrls_douyu.py
的Python脚本打包成一个没有依赖的独立应用程序/可执行文件,因此我使用了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.
如果我尝试打开应用程序包内部的可执行文件
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 requests
和pip 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还不太熟悉,所以可能犯了一些低级错误。非常感谢您的帮助。