繁体   English   中英

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

[英]APScheduler import error when generate executable with py2exe or cx_freeze

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

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

找到了解决方案。 问题是py2exe不包括library.zip的dist-info目录。 每个模块在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

干杯!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM