为什么在终端以asyncio模式运行时,Python 3.13.8会抛出缩进错误(IndentationError)?

编程语言 2026-07-11

使用Python >=3.13.0(尝试过3.11.14、3.12.10、3.13.0、3.13.8和 3.14.0a5),当我尝试让终端以异步模式运行时,在尝试启动一个 async with 代码块时,会抛出一个 IndentationError

编辑: 抱歉,我以为这点很清楚,但其实我是用uv运行Python,例如:uv run --python 3.13.0 python -m asyncio。关键概念是 -m asyncio,它会在启动Python时自动加载这个模块

Using CPython 3.13.0
Removed virtual environment at: .venv
Creating virtual environment at: .venv
Installed 6 packages in 29ms
asyncio REPL 3.13.0 (main, Oct 16 2024, 03:23:02) [Clang 18.1.8 ] on linux
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
Ctrl click to launch VS Code Native REPL
>>> import asyncio
>>> import httpx
>>> async with httpx.AsyncClient() as client:
  File "<stdin>", line 1
    async with httpx.AsyncClient() as client:
                                             ^
IndentationError: expected an indented block after 'with' statement on line 1
>>> 
[8]+  Stopped                 uv run --python 3.13.0 python -m asyncio
Using CPython 3.14.0a5
Removed virtual environment at: .venv
Creating virtual environment at: .venv
Installed 6 packages in 34ms
asyncio REPL 3.14.0a5 (main, Feb 12 2025, 14:51:40) [Clang 19.1.6 ] on linux
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
Ctrl click to launch VS Code Native REPL
>>> import asyncio
>>> import httpx
>>> async with httpx.AsyncClient() as client:
  File "<stdin>", line 1
    async with httpx.AsyncClient() as client:
                                             ^
IndentationError: expected an indented block after 'with' statement on line 1
>>> 
[6]+  Stopped                 uv run --python 3.14 python -m asyncio

但在版本 <3.13.0时似乎工作正常:

Using CPython 3.12.10
Removed virtual environment at: .venv
Creating virtual environment at: .venv
Installed 7 packages in 28ms
asyncio REPL 3.12.10 (main, Apr  9 2025, 04:03:51) [Clang 20.1.0 ] on linux
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> import httpx
>>> async with httpx.AsyncClient() as client:
...     r = await client.get('https://www.example.com/')
... 
Traceback (most recent call last):
  ...
>>> 
[5]+  Stopped                 uv run --python 3.12 python -m asyncio
Using CPython 3.11.14 interpreter at: /usr/bin/python3.11
Removed virtual environment at: .venv
Creating virtual environment at: .venv
Installed 7 packages in 33ms
asyncio REPL 3.11.14 (main, Oct 10 2025, 08:54:04) [GCC 13.3.0] on linux
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> import httpx
>>> async with httpx.AsyncClient() as client:
...     r = await client.get('https://www.example.com/')
... 
Traceback (most recent call last):
  ...
>>> 
[7]+  Stopped                 uv run --python 3.11 python -m asyncio

解决方案

在常规条件下,我无法重现这个错误:

% uv run -p 3.13.0 python3 -m asyncio
# asyncio REPL 3.13.0 (main, Oct 16 2024, 08:05:40) [Clang 18.1.8 ] on darwin
# Use "await" directly instead of "asyncio.run()".
# Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> async with asyncio.TaskGroup() as tg:
...     tg
...     
<TaskGroup entered>

然而,如果在环境变量中设置 PYTHON_BASIC_REPL,就会抛出一个 IndentationError

% PYTHON_BASIC_REPL=1 uv run -p 3.13.0 python3 -m asyncio
# asyncio REPL 3.13.0 (main, Oct 16 2024, 08:05:40) [Clang 18.1.8 ] on darwin
# Use "await" directly instead of "asyncio.run()".
# Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> async with asyncio.TaskGroup() as tg:
  File "<stdin>", line 1
    async with asyncio.TaskGroup() as tg:
                                         ^
IndentationError: expected an indented block after 'with' statement on line 1

因此你可以检查在你的shell中是否设置了这个变量,或查看 os.environ

看起来对此的支持被标记为“未计划”:

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章