Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

python - How to generate an executable from an AWS-chalice application?

I have an AWS-chalice project and I want to generate an executable form this project.

So instead of running chalice local to start a local server, I just call the executable file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I came up with this solution, based on Pyinstaller:

  1. Manually inject environment variables
  2. Manually start local server
  3. Manually intercept and handle special events (like SQS, Cron, ...)
  4. Compile the code with Pyinstaller

call this code from at the end of app.py:

import os
from chalice.cli import CLIFactory
from chalice.local import LocalDevServer
def start_standalone(app):
    stage = os.environ.get("stage", "dev")
    print(f"initializing standalone server: {stage}")
    factory = CLIFactory(project_dir=os.getcwd(), debug=True, environ=os.environ, profile="dev")
    config = factory.create_config_obj(
        chalice_stage_name=stage
    )
    os.environ = {**os.environ, **config.environment_variables}

    host = os.environ.get("host", '127.0.0.1')
    port = os.environ.get("port", 8000)
    s = LocalDevServer(app, config=config, host=host, port=port)
    s.serve_forever()
    #handle_special_events(app) # to handle SQS and Cron; didn't include the code as it is not the main goal of this question

At this stage, you can run python app.py to make sure that your changes are valid.

Then using Pyinstaller run pyinstaller -F --add-data="./.chalice/config.json:.chalice" app.py

PS: you might want to move the part about environment variables to the beginning of app.py so it can be used in your initialization.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...