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