在 macOS 上使用 py2app 导入 pandas 时出现问题

Problem importing pandas with py2app on macos

当我使用带有选项 -An 的 py2app 时,一切正常。 但是没有 pandas 导入时我有错误。 我尝试一个示例代码:

import sys
from PyQt5.QtWidgets import QApplication, QWidget
import pandas as pd

def main():
    df = pd.read_csv("data.csv")
    app = QApplication(sys.argv)
    file = "theme.stylesheet"
    w = QWidget()
    w.resize(250, 150)
    w.move(300, 300)
    w.setWindowTitle('Simple')
    with open(file,'r') as fh:
        w.setStyleSheet(fh.read())
    w.show()

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

然后python setup.py py2app

与setup.py:

from setuptools import setup

APP = ['main.py']
OPTIONS = {'argv_emulation': True}
DATA_FILES = ['theme.stylesheet', 'data.csv']
setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app':OPTIONS},
    setup_requires=['py2app']
)

但是我有这个错误:

Traceback (most recent call last):
  File "<frozen zipimport>", line 259, in load_module
  File "pandas/_libs/tslibs/conversion.pyc", line 14, in <module>
  File "pandas/_libs/tslibs/conversion.pyc", line 10, in __load
  File "imp.pyc", line 342, in load_dynamic
  File "<frozen importlib._bootstrap>", line 711, in _load
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 1181, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "pandas/_libs/tslibs/conversion.pyx", line 1, in init pandas._libs.tslibs.conversion
ModuleNotFoundError: No module named 'pandas._libs.tslibs.base'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen zipimport>", line 259, in load_module
  File "pandas/_libs/missing.pyc", line 14, in <module>
  File "pandas/_libs/missing.pyc", line 10, in __load
  File "imp.pyc", line 342, in load_dynamic
  File "<frozen importlib._bootstrap>", line 711, in _load
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 1181, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "pandas/_libs/missing.pyx", line 1, in init pandas._libs.missing
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 627, in _load_backward_compatible
  File "<frozen zipimport>", line 259, in load_module
  File "pandas/_libs/tslibs/__init__.pyc", line 31, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 627, in _load_backward_compatible
  File "<frozen zipimport>", line 261, in load_module
KeyError: 'pandas._libs.tslibs.conversion'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen zipimport>", line 259, in load_module
  File "pandas/_libs/hashtable.pyc", line 14, in <module>
  File "pandas/_libs/hashtable.pyc", line 10, in __load
  File "imp.pyc", line 342, in load_dynamic
  File "<frozen importlib._bootstrap>", line 711, in _load
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 1181, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "pandas/_libs/hashtable.pyx", line 1, in init pandas._libs.hashtable
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 627, in _load_backward_compatible
  File "<frozen zipimport>", line 261, in load_module
KeyError: 'pandas._libs.missing'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen zipimport>", line 259, in load_module
  File "pandas/_libs/interval.pyc", line 14, in <module>
  File "pandas/_libs/interval.pyc", line 10, in __load
  File "imp.pyc", line 342, in load_dynamic
  File "<frozen importlib._bootstrap>", line 711, in _load
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 1181, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "pandas/_libs/interval.pyx", line 1, in init pandas._libs.interval
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 627, in _load_backward_compatible
  File "<frozen zipimport>", line 261, in load_module
KeyError: 'pandas._libs.hashtable'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jaysenoner/PycharmProjects/final/dist/main.app/Contents/Resources/__boot__.py", line 411, in <module>
    _run()
  File "/Users/jaysenoner/PycharmProjects/final/dist/main.app/Contents/Resources/__boot__.py", line 394, in _run
    exec(compile(source, path, "exec"), globals(), globals())
  File "/Users/jaysenoner/PycharmProjects/final/dist/main.app/Contents/Resources/main.py", line 6, in <module>
    import pandas as pd
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 627, in _load_backward_compatible
  File "<frozen zipimport>", line 259, in load_module
  File "pandas/__init__.pyc", line 22, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 627, in _load_backward_compatible
  File "<frozen zipimport>", line 259, in load_module
  File "pandas/compat/__init__.pyc", line 15, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 627, in _load_backward_compatible
  File "<frozen zipimport>", line 259, in load_module
  File "pandas/compat/numpy/__init__.pyc", line 7, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 627, in _load_backward_compatible
  File "<frozen zipimport>", line 259, in load_module
  File "pandas/util/__init__.pyc", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 627, in _load_backward_compatible
  File "<frozen zipimport>", line 259, in load_module
  File "pandas/util/_decorators.pyc", line 14, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 627, in _load_backward_compatible
  File "<frozen zipimport>", line 259, in load_module
  File "pandas/_libs/__init__.pyc", line 13, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 627, in _load_backward_compatible
  File "<frozen zipimport>", line 261, in load_module

我尝试使用 pyinstaller 但是当我打开应用程序时立即关闭。

如果我通过 Windows 使用它,那工作正常,但在我需要的 macos 中没有。

我也尝试将 pandas 放入 setup.py 的包中,但仍然无法正常工作。

我通过 3.9 将 python 降级到 3.6 解决了这个问题。