【问题标题】:APScheduler import error when generate executable with py2exe or cx_freeze使用 py2exe 或 cx_freeze 生成可执行文件时,APScheduler 导入错误
【发布时间】:2017-03-27 18:00:48
【问题描述】:

在 python 2.7 上使用 py2exe (0.6.9) 或 cx_freeze (5.0.1) 和 APScheduler (3.3.1) 生成可执行文件时,出现以下错误:

  File "apscheduler\__init__.pyc", line 2, in <module>
  File "pkg_resources\__init__.pyc", line 552, in get_distribution
  File "pkg_resources\__init__.pyc", line 426, in get_provider
  File "pkg_resources\__init__.pyc", line 968, in require
  File "pkg_resources\__init__.pyc", line 854, in resolve
pkg_resources.DistributionNotFound: The 'APScheduler' distribution was not found and is required by the application

这是我的 cx_freeze setup.py 文件:

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"includes": ["requests", "apscheduler"], "include_files": ["XXX"]}

# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(  name = "XXX",
        version = "XXX",
        description = "XXX",
        options = {"build_exe": build_exe_options},
        executables = [Executable("XXX.py", base=base), Executable("XXX2.py", base=base)])

这是我的 py2exe setup.py 文件:

from distutils.core import setup
import py2exe

data_files = ['XXX']

setup(
    data_files=data_files,
    windows=[
        {'script': 'XXX.py'},
        {'script': 'XXX2.py'},
    ],
    options={'py2exe':{
                        'includes': ['requests', 'apscheduler'],
                        'bundle_files': 1,
                      }
    },
)

我已经尝试使用“packages”选项,但没有成功。

如果我从 APScheduler __init__.py (apscheduler/__init__.py) 中删除代码,它会起作用。

下面是 APScheduler 包中的 __init__.py:

# These will be removed in APScheduler 4.0.
release = __import__('pkg_resources').get_distribution('apscheduler').version.split('-')[0]
version_info = tuple(int(x) if x.isdigit() else x for x in release.split('.'))
version = __version__ = '.'.join(str(x) for x in version_info[:3])

我是否需要以某种方式将依赖项包含到 py2exe/cx_freeze 库包中?

我已经在互联网上做了一些研究,但没有成功。

【问题讨论】:

    标签: python py2exe cx-freeze apscheduler


    【解决方案1】:

    找到了解决办法。问题是 py2exe 不包含 library.zip 的 dist-info 目录。每个模块在 site-packages python 库中都有自己的 dist-info 目录。

    pkg_resources 库使用这些目录来导入模块 正如我们在 apscheduler 上看到的 __init__.py:2

    您所要做的就是将那些 dist-info 目录添加到由 py2exe 生成的 library.zip 文件中。

    以下是 google 将 zoneinfo 目录(不相关,但可以用作示例)导入 library.zip 的示例。

    https://github.com/google/transitfeed/blob/master/setup.py#L96

    要获取模块的 dist-info 路径,请使用 follow:

    import os
    import pkg_resources
    dist_info_dir = pkg_resources.get_distribution('desired_module')._provider.egg_info
    
    # get base name of directory
    base_name = os.path.basename(dist_info_dir)
    

    注意:如果没有 dist-info 目录和 egg-info 代替。您可能应该搜索该库的 wheel 包或自己构建:

    python setup.py bdist_wheel
    

    干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-14
      • 2010-09-24
      • 2010-09-11
      相关资源
      最近更新 更多