The approach described below is not using freezer and is suitable for python3 and working on Mac and Linux
In order to use python2 modify the shebang and pip tool to use python2
Significant Portions of the information below (beside the flask example and dependencies … ) are mentioned in this blog post Bundling Python files into a stand-alone executable
Additional information related to zip application are explained in The Python Zip Application Archive Format
Lets assume the flask application is as follow :
from flask import Flask, redirect, url_for, request
app = Flask(__name__)
@app.route('/')
def root():
static_page='''
<html>
<body>
<form action = "http://localhost:5000/login" method = "post">
<p>Enter Name:</p>
<p><input type = "text" name = "nm" /></p>
<p><input type = "submit" value = "submit" /></p>
</form>
</body>
</html>
'''
return static_page
@app.route('/success/<name>')
def success(name):
return 'welcome %s' % name
@app.route('/login',methods = ['POST', 'GET'])
def login():
if request.method == 'POST':
user = request.form['nm']
return redirect(url_for('success',name = user))
else:
user = request.args.get('nm')
return redirect(url_for('success',name = user))
def main():
app.run()
It will be named moosh1.py.
Now constructing the following folder hierarchy:
app/
| __main__.py
| moosh/
| __init__.py
| moosh1.py
Where __main__.py is as follow:
import sys
import moosh
if __name__ == '__main__':
print("hello moosh1 - running on python %s.%s" %
(sys.version_info[0], sys.version_info[1]))
moosh.main()
and moosh/__init__.py is as follow:
from .moosh1 import main
__all__ = ['main']
For portability purposes install the flask dependencies into app folder
$ cd app
$ pip3 install flask --target ./
$ cd ..
Now zip it
$ cd app
$ zip -r ../app.zip *
$ cd ..
And now add shebang and make our stand-alone app executable
$ echo '#!/usr/bin/env python3' | cat - app.zip > app
$ chmod +x app
And there it is, app is a stand-alone python program with all the relevant dependencies
More elaborate programs will require more tweaking and may not work with this approach out of the box.
The largest program i found using a scheme like this is youtube-dl What is this binary file? Where has the code gone?