This is a hidden gem. Check this out:
~/Desktop/ziptest
▶ ls
__main__.py test.py
~/Desktop/ziptest
▶ zip ziptest.zip __main__.py test.py
adding: __main__.py (deflated 13%)
adding: test.py (deflated 21%)
~/Desktop/ziptest
▶ ls
__main__.py test.py ziptest.zip
~/Desktop/ziptest
▶ echo '#!/usr/bin/env python' | cat - ziptest.zip > ziptest
~/Desktop/ziptest
▶ ls
__main__.py test.py test.pyc ziptest ziptest.zip
~/Desktop/ziptest
▶ chmod +x ziptest
~/Desktop/ziptest
▶ ./ziptest hi
hi
~/Desktop/ziptest
▶ mv ziptest /usr/local/bin/ziptest
~/Desktop/ziptest
▶ ziptest hello
hello
You don't need the .zip filetype suffix on the archive as Hikmat demonstrated above (tested on Mac OSX).
main.py
#!/usr/bin/env python
# encoding: utf-8
# file: __main__.py
import sys
from test import main
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
test.py
#!/usr/bin/env python
# encoding: utf-8
# file: test.py
import sys
def main(argv):
if len(argv) > 0:
print(argv[0])
else:
print('No arguments!')
if __name__ == '__main__':
main(sys.argv[1:])
Thanks much for sharing this @mehikmat. After a bit of digging, it appears that this was released in Python 2.6.
Here is an approach for Windows from the first comment on the above link (Andy Chu):
On Windows, you can do a similar thing by making a file that is both a zip file and a batch file. The batch file will pass %~f0 (itself) to the -z flag of the Python interpreter.
This is an interesting tip. Where does the interpreter start execution of the scripts in the archive?
Great tip Dylan. The port can be modified from the 8000 default by adding the port number at the end of the command.
For Rubyists, @dallas posted this Ruby one-liner here on Coderwall:
ruby -run -e httpd . -p 8000
Thanks Dmitry. I will check it out. Appreciate the link. -C
+1 thanks much for this post.
Great tip Dung. I am guessing that it is possible to modify the syntax highlighting so that it works with other languages by modifying the setting in this line?
jsEditor.getSession().setMode("ace/mode/javascript")
Really enjoyed this post. Thanks Dan.
great suggestion. thanks much!
Great thread!
Thanks! I will check it out. -C