使用 Python 和 py2app 创建独立的 macOS 应用程序

phi*_*uix 5 python macos executable py2app

我想创建一个独立的应用程序,它可以在我的以外的其他 Mac 上全局使用。

我遵循了这个页面的教程:https : //www.metachris.com/2015/11/create-standalone-mac-os-x-applications-with-python-and-py2app/

但是,在构建部署步骤完成后,我想通过双击在 dist 文件夹中运行该应用程序,我收到此错误消息:

"*MYAPP* has encountered a fatal error, and will not terminate.

A Python runtime not could be located. You may need to install a framework build of Python, or edit the PyRuntimeLocations array in this application's Info.plist file"
Run Code Online (Sandbox Code Playgroud)

--> 我假设我必须在我的虚拟环境中安装 Python。此外,我知道为了解决这个问题,也许我必须使用“Macports”。我通过关注和阅读此线程并关注问题根源来了解这一点:py2app is not copy the Python.framework to the new app while using virutalenv

但即使阅读后我也不明白:

--> 如何在我的虚拟环境中安装 Python 以便为 Mac 创建独立的可执行应用程序。

当我在终端中执行它时,我的 Python 脚本运行得很好,而且我没有为这个小应用程序使用任何包,所以不应该有来自其他包的任何依赖项。

希望有人可以提供帮助。

谢谢!

---编辑--- 这是我的 setup.py 文件:

"""
This is a setup.py script generated by py2applet

Usage:
    python setup.py py2app
"""

from setuptools import setup

APP = ['Sandwich.py']
DATA_FILES = []
OPTIONS = {}

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

Run Code Online (Sandbox Code Playgroud)

此外,这里是方向树的概述:

??? Sandwich.py
??? build
?   ??? bdist.macosx-10.13-intel
?       ??? python2.7-standalone
?           ??? app
?               ??? Frameworks
?               ??? collect
?               ?   ??? Carbon
?               ?   ?   ??? AE.pyc
?               ?   ?   ??? Appearance.pyc
?               ?   ?   ??? AppleEvents.pyc
?               ?   ?   ??? CarbonEvents.pyc
?               ?   ?   ??? ControlAccessor.pyc
?               ?   ?   ??? Controls.pyc
?               ?   ?   ??? Ctl.pyc
?               ?   ?   ??? Dialogs.pyc
?               ?   ?   ??? Dlg.pyc
?               ?   ?   ??? Dragconst.pyc
?               ?   ?   ??? Events.pyc
?               ?   ?   ??? Evt.pyc
?               ?   ?   ??? File.pyc
?               ?   ?   ??? Files.pyc
?               ?   ?   ??? Menu.pyc
?               ?   ?   ??? Qd.pyc
?               ?   ?   ??? QuickDraw.pyc
?               ?   ?   ??? Res.pyc
?               ?   ?   ??? TextEdit.pyc
?               ?   ?   ??? Win.pyc
?               ?   ?   ??? Windows.pyc
?               ?   ?   ??? __init__.pyc
?               ?   ??? EasyDialogs.pyc
?               ?   ??? Finder
?               ?   ?   ??? Containers_and_folders.pyc
?               ?   ?   ??? Enumerations.pyc
?               ?   ?   ??? Files.pyc
?               ?   ?   ??? Finder_Basics.pyc
?               ?   ?   ??? Finder_items.pyc
?               ?   ?   ??? Legacy_suite.pyc
?               ?   ?   ??? Standard_Suite.pyc
?               ?   ?   ??? Type_Definitions.pyc
?               ?   ?   ??? Window_classes.pyc
?               ?   ?   ??? __init__.pyc
?               ?   ??? FixTk.pyc
?               ?   ??? StdSuites
?               ?   ?   ??? AppleScript_Suite.pyc
?               ?   ?   ??? Macintosh_Connectivity_Clas.pyc
?               ?   ?   ??? QuickDraw_Graphics_Suite.pyc
?               ?   ?   ??? QuickDraw_Graphics_Suppleme.pyc
?               ?   ?   ??? Required_Suite.pyc
?               ?   ?   ??? Standard_Suite.pyc
?               ?   ?   ??? Table_Suite.pyc
?               ?   ?   ??? Text_Suite.pyc
?               ?   ?   ??? Type_Names_Suite.pyc
?               ?   ?   ??? __init__.pyc
?               ?   ??? StringIO.pyc
?               ?   ??? Tkconstants.pyc
?               ?   ??? Tkinter.pyc
?               ?   ??? UserDict.pyc
?               ?   ??? _LWPCookieJar.pyc
?               ?   ??? _MozillaCookieJar.pyc
?               ?   ??? __future__.pyc
?               ?   ??? _abcoll.pyc
?               ?   ??? _builtinSuites
?               ?   ?   ??? __init__.pyc
?               ?   ?   ??? builtin_Suite.pyc
?               ?   ??? _markerlib
?               ?   ?   ??? __init__.pyc
?               ?   ?   ??? markers.pyc
?               ?   ??? _osx_support.pyc
?               ?   ??? _strptime.pyc
?               ?   ??? _sysconfigdata.pyc
?               ?   ??? _threading_local.pyc
?               ?   ??? _weakrefset.pyc
?               ?   ??? abc.pyc
?               ?   ??? aepack.pyc
?               ?   ??? aetools.pyc
?               ?   ??? aetypes.pyc
?               ?   ??? aifc.pyc
?               ?   ??? applesingle.pyc
?               ?   ??? ast.pyc
?               ?   ??? atexit.pyc
?               ?   ??? base64.pyc
?               ?   ??? bdb.pyc
?               ?   ??? bisect.pyc
?               ?   ??? calendar.pyc
?               ?   ??? chunk.pyc
?               ?   ??? cmd.pyc
?               ?   ??? codecs.pyc
?               ?   ??? collections.pyc
?               ?   ??? contextlib.pyc
?               ?   ??? cookielib.pyc
?               ?   ??? copy.pyc
?               ?   ??? copy_reg.pyc
?               ?   ??? ctypes
?               ?   ?   ??? __init__.pyc
?               ?   ?   ??? _endian.pyc
?               ?   ?   ??? macholib
?               ?   ?   ?   ??? README.ctypes
?               ?   ?   ?   ??? __init__.pyc
?               ?   ?   ?   ??? dyld.pyc
?               ?   ?   ?   ??? dylib.pyc
?               ?   ?   ?   ??? fetch_macholib
?               ?   ?   ?   ??? fetch_macholib.bat
?               ?   ?   ?   ??? framework.pyc
?               ?   ?   ??? util.pyc
?               ?   ?   ??? wintypes.pyc
?               ?   ??? difflib.pyc
?               ?   ??? dis.pyc
?               ?   ??? distutils
?               ?   ?   ??? README
?               ?   ?   ??? __init__.pyc
?               ?   ?   ??? debug.pyc
?               ?   ?   ??? dep_util.pyc
?               ?   ?   ??? errors.pyc
?               ?   ?   ??? log.pyc
?               ?   ?   ??? spawn.pyc
?               ?   ?   ??? sysconfig.pyc
?               ?   ?   ??? text_file.pyc
?               ?   ?   ??? util.pyc
?               ?   ??? doctest.pyc
?               ?   ??? dummy_thread.pyc
?               ?   ??? dummy_threading.pyc
?               ?   ??? encodings
?               ?   ?   ??? __init__.pyc
?               ?   ?   ??? aliases.pyc
?               ?   ?   ??? ascii.pyc
?               ?   ?   ??? base64_codec.pyc
?               ?   ?   ??? big5.pyc
?               ?   ?   ??? big5hkscs.pyc
?               ?   ?   ??? bz2_codec.pyc
?               ?   ?   ??? charmap.pyc
?               ?   ?   ??? cp037.pyc
?               ?   ?   ??? cp1006.pyc
?               ?   ?   ??? cp1026.pyc
?               ?   ?   ??? cp1140.pyc
?               ?   ?   ??? cp1250.pyc
?               ?   ?   ??? cp1251.pyc
?               ?   ?   ??? cp1252.pyc
?               ?   ?   ??? cp1253.pyc
?               ?   ?   ??? cp1254.pyc
?               ?   ?   ??? cp1255.pyc
?               ?   ?   ??? cp1256.pyc
?               ?   ?   ??? cp1257.pyc
?               ?   ?   ??? cp1258.pyc
?               ?   ?   ??? cp424.pyc
?               ?   ?   ??? cp437.pyc
?               ?   ?   ??? cp500.pyc
?               ?   ?   ??? cp720.pyc
?               ?   ?   ??? cp737.pyc
?               ?   ?   ??? cp775.pyc
?               ?   ?   ??? cp850.pyc
?               ?   ?   ??? cp852.pyc
?               ?   ?   ??? cp855.pyc
?               ?   ?   ??? cp856.pyc
?               ?   ?   ??? cp857.pyc
?               ?   ?   ??? cp858.pyc
?               ?   ?   ??? cp860.pyc
?               ?   ?   ??? cp861.pyc
?               ?   ?   ??? cp862.pyc
?               ?   ?   ??? cp863.pyc
?               ?   ?   ??? cp864.pyc
?               ?   ?   ??? cp865.pyc
?               ?   ?   ??? cp866.pyc
?               ?   ?   ??? cp869.pyc
?               ?   ?   ??? cp874.pyc
?               ?   ?   ??? cp875.pyc
?               ?   ?   ??? cp932.pyc
?               ?   ?   ??? cp949.pyc
?               ?   ?   ??? cp950.pyc
?               ?   ?   ??? euc_jis_2004.pyc
?               ?   ?   ??? euc_jisx0213.pyc
?               ?   ?   ??? euc_jp.pyc
?               ?   ?   ??? euc_kr.pyc
?               ?   ?   ??? gb18030.pyc
?               
??? dist
?   ??? Sandwich.app
?       ??? Contents
?           ??? Info.plist
?           ??? MacOS
?           ?   ??? Sandwich
?           ??? PkgInfo
?           ??? Resources
??? setup.py
??? venv
    ??? bin
    ?   ??? activate
    ?   ??? activate.csh
    ?   ??? activate.fish
    ?   ??? activate.ps1
    ?   ??? activate.xsh
    ?   ??? activate_this.py
    ?   ??? easy_install
    ?   ??? easy_install-3.8
    ?   ??? easy_install3
    ?   ??? pip
    ?   ??? pip-3.8
    ?   ??? pip3
    ?   ??? pip3.8
    ?   ??? python -> /Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8
    ?   ??? python3 -> python
    ?   ??? python3.8 -> python
    ?   ??? wheel
    ?   ??? wheel-3.8
    ?   ??? wheel3
    ??? lib
    ?   ??? python3.8
    ?       ??? site-packages
    ?           ??? _virtualenv.pth
    ?           ??? _virtualenv.py
    ?           ??? easy_install.py
    ?           ??? pip
    ?           ?   ??? __init__.py
    ?           ?   ??? __main__.py
    ?           ?   ??? _internal
    ?           ?   ?   ??? __init__.py
    ?           ?   ?   ??? build_env.py
    ?           ?   ?   ??? cache.py
    ?           ?   ?   ??? cli
    ?           ?   ?   ?   ??? __init__.py
    ?           ?   ?   ?   ??? autocompletion.py
    ?           ?   ?   ?   ??? base_command.py
    ?           ?   ?   ?   ??? cmdoptions.py
    ?           ?   ?   ?   ??? command_context.py
    ?           ?   ?   ?   ??? main.py
    ?           ?   ?   ?   ??? main_parser.py
    ?           ?   ?   ?   ??? parser.py
    ?           ?   ?   ?   ??? req_command.py
    ?           ?   ?   ?   ??? status_codes.py
    ?           ?   ?   ??? commands
    ?           ?   ?   ?   ??? __init__.py
    ?           ?   ?   ?   ??? check.py
    ?           ?   ?   ?   ??? completion.py
    ?           ?   ?   ?   ??? configuration.py
    ?           ?   ?   ?   ??? debug.py
    ?           ?   ?   ?   ??? download.py
    ?           ?   ?   ?   ??? freeze.py
    ?           ?   ?   ?   ??? hash.py
    ?           ?   ?   ?   ??? help.py
    ?           ?   ?   ?   ??? install.py
    ?           ?   ?   ?   ??? list.py
    ?           ?   ?   ?   ??? search.py
    ?           ?   ?   ?   ??? show.py
    ?           ?   ?   ?   ??? uninstall.py
    ?           ?   ?   ?   ??? wheel.py
    ?           ?   ?   ??? configuration.py
    ?           ?   ?   ??? distributions
    ?           ?   ?   ?   ??? __init__.py
    ?           ?   ?   ?   ??? base.py
    ?           ?   ?   ?   ??? installed.py
    ?           ?   ?   ?   ??? sdist.py
    ?           ?   ?   ?   ??? wheel.py
    ?           ?   ?   ??? exceptions.py
    ?           ?   ?   ??? index
    ?           ?   ?   ?   ??? __init__.py
    ?           ?   ?   ?   ??? collector.py
    ?           ?   ?   ?   ??? package_finder.py
    ?           ?   ?   ??? legacy_resolve.py
    ?           ?   ?   ??? locations.py
    ?           ?   ?   ??? main.py
    ?           ?   ?   ??? models
    ?           ?   ?   ?   ??? __init__.py
    ?           ?   ?   ?   ??? candidate.py
    ?           ?   ?   ?   ??? format_control.py
    ?           ?   ?   ?   ??? index.py
    ?           ?   ?   ?   ??? link.py
    ?           ?   ?   ?   ??? scheme.py
    ?           ?   ?   ?   ??? search_scope.py
    ?           ?   ?   ?   ??? selection_prefs.py
    ?           ?   ?   ?   ??? target_python.py
    ?           ?   ?   ?   ??? wheel.py
    ?           ?   ?   ??? network
    ?           ?   ?   ?   ??? __init__.py
    ?           ?   ?   ?   ??? auth.py
    ?           ?   ?   ?   ??? cache.py
    ?           ?   ?   ?   ??? download.py
    ?           ?   ?   ?   ??? session.py
    ?           ?   ?   ?   ??? utils.py
    ?           ?   ?   ?   ??? xmlrpc.py
    ?           ?   ?   ??? operations
    ?           ?   ?   ?   ??? __init__.py
    ?           ?   ?   ?   ??? build
    ?           ?   ?   ?   ?   ??? __init__.py
    ?           ?   ?   ?   ?   ??? metadata.py
    ?           ?   ?   ?   ?   ??? metadata_legacy.py
    ?           ?   ?   ?   ?   ??? wheel.py
    ?           ?   ?   ?   ?   ??? wheel_legacy.py
    ?           ?   ?   ?   ??? check.py
    ?           ?   ?   ?   ??? freeze.py
    ?           ?   ?   ?   ??? install
    ?           ?   ?   ?   ?   ??? __init__.py
    ?           ?   ?   ?   ?   ??? editable_legacy.py
    ?           ?   ?   ?   ?   ??? legacy.py
    ?           ?   ?   ?   ?   ??? wheel.py
    ?           ?   ?   ?   ??? prepare.py
    ?           ?   ?   ??? pep425tags.py
    ?           ?   ?   ??? pyproject.py
    ?           ?   ?   ??? req
    ?           ?   ?   ?   ??? __init__.py
    ?           ?   ?   ?   ??? constructors.py
    ?           ?   ?   ?   ??? req_file.py
    ?           ?   ?   ?   ??? req_install.py
    ?           ?   ?   ?   ??? req_set.py
    ?           ?   ?   ?   ??? req_tracker.py
    ?           ?   ?   ?   ??? req_uninstall.py
    ?           ?   ?   ??? self_outdated_check.py
    ?           ?   ?   ??? utils
    ?           ?   ?   ?   ??? __init__.py
    ?           ?   ?   ?   ??? appdirs.py
    ?           ?   ?   ?   ??? compat.py
    ?           ?   ?   ?   ??? deprecation.py
    ?           ?   ?   ?   ??? distutils_args.py
    ?           ?   ?   ?   ??? encoding.py
    ?           ?   ?   ?   ??? entrypoints.py
    ?           ?   ?   ?   ??? filesystem.py
    ?           ?   ?   ?   ??? filetypes.py
    ?           ?   ?   ?   ??? glibc.py
    ?           ?   ?   ?   ??? hashes.py
    ?           ?   ?   ?   ??? inject_securetransport.py
    ?           ?   ?   ?   ??? logging.py
    ?           ?   ?   ?   ??? marker_files.py
    ?           ?   ?   ?   ??? misc.py
    ?           ?   ?   ?   ??? models.py
    ?           ?   ?   ?   ??? packaging.py
    ?           ?   ?   ?   ??? pkg_resources.py
    ?           ?   ?   ?   ??? setuptools_build.py
    ?           ?   ?   ?   ??? subprocess.py
    ?           ?   ?   ?   ??? temp_dir.py
    ?           ?   ?   ?   ??? typing.py
    ?           ?   ?   ?   ??? ui.py
    ?           ?   ?   ?   ??? unpacking.py
    ?           ?   ?   ?   ??? urls.py
    ?           ?   ?   ?   ??? virtualenv.py
    ?           ?   ?   ?   ??? wheel.py
    ?           ?   ?   ??? vcs
    ?           ?   ?   ?   ??? __init__.py
    ?           ?   ?   ?   ??? bazaar.py
    ?           ?   ?   ?   ??? git.py
    ?           ?   ?   ?   ??? mercurial.py
    ?           ?   ?   ?   ??? subversion.py
    ?           ?   ?   ?   ??? versioncontrol.py
    ?           ?   ?   ??? wheel_builder.py
    ?           ?   ??? _vendor
    ?           ?       ??? __init__.py
    ?           ?       ??? appdirs.py
    ?           ?       ??? cachecontrol
    ?           ?       ?   ??? __init__.py
    ?           ?       ?   ??? _cmd.py
    ?           ?       ?   ??? adapter.py
    ?           ?       ?   ??? cache.py
    ?           ?       ?   ??? caches
    ?           ?       ?   ?   ??? __init__.py
    ?           ?       ?   ?   ??? file_cache.py
    ?           ?       ?   ?   ??? redis_cache.py
    ?           ?       ?   ??? compat.py
    ?           ?       ?   ??? controller.py
    ?           ?       ?   ??? filewrapper.py
    ?           ?       ?   ??? heuristics.py
    ?           ?       ?   ??? serialize.py
    ?           ?       ?   ??? wrapper.py
    ?           ?       ??? certifi
    ?           ?       ?   ??? __init__.py
    ?           ?       ?   ??? __main__.py
    ?           ?       ?   ??? cacert.pem
    ?           ?       ?   ??? core.py
    ?           ?       ??? chardet
    ?           ?       ?   ??? __init__.py
    ?           ?       ?   ??? big5freq.py
    ?    
    ?           ?   ??? ssl_support.py
    ?           ?   ??? unicode_utils.py
    ?           ?   ??? version.py
    ?           ?   ??? wheel.py
    ?           ?   ??? windows_support.py
    ?           ??? setuptools-46.1.3.dist-info
    ?           ?   ??? INSTALLER
    ?           ?   ??? LICENSE
    ?           ?   ??? METADATA
    ?           ?   ??? RECORD
    ?           ?   ??? WHEEL
    ?           ?   ??? dependency_links.txt
    ?           ?   ??? entry_points.txt
    ?           ?   ??? top_level.txt
    ?           ?   ??? zip-safe
    ?           ??? setuptools-46.1.3.virtualenv
    ?           ??? wheel
    ?           ?   ??? __init__.py
    ?           ?   ??? __main__.py
    ?           ?   ??? _version.py
    ?           ?   ??? bdist_wheel.py
    ?           ?   ??? cli
    ?           ?   ?   ??? __init__.py
    ?           ?   ?   ??? convert.py
    ?           ?   ?   ??? install.py
    ?           ?   ?   ??? pack.py
    ?           ?   ?   ??? unpack.py
    ?           ?   ??? macosx_libfile.py
    ?           ?   ??? metadata.py
    ?           ?   ??? pep425tags.py
    ?           ?   ??? pkginfo.py
    ?           ?   ??? util.py
    ?           ?   ??? wheelfile.py
    ?           ??? wheel-0.34.2.dist-info
    ?           ?   ??? INSTALLER
    ?           ?   ??? LICENSE.txt
    ?           ?   ??? METADATA
    ?           ?   ??? RECORD
    ?           ?   ??? WHEEL
    ?           ?   ??? entry_points.txt
    ?           ?   ??? top_level.txt
    ?           ??? wheel-0.34.2.virtualenv
    ??? pyvenv.cfg

Run Code Online (Sandbox Code Playgroud)

--> 说实话,我没想到方向树这么复杂;由于stackoverflow的字符限制,我什至删除了一些文件。

>>>编辑 - 找到解决方案<<<

--> 错误是由于解释器不匹配造成的。我唯一需要改变的是:

而不是在终端输入

$ *cd TODIRECTORY*
$ virtualenv venv
$ . venv/bin/activate
$ (...) 
Run Code Online (Sandbox Code Playgroud)

我必须打字

$ *cd TODIRECTORY*
$ python3 -m venv env
$ . venv/bin/activate
$ (...) 
Run Code Online (Sandbox Code Playgroud)

--> 希望对某人有帮助!

感谢社区帮助解决这个问题!

Low*_*put 2

您的解释器版本似乎不匹配。

删除您的环境,然后在您的项目文件夹中尝试:

$ python3 -m venv env
$ . env/bin/activate
$ pip install py2app
Run Code Online (Sandbox Code Playgroud)

然后尝试https://py2app.readthedocs.io/en/latest/tutorial.html#create-a-setup-py-file