使用py2exe或cx_freeze生成可执行文件时APScheduler导入错误

时间:2017-03-27 18:00:49

标签: python py2exe cx-freeze apscheduler

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

  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库包中?

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

1 个答案:

答案 0 :(得分:3)

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

pkg_resources库使用这些目录导入模块 我们可以在apscheduler上看到__init __。py:2

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

以下是谷歌将(无关,但可以作为示例)zoneinfo目录导入library.zip的示例。

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

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

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

干杯!