使用 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_freezesetup.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 没有将 dist-info 目录包含到 library.zip。每个模块在站点包 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 路径,请使用以下命令:
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
干杯!
当使用 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_freezesetup.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 没有将 dist-info 目录包含到 library.zip。每个模块在站点包 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 路径,请使用以下命令:
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
干杯!