Sry,我的英语技能非常低。
我的问题:
我想用" os.system"开始一个EXE。功能。 它有效......
os.system("Engine\\2.75\\python\\python.exe TEST_GUI.py")
所以...现在。此代码启动Python EXE并启动我的TEST GUI。 但该做什么......当" TEST GUI.py"在ZIP文件中?
我想用" TESTGUI.py"启动Python.exe。 我之前不想解压缩zip文件。
答案 0 :(得分:0)
这可以通过包含
来完成__main__.py
zip存档中的文件,作为程序的入口点。例如,假设creatfile.py包含:
def writefile(f):
fout = open(f, 'wt')
fout.write('hello world')
fout.close()
和
__main__.py
包含:
from createfile import *
writefile('test1.txt')
然后,将它们放入writefile.zip后,运行
os.system('python writefile.zip')
导致在当前工作目录中使用' hello world'创建test1.txt。写给它。
在IPython中显示:
%ls
Directory of C:\Users\tn\Documents\python\tmp
09/22/2015 11:02 PM <DIR> .
09/22/2015 11:02 PM <DIR> ..
09/22/2015 10:51 PM 52 __main__.py
09/22/2015 10:50 PM 98 createfile.py
09/22/2015 10:51 PM 422 writefile.zip
3 File(s) 572 bytes
2 Dir(s) 304,887,132,160 bytes free
os.system('python writefile.zip')
Out[29]: 0
%ls
Directory of C:\Users\tn\Documents\python\tmp
09/22/2015 11:02 PM <DIR> .
09/22/2015 11:02 PM <DIR> ..
09/22/2015 10:51 PM 52 __main__.py
09/22/2015 10:50 PM 98 createfile.py
09/22/2015 11:02 PM 11 test1.txt
09/22/2015 10:51 PM 422 writefile.zip
4 File(s) 583 bytes
2 Dir(s) 304,887,132,160 bytes free
!type test1.txt # 'type' is the Windows equivilant of the Linux cat command
hello world
!type createfile.py
def writefile(f):
fout = open(f, 'wt')
fout.write('hello world')
fout.close()
!type __main__.py
from createfile import *
writefile('test1.txt')
!7za l writefile.zip
7-Zip (A) 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Listing archive: writefile.zip
--
Path = writefile.zip
Type = zip
Physical Size = 422
Date Time Attr Size Compressed Name
------------------- ----- ------------ ------------ ------------------------
2015-09-22 22:50:13 ....A 98 76 createfile.py
2015-09-22 22:51:08 ....A 52 52 __main__.py
------------------- ----- ------------ ------------ ------------------------
150 128 2 files, 0 folders
答案 1 :(得分:0)
您可以从zip文件导入Python模块,就像它是一个目录一样。
例如,如果TEST_GUI.py
内有TEST_GUI.zip
(只有一个文件,没有文件夹 - 最简单的情况):
def main():
print('Hello world!')
您可以使用单个shell调用来调用它,如下所示:
python -c "import sys;import os;sys.path.append(os.path.abspath('TEST_GUI.zip'));import TEST_GUI; TEST_GUI.main()"
让我逐行解释:
python -c
- 用一段代码作为参数调用Python import sys;import os;
- 导入必要的包sys.path.append(os.path.abspath('TEST_GUI.zip'))
- 这就是魔术发生的地方。 ZIP文件可以像普通文件夹一样添加到PATH
!import TEST_GUI
- 由于Python现在通过PATH
环境变量看到TEST_GUI.py,我们可以导入它。TEST_GUI.main()
- 我们调用main()
函数,它会执行我们想要的任何操作。将os.system
用于相同的脚本非常简单:
>>> os.system("python -c \"import sys;import os;sys.path.append(os.path.abspath('TEST_GUI.zip'));import TEST_GUI; TEST_GUI.main()\"")
Hello world!
0
但是,我建议您删除os.system
并改用subprocess module。