Python py2exe指定子文件夹
我想使用py2exe将.py脚本编译成exe 文件结构:Python py2exe指定子文件夹,python,py2exe,Python,Py2exe,我想使用py2exe将.py脚本编译成exe 文件结构: ProjectDir - src - FullGui.py - other modules - setup.py - tools - Gui2exe - docs 为了满足我的典型工作流程,我想移动setup.py(例如,移动到tools,因为创建.exe是一个独立的过程,而不是部署的软件的一部分)。 我使用Gui2exe创建了setup.py。当我在我的src文件夹中使用它时,它工作得完美无
ProjectDir
- src
- FullGui.py
- other modules
- setup.py
- tools
- Gui2exe
- docs
为了满足我的典型工作流程,我想移动setup.py(例如,移动到tools
,因为创建.exe是一个独立的过程,而不是部署的软件的一部分)。
我使用Gui2exe创建了setup.py。当我在我的src
文件夹中使用它时,它工作得完美无缺
# ======================================================== #
# File automagically generated by GUI2Exe version 0.5.3
# Copyright: (c) 2007-2012 Andrea Gavana
# ======================================================== #
# Let's start with some default (for me) imports...
from distutils.core import setup
from py2exe.build_exe import py2exe
import glob
import os
import zlib
import shutil
# Remove the build folder
shutil.rmtree("build", ignore_errors=True)
class Target(object):
""" A simple class that holds information on our executable file. """
def __init__(self, **kw):
""" Default class constructor. Update as you need. """
self.__dict__.update(kw)
# Ok, let's explain why I am doing that.
# Often, data_files, excludes and dll_excludes (but also resources)
# can be very long list of things, and this will clutter too much
# the setup call at the end of this file. So, I put all the big lists
# here and I wrap them using the textwrap module.
data_files = []
includes = []
excludes = ['Tkconstants', 'Tkconstants', 'Tkinter', 'Tkinter', '_gtkagg',
'_gtkagg', '_tkagg', '_tkagg', 'bsddb', 'bsddb',
'curses', 'curses', 'email', 'email', 'pywin.debugger',
'pywin.debugger', 'pywin.debugger.dbgcon', 'pywin.debugger.dbgcon',
'pywin.dialogs', 'pywin.dialogs', 'tcl', 'tcl']
packages = []
dll_excludes = ['libgdk-win32-2.0-0.dll', 'libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll',
'libgobject-2.0-0.dll', 'msvcp90.dll', 'msvcp90.dll',
'tcl84.dll', 'tcl84.dll', 'tk84.dll', 'tk84.dll']
icon_resources = []
bitmap_resources = []
other_resources = []
# This is a place where the user custom code may go. You can do almost
# whatever you want, even modify the data_files, includes and friends
# here as long as they have the same variable name that the setup call
# below is expecting.
import matplotlib
data_files.extend(matplotlib.get_py2exe_datafiles())
# Ok, now we are going to build our target class.
# I chose this building strategy as it works perfectly for me :-D
GUI2Exe_Target_1 = Target(
# what to build
script = "FullGui.py",
icon_resources = icon_resources,
bitmap_resources = bitmap_resources,
other_resources = other_resources,
dest_base = "FullGui",
version = "1.0",
company_name = "Bytec Medizintechnik GmbH",
copyright = "All Rights Reserved",
name = "void",
)
# That's serious now: we have all (or almost all) the options py2exe
# supports. I put them all even if some of them are usually defaulted
# and not used. Some of them I didn't even know about.
setup(
# No UPX or Inno Setup
data_files = data_files,
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,
console = [],
windows = [GUI2Exe_Target_1],
service = [],
com_server = [],
ctypes_com_server = []
)
# This is a place where any post-compile code may go.
# You can add as much code as you want, which can be used, for example,
# to clean up your folders or to do some particular post-compilation
# actions.
# No post-compilation code added
# And we are done. That's a setup script :-D
第一步是将它向上移动一级到ProjectDir
。我将script=“FullGui.py”
更改为script=“src\FullGui.py”
,但它没有按预期工作。它可以将.py编译为.exe,但会中止启动,并在exe的日志文件中出现以下错误:
Traceback (most recent call last):
File "FullGui.py", line 7, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "receiver.pyo", line 9, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "serial\__init__.pyo", line 18, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "serial\serialwin32.pyo", line 9, in <module>
File "zipextimporter.pyo", line 98, in load_module
ImportError: MemoryLoadLibrary failed loading win32file.pyd
回溯(最近一次呼叫最后一次):
文件“FullGui.py”,第7行,在
加载模块中的第82行文件“zipextimporter.pyo”
文件“receiver.pyo”,第9行,在
加载模块中的第82行文件“zipextimporter.pyo”
文件“serial\\ uuuu init\ uuuu.pyo”,第18行,在
加载模块中的第82行文件“zipextimporter.pyo”
文件“serial\serialwin32.pyo”,第9行,在
加载模块中第98行的文件“zipextimporter.pyo”
ImportError:MemoryLoadLibrary无法加载win32file.pyd
我试图通过sys.path.append('src')
将src
添加到路径中,并通过添加不同的路径将其搞乱,但我无法让它找到它需要的内容
任何人的提示或解决方案?TL;DR
在setup
函数中使用选项package_dir={'':'../src'}
,您可以使用选项packages
将自定义模块包含在项目中
来源 从 Py2exe扩展Distutils设置关键字 从中可以找到这个选项 包目录包到目录名的映射[字典] 详细举例说明