0

Sry, my english skills are very low.

My problem :

I want start a EXE with the "os.system" function. It works...

os.system("Engine\\2.75\\python\\python.exe TEST_GUI.py")

So...now. This Code starts the Python EXE and starts my TEST GUI. But what is to do...when the "TEST GUI.py" is in a ZIP File ?

I want to start the Python.exe with the "TESTGUI.py". I dont wanna extract the zip file before.

Kevin Bose
  • 21
  • 5

2 Answers2

0

This can be done by including a

__main__.py 

file in the zip archive which acts as the entry point to your program. For example given that creatfile.py contains:

def writefile(f):
    fout = open(f, 'wt')
    fout.write('hello world')
    fout.close()

and

__main__.py

contains:

from createfile import *

writefile('test1.txt')

Then, after putting them in writefile.zip, running

os.system('python writefile.zip')

causes test1.txt to be created in the current working directory with 'hello world' written to it.

Showing this in 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
0

You can import Python modules from a zip file as if it was a directory.

For example, if you have TEST_GUI.py inside TEST_GUI.zip (just one file, no folders - a simplest case):

def main():
    print('Hello world!')

you can call it with single shell call as follows:

python -c "import sys;import os;sys.path.append(os.path.abspath('TEST_GUI.zip'));import TEST_GUI; TEST_GUI.main()"

Let me explain it line by line:

  • python -c - call Python with a piece of code as an argument
  • import sys;import os; - import necessary packages
  • sys.path.append(os.path.abspath('TEST_GUI.zip')) - this is where magic happens. ZIP file can be added to PATH just like an ordinary folder!
  • import TEST_GUI - since Python now sees TEST_GUI.py via PATH environment variable, we can import it.
  • TEST_GUI.main() - we call the main() function, and it does whatever we want.

Using os.system for the same script is straightforward:

>>> 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

However, I recommend you to get rid of os.system and use subprocess module instead.

alexanderlukanin13
  • 4,577
  • 26
  • 29