Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> I personally could not succeed in packaging a uvicorn app, for example.

I kinda did managed to run a fastapi with uvicorn and pyinstaller. And I did nothing special



> And I did nothing special.

I just took the example app from uvicorn.org:

    # main.py
    import uvicorn

    async def app(scope, receive, send):
        assert scope['type'] == 'http'

        await send({
            'type': 'http.response.start',
            'status': 200,
            'headers': [
                [b'content-type', b'text/plain'],
            ],
        })
        await send({
            'type': 'http.response.body',
            'body': b'Hello, world!',
        })

    if __name__ == '__main__':
        uvicorn.run('main:app', port=5000, log_level="info")
And ran "pyinstaller --onefile main.py" on it. The resulting binary, when executed, just says:

    Error loading ASGI app. Could not import module "main".
Please share what exactly did you do that made it work. I already assume it's far from "nothing special"...


#main.py

    from src.webserver import app
    import uvicorn

    if __name__ == '__main__':
        uvicorn.run(
        app,
        host='0.0.0.0',
        port=8080,
    )
#webserver

    from fastapi import FastAPI
    app = FastAPI()

    @app.get('/')
    async def home():
        return {
            "home": True
        }

    @app.on_event("startup")
    async def startup_event():
        import webbrowser
        ip_address = "http://localhost:8080/"
        webbrowser.open(ip_address)
#create a python file for pyinstaller

    import PyInstaller.__main__

    if __name__ == '__main__':
        name = "test"
        pyinstaller_list = [
            'main.py',
            f'--name={name}',
            '--clean',
            '--onefile',
        ]
        PyInstaller.__main__.run(pyinstaller_list)
I remember reading this part 'main:app' where you should import the app instead for pyinstaller.


You are right. I think I've misremembered the module name - it was uwsgi, not uvicorn.

This is a github issue where I discussed my original issue with PyInstaller devs - the dev explained the situation very well: https://github.com/pyinstaller/pyinstaller/issues/6362


My guess is that the main.py file is at the wrong location. Possibly dist/main.py aka dist.main:app.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: