Skip to content

ptpython on Windows 11 in asyncio mode does not work #582

Description

@sockduct

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:

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:\Users\james\AppData\Roaming\Python\Python312\site-packages\ptpython\repl.py", line 183, in run_and_show_expression_async
    loop.add_signal_handler(signal.SIGINT, lambda *_: task.cancel())
  File "C:\Program Files\Python312\Lib\asyncio\events.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:\Users\james\AppData\Roaming\Python\Python312\Scripts\ptpython.exe\__main__.py", line 7, in <module>

  File "C:\Users\james\AppData\Roaming\Python\Python312\site-packages\ptpython\entry_points\run_ptpython.py", line 231, in run

    asyncio.run(embed_result)
  File "C:\Program Files\Python312\Lib\asyncio\runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\asyncio\base_events.py", line 687, in run_until_complete

    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\james\AppData\Roaming\Python\Python312\site-packages\ptpython\repl.py", line 528, in coroutine

    await repl.run_async()
  File "C:\Users\james\AppData\Roaming\Python\Python312\site-packages\ptpython\repl.py", line 252, in run_async

    await self.run_and_show_expression_async(text)
  File "C:\Users\james\AppData\Roaming\Python\Python312\site-packages\ptpython\repl.py", line 206, in run_and_show_expression_async

    loop.remove_signal_handler(signal.SIGINT)
  File "C:\Program Files\Python312\Lib\asyncio\events.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:\Users\james\AppData\Roaming\Python\Python312\site-packages\ptpython\repl.py:172> exception=NameError("name 'asyncio' is not defined")>


Traceback (most recent call last):
  File "C:\Users\james\AppData\Roaming\Python\Python312\site-packages\ptpython\repl.py", line 175, in eval

    return await self.eval_async(text)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\james\AppData\Roaming\Python\Python312\site-packages\ptpython\repl.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'

Note it also doesn't work with Python 3.8.10:

PS C:\> python -m asyncio
asyncio REPL 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:34:34) [MSC v.1928 32 bit (Intel)] 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()

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:\users\james\appdata\local\programs\python\python38-32\lib\site-packages\ptpython\repl.py", line 183, in run_
and_show_expression_async
    loop.add_signal_handler(signal.SIGINT, lambda *_: task.cancel())
  File "c:\users\james\appdata\local\programs\python\python38-32\lib\asyncio\events.py", line 536, in add_signal_handler
    raise NotImplementedError
NotImplementedError

Traceback (most recent call last):
  File "c:\users\james\appdata\local\programs\python\python38-32\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\james\appdata\local\programs\python\python38-32\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\users\james\appdata\local\Programs\Python\Python38-32\Scripts\ptpython.exe\__main__.py", line 7, in <module>
  File "c:\users\james\appdata\local\programs\python\python38-32\lib\site-packages\ptpython\entry_points\run_ptpython.py", line 231, in run
    asyncio.run(embed_result)
  File "c:\users\james\appdata\local\programs\python\python38-32\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "c:\users\james\appdata\local\programs\python\python38-32\lib\asyncio\base_events.py", line 616, in run_until_complete
    return future.result()
  File "c:\users\james\appdata\local\programs\python\python38-32\lib\site-packages\ptpython\repl.py", line 528, in coroutine
    await repl.run_async()
  File "c:\users\james\appdata\local\programs\python\python38-32\lib\site-packages\ptpython\repl.py", line 252, in run_async
    await self.run_and_show_expression_async(text)
  File "c:\users\james\appdata\local\programs\python\python38-32\lib\site-packages\ptpython\repl.py", line 206, in run_and_show_expression_async
    loop.remove_signal_handler(signal.SIGINT)
  File "c:\users\james\appdata\local\programs\python\python38-32\lib\asyncio\events.py", line 539, in remove_signal_handler
    raise NotImplementedError
NotImplementedError
Task exception was never retrieved
future: <Task finished name='Task-61' coro=<PythonRepl.run_and_show_expression_async.<locals>.eval() done, defined at c:\users\james\appdata\local\programs\python\python38-32\lib\site-packages\ptpython\repl.py:172> exception=NameError("name 'asyncio' is not defined")>
Traceback (most recent call last):
  File "c:\users\james\appdata\local\programs\python\python38-32\lib\site-packages\ptpython\repl.py", line 175, in eval
    return await self.eval_async(text)
  File "c:\users\james\appdata\local\programs\python\python38-32\lib\site-packages\ptpython\repl.py", line 329, in eval_async
    result = await result
  File "<stdin>", line 1, in <module>
NameError: name 'asyncio' is not defined

Is it possible to get this working on Windows? I see on Stack Overflow, in some cases the event loop can be changed to support various use cases. Is there a configurable option to get this to work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions