cx_Freeze不包含子目录中的.py文件
我正在用Python 3.3、Pyside和lxml等工具构建一个程序。cx_Freeze本来运行得很好,但当我开始把模块整理到子文件夹里以便更整洁时,问题就来了。cx_Freeze虽然能工作,但却报告说找不到模块,生成的.exe文件无法加载,不过用.py文件打开程序是没问题的。
我的文件夹结构很简单,只有两个子文件夹,分别叫做parsers和constants。
cx_Freeze的文档里提到可以添加一个路径变量,但这个方法并不能找到模块。任何帮助都非常感谢。
import sys
from cx_Freeze import setup,Executable
includefiles = ['open.png', 'appicon.png']
includes = []
excludes = ['Tkinter']
packages = ['lxml._elementpath','lxml.etree']
build_exe_options = {'excludes':excludes,'packages':packages,'include_files':includefiles}
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(
name = 'Config-Sheets',
version = '0.1',
description = 'Convert system configuration into .XLSX files',
author = 'ZZZ',
author_email = 'XXX@YYY.com',
options = {'build_exe': build_exe_options },
executables = [Executable('main.py', base=base)]
)
结果是生成了一个无法运行的exe文件。
Missing modules:
...
? parsers.parsercelerra imported from main__main__
? parsers.parserVPLEX imported from main__main__
? parsers.parserrain imported from main__main__
2 个回答
-2
你需要在 setup.py
脚本中包含你的 .py
文件。如果这个文件不是Python自带的,cx_freeze就需要知道你想把这些文件包含进来。你应该把额外的 .py
文件和它们的路径添加到 includefiles
列表中。例如:
import sys
from cx_Freeze import setup,Executable
includefiles = ['open.png', 'appicon.png', 'parsers\xtra_file1.py','constants\xtra_file2.py']
includes = []
excludes = ['Tkinter']
packages = ['lxml._elementpath','lxml.etree']
build_exe_options = {'excludes':excludes,'packages':packages,'include_files':includefiles}
...
希望这对你有帮助。
2
你应该可以很简单地包含你的包:
packages = ['lxml._elementpath','lxml.etree', 'parsers', 'constants']
当然,这些目录里必须有一个叫 __init__.py
的文件,才能被认为是包。
如果这样做还是没有达到你想要的效果,那么查看一下在 main.py
中是如何引入这些文件的就很有帮助。如果使用的方式不是简单的 import
(或者 from x import
),那么这可能会导致 cx_Freeze 找不到它。