[Python] Python run own module in async shell

Discussão em 'Python' iniciado por Stack, Abril 6, 2025.

  1. Stack

    Stack Membro Participativo

    I wrote a little module that makes heavy use of pythons async functionality.

    If you run python -m asyncio you are dropped into a shell where you can await coroutines directly. Additionally, asyncio is imported automatically (you can see it being imported).

    I want to be able to run my module in a similar way. python -m my_module shall drop me into a shell like python -m asyncio, only with a few additional imports and variables, so you can directly run things like await my_module.ct.connect('uri') and things like that.

    Is there any way to achive this in a clean way?

    EDIT:

    I tried rebuilding asyncio.__main__, but it wasn't very practical for me. At one point, they do a from _pyrepl.main import CAN_USE_PYREPL, which I wasn't able to perform in my python script. I haven't understood exactly how it works, but running the python interpreter doesn't expose _pyrepl, and normal module pyrepl has no submodule pyrepl.main. Also, asyncio.__main__ changes significantly between python version 3.12 and 3.13.

    Continue reading...

Compartilhe esta Página