Python 脚本依赖于另一个 .exe。是否可以将两者合并为一个 .exe?
Python script is dependent of another .exe. Is it possible to merge both into one single .exe?
我正在写一个简短的 script.py
,它将更新 archive.tar
。
tar 存档的内容是一个文件:file.txt (Content: "Hello")
.
我正在使用 7za.exe
,这是 7zip 的命令行版本,用另一个 file.txt (Content: "Hello world")
更新 file.txt
。我正在使用以下命令:
os.system("7za u archive.tar file.txt")
到目前为止一切正常,但是我想使用 PyInstaller 从 Python 脚本创建一个可执行文件,因为并不是每个使用此脚本的人都会安装 python。
我的问题:有没有办法将 script.py
和 7za.exe
合并到一个 .exe 文件中?
到目前为止,我还没有找到答案。
为什么不简单地使用 ZipFile 库?
这可以很简单:
with ZipFile('spam.zip', 'w') as myzip:
myzip.write('eggs.txt')
如果您真的想使用第三方,那么不,没有简单的方法可以做您想做的事。唯一的 "clean" 方法是使用安装程序将您的可执行文件打包在一起,就像您可以使用 Innosetup
创建的那样
NB: You should prefer the use of subprocess rather that os.system
, see there https://docs.python.org/fr/3/library/subprocess.html#replacing-os-system
更新:对于 tarfile,也许这些可以帮助您:
How can files be added to a tarfile with Python, without adding the directory hierarchy?
也许你需要解压缩/添加/重新压缩,但你应该能够在内存中完成。
我正在写一个简短的 script.py
,它将更新 archive.tar
。
tar 存档的内容是一个文件:file.txt (Content: "Hello")
.
我正在使用 7za.exe
,这是 7zip 的命令行版本,用另一个 file.txt (Content: "Hello world")
更新 file.txt
。我正在使用以下命令:
os.system("7za u archive.tar file.txt")
到目前为止一切正常,但是我想使用 PyInstaller 从 Python 脚本创建一个可执行文件,因为并不是每个使用此脚本的人都会安装 python。
我的问题:有没有办法将 script.py
和 7za.exe
合并到一个 .exe 文件中?
到目前为止,我还没有找到答案。
为什么不简单地使用 ZipFile 库?
这可以很简单:
with ZipFile('spam.zip', 'w') as myzip:
myzip.write('eggs.txt')
如果您真的想使用第三方,那么不,没有简单的方法可以做您想做的事。唯一的 "clean" 方法是使用安装程序将您的可执行文件打包在一起,就像您可以使用 Innosetup
创建的那样NB: You should prefer the use of subprocess rather that
os.system
, see there https://docs.python.org/fr/3/library/subprocess.html#replacing-os-system
更新:对于 tarfile,也许这些可以帮助您:
How can files be added to a tarfile with Python, without adding the directory hierarchy?
也许你需要解压缩/添加/重新压缩,但你应该能够在内存中完成。