gpt4 book ai didi

python - Py2Exe 的单个可执行文件

转载 作者:太空狗 更新时间:2023-10-29 18:08:44 24 4
gpt4 key购买 nike

我一直在尝试制作一个单一的可执行文件,而且我越来越接近了。请不要推荐我使用 PyInstaller——我已经尝试过这条路线,在 SO here 上询问过, 并已投入门票。它很接近但不是很有效。我现在正在尝试 py2exe 并且也非常接近。在 pyinstaller 中,我能够创建资源文件(它使用包含的文件构建可执行文件——然后我可以在临时文件夹中访问这些文件)。

我想对 py2exe 做同样的事情。我有一个可执行文件,但有五个额外的文件夹(maps、mpl-data、data、pics 和 tcl)。我看过这个question但似乎无法理解它,也无法让它发挥作用。在我的主要 py 文件中,我正在使用 PersistentDict(filepath),这是我需要文件路径的地方。

我的问题分为两个部分: 1. 如何将文件(下面的数据文件)打包到可执行文件中。 2. 如何在我的代码中访问这些文件并返回它们的路径(作为字符串),例如/temp/file1.jpg。

这是我的 py2exe 安装文件的代码——请注意,我有 matplotlib,并且必须在我的可执行文件中正确包含 mpl-data。谢谢!

from distutils.core import setup 
import py2exe
import shutil
import glob
import matplotlib,six

opts = {'py2exe': { "includes" : ["matplotlib.backends",
"matplotlib.backends.backend_qt4agg",
"matplotlib.figure","numpy",
"six",
"mpl_toolkits.basemap",
"matplotlib.backends.backend_tkagg"],
'excludes': ['_gtkagg', '_tkagg','_agg2','_cairo',
'_cocoaagg', '_fltkagg', '_gtk', '_gtkcairo', 'tcl' ],
'dll_excludes': ['libgdk-win32-2.0-0.dll','w9xpopen.exe',
'libgobject-2.0-0.dll'],
'bundle_files': 1,
'dist_dir': "Dist Folder",
'compressed': True,
}
}

data_files = [(r'mpl-data', glob.glob(r'C:\Python27\Lib\site-packages\matplotlib\mpl-data\*.*')),
(r'mpl-data', [r'C:\Python27\Lib\site-packages\matplotlib\mpl-data\matplotlibrc']),
(r'mpl-data\images',glob.glob(r'C:\Python27\Lib\site-packages\matplotlib\mpl-data\images\*.*')),
(r'mpl-data\fonts',glob.glob(r'C:\Python27\Lib\site-packages\matplotlib\mpl-data\fonts\*.*')),
(r'mpl-data\data', glob.glob(r'C:\Python27\Lib\site-packages\matplotlib\mpl-data\data\*.*')),
('data', ['C:\\Users\\Me\\Documents\\Example_Json_File.json']),
('pics', ['C:\\Users\\Me\\Documents\\Example_Icon.ico',
'C:\\Users\\Me\\Documents\\Example_Jpg.jpg',
])]


setup(windows=[{"script" : "MyMainScript.py",
"data_files" : data_files,
"icon_resources": [(1, 'C:\\Users\\Me\\Documents\\Example_Icon.ico')]}, ],

version = "1.0",
options=opts,
data_files=data_files,
zipfile = None,
)

最佳答案

盖伊 here解释了如何使用 py2exe 打包到一个文件。他的设置也没有将资源打包到可执行文件中。

当我打包我的应用程序时,我没有使用一个可执行选项

options = {"py2exe": {'bundle_files': 1, 'compressed': True}},

甚至懒得将它们放入 library.zip 中

options = {"py2exe": {"skip_archive":0}}

只需在一个目录中放置多个 pyc、数据文件、dll 等。然后使用 NSIS 或 Inno setup 创建安装程序。由于我的一些应用程序必须作为服务运行,Inno 负责这件事。这种方法的最大优点是,您不必处理与原始路径不同的“卡住”文件路径。

否则您可能需要更改代码以检测卡住路径,例如http://www.py2exe.org/index.cgi/WhereAmI

关于python - Py2Exe 的单个可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19865649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com