Skip to content

Instantly share code, notes, and snippets.

@0atman
Last active September 7, 2017 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0atman/713f7a60a64d3dfc7d1216c58dcbbbb1 to your computer and use it in GitHub Desktop.
Save 0atman/713f7a60a64d3dfc7d1216c58dcbbbb1 to your computer and use it in GitHub Desktop.
A simple wrapper around `pex`. For self-contained python scripts.
#!/usr/bin/env sh
requirements=$1
script=$2
exec pex $requirements -- $script
@0atman
Copy link
Author

0atman commented Sep 7, 2017

Install to /usr/bin/pexed.

An example script using pexed:

#!/usr/bin/pexed flask flask_restful
from flask import Flask
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)


class HelloWorld(Resource):
    def get(self):
        return {'hello': 'world'}


api.add_resource(HelloWorld, '/')

if __name__ == '__main__':
    app.run()

@0atman
Copy link
Author

0atman commented Sep 7, 2017

This wrapper around pex is needed because of the way shebangs pass everything after the cmd (ie flask flask_restful) as a single string, rather than splitting them into arguments, as you might expect.
[source]

@0atman
Copy link
Author

0atman commented Sep 7, 2017

pex smartly caches your requirements, so the second time you run the script, there's no delay, and pinning is supported by pex (requests==2.18.4)

@0atman
Copy link
Author

0atman commented Sep 7, 2017

A general-purpose solution to this is https://github.com/jordansissel/shebang

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment