I have Windows 11 and Python 3.12.3 and wish to experiment with the asyncio REPL. It works fine with the built-in REPL:
python -m asyncio
PS C:> python -m asyncio
asyncio REPL 3.12.3 (tags/v3.12.3:f6650f9, Apr 9 2024, 14:05:25) [MSC v.1938 64 bit (AMD64)] on win32
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> await asyncio.sleep(1, 'Done!')
'Done!'
>>> exit()
However, I cannot get it to work with ptpython:
PS C:> ptpython --asyncio
Starting ptpython asyncio REPL
Use "await" directly instead of "asyncio.run()".
In [1]: await asyncio.sleep(1, 'Done!')
Traceback (most recent call last):
File "C:UsersjamesAppDataRoamingPythonPython312site-packagesptpythonrepl.py", line 183, in run_and_show_expression_async
loop.add_signal_handler(signal.SIGINT, lambda *_: task.cancel())
File "C:Program FilesPython312Libasyncioevents.py", line 582, in add_signal_handler
raise NotImplementedError
NotImplementedError
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "C:UsersjamesAppDataRoamingPythonPython312Scriptsptpython.exe__main__.py", line 7, in <module>
File "C:UsersjamesAppDataRoamingPythonPython312site-packagesptpythonentry_pointsrun_ptpython.py", line 231, in run
asyncio.run(embed_result)
File "C:Program FilesPython312Libasynciorunners.py", line 194, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "C:Program FilesPython312Libasynciorunners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:Program FilesPython312Libasynciobase_events.py", line 687, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "C:UsersjamesAppDataRoamingPythonPython312site-packagesptpythonrepl.py", line 528, in coroutine
await repl.run_async()
File "C:UsersjamesAppDataRoamingPythonPython312site-packagesptpythonrepl.py", line 252, in run_async
await self.run_and_show_expression_async(text)
File "C:UsersjamesAppDataRoamingPythonPython312site-packagesptpythonrepl.py", line 206, in run_and_show_expression_async
loop.remove_signal_handler(signal.SIGINT)
File "C:Program FilesPython312Libasyncioevents.py", line 585, in remove_signal_handler
raise NotImplementedError
NotImplementedError
Task exception was never retrieved
future: <Task finished name='Task-314' coro=<PythonRepl.run_and_show_expression_async.<locals>.eval() done, defined at C:UsersjamesAppDataRoamingPythonPython312site-packagesptpythonrepl.py:172> exception=NameError("name 'asyncio' is not defined")>
Traceback (most recent call last):
File "C:UsersjamesAppDataRoamingPythonPython312site-packagesptpythonrepl.py", line 175, in eval
return await self.eval_async(text)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersjamesAppDataRoamingPythonPython312site-packagesptpythonrepl.py", line 329, in eval_async
result = await result
^^^^^^^^^^^^
File "<stdin>", line 1, in <module>
NameError: name 'asyncio' is not defined. Did you forget to import 'asyncio'
From the ptpython docs, this should work – a similar example is shown working:
https://github.com/prompt-toolkit/ptpython
How do I get ptpython to work in asyncio mode on Windows 11?