Python和py2exe - 隐含地导入模块 [英] Python and py2exe - Implicitely Importing Modules
问题描述
我过去曾多次使用py2exe为我的python程序创建* .exe文件。但是,这次我收到了一个错误。我想我知道问题是什么,但我不知道如何解决它。
I've used py2exe several times in the past to create *.exe files for my python programs. However, I'm getting an error this time. I think I know what the issue is, but I don't know how to resolve it.
我在子文件夹中有一些wx.Panels它可能是一个变量,所以我通过一个函数导入它们,找到文件夹中的* .py文件并调用下面的函数实际导入每个面板。
I have a handful of wx.Panels in a subfolder and it could be a variable amount, so I import them via a function that finds the *.py files in the folder and calls the function below to actually import each panel.
在普通的python中,这很好用。但是,py2exe会将这些文件保留。我认为因为它们没有明确导入,所以py2exe不相信它们是必需的。有针对这个的解决方法吗?我不知道py2exe中的一些选项?
In normal python, this works well. However, py2exe leaves these files out. I assume that because they are not explicitly imported, py2exe doesn't believe they are needed. Is there a solution to this? Some option in py2exe that I'm unaware of?
谢谢!
# module = Module to be imported (string)
# folder = Folder containing the module (string)
def import_module(module, folder=None):
if folder is None:
return __import__(module)
return getattr(__import__('%s.%s' % (folder.replace(os.path.sep, '.'),
module)), module)
...within some other function...
modules = [import_module(os.path.basename(os.path.splitext(filename)[0]), 'Panels') for filename in glob.glob('Panels//*.py')]
编辑
我正在添加一个我用过的示例setup.py脚本。但我已经使用了大约20种不同的变体和几种完全不同的脚本(我可以在互联网上找到)。请注意,一个要求是它在一个可执行文件中是完全自包含的。
I'm adding a sample setup.py script I've used. But I've used probably 20 different variations and several completely different scripts (what I could find on the internet). Note that one requirement is that it is completely self-contained in one executable file.
from distutils.core import setup
import py2exe
import wxversion
wxversion.select("2.8.12.1")
import wx
import wx.lib.pubsub
includes = []
excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
'Tkconstants', 'Tkinter']
packages = ['wx.lib.pubsub']
dll_excludes = ['libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll',
'tk84.dll']
import glob
my_data_files = [('Panels', glob.glob('Panels/*.py'))]
setup(
options = {"py2exe": {"compressed": 2,
"optimize": 2,
"includes": includes,
"excludes": excludes,
"packages": packages,
"dll_excludes": dll_excludes,
"bundle_files": 2,
"dist_dir": "dist",
"xref": False,
"skip_archive": False,
"ascii": False,
"custom_boot_script": '',
}
},
zipfile = None,
#data_files = my_data_files,
windows=['Main.py']
)
推荐答案
我相信我找到了解决问题的方法。在我的setup.py文件中,我将'includes = []'行替换为:
I believe I've found the solution for my issue. In my setup.py file, I replaced the 'includes = []' line with:
includes = ['Panels.%s' % os.path.basename(os.path.splitext(filename)[0]) for
filename in glob.glob('Panels//*.py')]
在我使用'import_module'函数的代码中,它曾使用该glob来导入Panels目录中的模块。相反,我已经硬编码了要包含的模块列表。
In my code where I use the 'import_module' function, it used to use that glob to import the modules within the Panels directory. Instead, I've hard-coded a list of modules to include.
这不是我想要的确切解决方案(我不想硬编码面板列表),但似乎确实有效。除非我发现更好的东西,否则我将继续使用。
This isn't the exact solution I wanted (I don't want to hard-code that list of panels), but it does seem to work. Unless I discover anything better, this is what I'll continue to use.
这篇关于Python和py2exe - 隐含地导入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!