-->

Python: Making a standalone executable file on Mac

2019-05-09 08:03发布

问题:

I have an application in a python script my_app.py and want to make a standalone executable out of it on a MacOS (10.14). Following the video-tutorial here, I entered sequentially the following commmands:

pip install virtualenv
virtualenv venv --system-site-packages
source venv/bin/activate
pip install py2app
cd /path/to/my_app.py
python setup.py py2app -A

with the following setup.py file:

from setuptools import setup

APP = ["my_app.py"]
DATA_FILES = []
OPTIONS = {
    "argv_emulation": True,
    "packages": ["certifi"],
}
setup(
    app = APP,
    data_files = DATA_FILES,
    options = {"py2app": OPTIONS},
    setup_requires = ["py2app"]

)

but got the following error message:

Traceback (most recent call last):
  File "setup.py", line 13, in <module>
    setup_requires = ["py2app"]
  File "/Users/mymac/venv/lib/python3.7/site-packages/setuptools/__init__.py", line 145, in setup
    return distutils.core.setup(**attrs)
  File "/Users/mymac/anaconda3/lib/python3.7/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/Users/mymac/anaconda3/lib/python3.7/distutils/dist.py", line 966, in run_commands
    self.run_command(cmd)
  File "/Users/mymac/anaconda3/lib/python3.7/distutils/dist.py", line 984, in run_command
    cmd_obj.ensure_finalized()
  File "/Users/mymac/anaconda3/lib/python3.7/distutils/cmd.py", line 107, in ensure_finalized
    self.finalize_options()
  File "/Users/mymac/venv/lib/python3.7/site-packages/py2app/build_app.py", line 567, in finalize_options
    if isinstance(self.plist, plistlib.Dict):
AttributeError: module 'plistlib' has no attribute 'Dict'

With that error message, I found that github page and typed the following code:

brew install qt  # will install qt-5.x.x
brew install libxml2
make qt5py3
python3 labelImg.py
python3 labelImg.py [IMAGE_PATH] [PRE-DEFINED CLASS FILE]

As a side note, if mssing pyrcc5 or lxml, try
pip3 install pyqt5 lxml

However after entering the line make qt5py3, I get:

make: *** No rule to make target `qt5py3'.  Stop.

I also tried the following commands:

brew install python3
pip install pipenv
pipenv --three
pipenv shell
pip install py2app
pip install PyQt5 lxml
make qt5py3
rm -rf build dist
python setup.py py2app -A
mv "dist/labelImg.app" /Applications

But also got

make: *** No rule to make target `qt5py3'.  Stop.

after entering the line make qt5py3

I posted an issue on github

What am I doing wrong?

回答1:

plistlib.Dict was available but not documented. In python 3.4 it was finally documented; also documented that it's deprecated since 3.0.

In python 3.7 it is no longer documented and perhaps removed from plistlib library.

This means that py2app currently not yet compatible with Python 3.7. I found this issue, it was reported half a year ago and resolved a few days later. The resolution is: upgrade py2app to the latest version 0.15 with the following command :

pip3.7 install -U py2app

Verify version with

pip show py2app