简体   繁体   English

py2app错误运行应用程序

[英]py2app error running app

macOS 10.12 macOS 10.12

I'm trying to package a python script (called getUrls_douyu.py ) as a standalone application/executable without dependencies, so I'm using py2app.我正在尝试将 package 和 python 脚本(称为getUrls_douyu.py )作为没有依赖项的独立应用程序/可执行文件,所以我使用的是 py2app。 The problem is that when I try to run my app after building (from terminal with: open getUrls_douyu.app ), nothing happens and I get the following error prompt:问题是,当我在构建后尝试运行我的应用程序时(从终端: open getUrls_douyu.app ),没有任何反应,我得到以下错误提示:

错误提示图片

The console error:控制台错误:

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.

If I try open getUrls_douyu.app/Contents/MacOS/getUrls_douyu (the executable inside the app bundle), I get a different error:如果我尝试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

But I checked and cacert.pem does indeed exist there, so for some reason the certificate is invalid?但我查了一下,cacert.pem 确实存在,所以由于某种原因证书无效? My.py script uses requests module to get stuff from a webpage which I think must be the issue. My.py 脚本使用requests模块从我认为一定是问题所在的网页中获取内容。 Here's my full python script:这是我的完整 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

When I first built this script I did so in a virtualenv within which I've done pip install requests and pip install beautifulsoup4 and tested that the script successfully ran with no problem.当我第一次构建这个脚本时,我是在一个 virtualenv 中这样做的,我在其中完成了pip install requestspip install beautifulsoup4并测试脚本成功运行没有问题。

This answer to a question regarding the cacert.pem error with the requests module did not help me.这个关于请求模块的 cacert.pem 错误的问题的答案对我没有帮助。 Here is my python script when applying that given solution:这是应用给定解决方案时我的 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

I think I've set up my py2app setup.py file correctly...我想我已经正确设置了我的 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'],
)

Sorry for the verbose question.抱歉这个冗长的问题。 I'm new-ish to python so I imagine I've made some dumb mistake.我是 python 的新手,所以我想我犯了一些愚蠢的错误。 Any help GREATLY appreciated.非常感谢任何帮助。

Try updating py2app options to:尝试将 py2app 选项更新为:

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

This way you're explicitly letting py2app include certifi package.这样你就明确地让 py2app 包含 certifi 包。

If you look at .pem file it's trying to find:如果您查看 .pem 文件,它会尝试找到:

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

It won't be able to find any file in .zip since it's not a folder.它无法在 .zip 中找到任何文件,因为它不是文件夹。

Another tip/trick is to run your app by opening另一个提示/技巧是通过打开来运行您的应用程序

dist/App.app/Contents/MacOS/App dist/App.app/Contents/MacOS/App

This will open up a terminal with logs which help you debug the issue easier.这将打开一个带有日志的终端,帮助您更轻松地调试问题。

A quick workaround: "python setup.py py2app --packages=wx",快速解决方法:“python setup.py py2app --packages=wx”,

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

You have to add 'cifi' in the packages in order for it to run您必须在包中添加“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'],
   )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM