pytorch - ✅(Solved) Fix import torch fails with release 2.11 on python 3.13, AST parsing error (IndentationError) [2 pull requests, 9 comments, 5 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#178255Fetched 2026-04-08 01:20:59
View on GitHub
Comments
9
Participants
5
Timeline
86
Reactions
0
Author
Assignees
Timeline (top)
mentioned ×21subscribed ×21labeled ×10commented ×9

Error Message

uv venv --python=3.13; source .venv/bin/activate Using CPython 3.13.8 Creating virtual environment at: .venv Activate with: source .venv/bin/activate

uv pip install torch==2.11
Resolved 10 packages in 1.84s Prepared 4 packages in 4.19s Installed 10 packages in 278ms

  • filelock==3.25.2
  • fsspec==2026.2.0
  • jinja2==3.1.6
  • markupsafe==3.0.3
  • mpmath==1.3.0
  • networkx==3.6.1
  • setuptools==81.0.0
  • sympy==1.14.0
  • torch==2.11.0
  • typing-extensions==4.15.0

python -c "import torch" Traceback (most recent call last): File "<string>", line 1, in <module> import torch File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/init.py", line 2229, in <module> from torch import _VF as _VF, functional as functional # usort: skip ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/functional.py", line 8, in <module> import torch.nn.functional as F File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/nn/init.py", line 8, in <module> from torch.nn.modules import * # usort: skip # noqa: F403 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/nn/modules/init.py", line 157, in <module> from .rnn import GRU, GRUCell, LSTM, LSTMCell, RNN, RNNBase, RNNCell, RNNCellBase File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/nn/modules/rnn.py", line 486, in <module> class RNN(RNNBase): ...<329 lines>... return output, self.permute_hidden(hidden, unsorted_indices) File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/nn/modules/rnn.py", line 657, in RNN @torch._jit_internal._overload_method # noqa: F811 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/_jit_internal.py", line 1100, in _overload_method _check_overload_body(func) ~~~~~~~~~~~~~~~~~~~~^^^^^^ File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/_jit_internal.py", line 1015, in _check_overload_body parsed_def = parse_def(func) File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/_sources.py", line 127, in parse_def py_ast = ast.parse(dedent_src) File "~/.local/share/uv/python/cpython-3.13.8-macos-aarch64-none/lib/python3.13/ast.py", line 50, in parse return compile(source, filename, mode, flags, _feature_version=feature_version, optimize=optimize) File "<unknown>", line 9 ) -> tuple[Tensor, Tensor]: ^ IndentationError: expected an indented block after function definition on line 4

PR fix notes

PR #4: Add preflight checks for macOS compatibility issues

Description (problem / solution / changelog)

Summary

  • MemoryStore에 sqlite extension 로딩 사전 검증 추가 — enable_load_extension 미지원 시 원인과 해결 방법을 명확히 안내
  • LocalEmbeddingProvider에 sentence-transformers import 실패 시 Python 3.13.8 AST 리그레션 안내 추가
  • 서버 시작 전 _preflight_checks() — sqlite extension 지원 + torch import를 서버 시작 전에 검증
  • README에 macOS 호환성 매트릭스 및 권장 설치 방법 추가

Context

macOS 환경에서 Python 설치 방식에 따라 서버 시작이 실패하는 보고가 있었습니다:

  1. python.org macOS 인스톨러 — --enable-loadable-sqlite-extensions 빌드 플래그 누락
  2. Python 3.13.8 — CPython ast.parse() 리그레션으로 torch import 실패 (pytorch/pytorch#178255)

두 문제 모두 upstream 환경 문제로 코드 레벨 근본 수정은 불가하나, 사전 검증과 명확한 에러 메시지로 사용자 경험을 개선합니다.

Test plan

  • 9개 새 preflight 테스트 추가 (mock 기반)
  • 기존 전체 테스트 114개 통과 확인
  • macOS python.org 인스톨러에서 에러 메시지 확인
  • Python 3.13.8 homebrew에서 에러 메시지 확인

🤖 Generated with Claude Code

Changed files

  • README.md (modified, +31/-5)
  • docs/superpowers/specs/2026-03-27-session-handoff-design.md (added, +164/-0)
  • src/memory_decay/embedding_provider.py (modified, +11/-1)
  • src/memory_decay/memory_store.py (modified, +26/-3)
  • src/memory_decay/server.py (modified, +48/-0)
  • tests/test_preflight.py (added, +109/-0)

PR #179066: [JIT] Silence CPython 3.13.8 inspect.getsourcelines() bug

Description (problem / solution / changelog)

Stack from ghstack (oldest at bottom):

  • -> #179066

Silences spurious indent error raised due to https://github.com/python/cpython/issues/139783 The fix catches IndentationError in torch._jit_internal._overload_method, and skips the overload body checks in that case on 3.13.8

Fixes https://github.com/pytorch/pytorch/issues/178255

Co-authored-by: Claude [email protected]

Changed files

  • torch/_jit_internal.py (modified, +18/-1)

Code Example

> uv venv --python=3.13; source .venv/bin/activate
Using CPython 3.13.8
Creating virtual environment at: .venv
Activate with: source .venv/bin/activate

> uv pip install torch==2.11                                    
Resolved 10 packages in 1.84s
Prepared 4 packages in 4.19s
Installed 10 packages in 278ms
 + filelock==3.25.2
 + fsspec==2026.2.0
 + jinja2==3.1.6
 + markupsafe==3.0.3
 + mpmath==1.3.0
 + networkx==3.6.1
 + setuptools==81.0.0
 + sympy==1.14.0
 + torch==2.11.0
 + typing-extensions==4.15.0

> python -c "import torch"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import torch
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/__init__.py", line 2229, in <module>
    from torch import _VF as _VF, functional as functional  # usort: skip
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/functional.py", line 8, in <module>
    import torch.nn.functional as F
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/nn/__init__.py", line 8, in <module>
    from torch.nn.modules import *  # usort: skip # noqa: F403
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/nn/modules/__init__.py", line 157, in <module>
    from .rnn import GRU, GRUCell, LSTM, LSTMCell, RNN, RNNBase, RNNCell, RNNCellBase
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/nn/modules/rnn.py", line 486, in <module>
    class RNN(RNNBase):
    ...<329 lines>...
            return output, self.permute_hidden(hidden, unsorted_indices)
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/nn/modules/rnn.py", line 657, in RNN
    @torch._jit_internal._overload_method  # noqa: F811
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/_jit_internal.py", line 1100, in _overload_method
    _check_overload_body(func)
    ~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/_jit_internal.py", line 1015, in _check_overload_body
    parsed_def = parse_def(func)
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/_sources.py", line 127, in parse_def
    py_ast = ast.parse(dedent_src)
  File "~/.local/share/uv/python/cpython-3.13.8-macos-aarch64-none/lib/python3.13/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
                   _feature_version=feature_version, optimize=optimize)
  File "<unknown>", line 9
    ) -> tuple[Tensor, Tensor]:
                               ^
IndentationError: expected an indented block after function definition on line 4

---

> curl -sL https://raw.githubusercontent.com/pytorch/pytorch/main/torch/utils/collect_env.py | python

Collecting environment information...
PyTorch version: 2.11.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 14.8.4 (arm64)
GCC version: Could not collect
Clang version: 20.1.5
CMake version: version 3.30.2
Libc version: N/A

# actually python 3.13, see above
Python version: 3.12.11 (main, Oct  7 2025, 15:53:48) [Clang 20.1.4 ] (64-bit runtime)
Python platform: macOS-14.8.4-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
Caching allocator config: N/A

CPU:
Apple M3 Pro

Versions of relevant libraries:
[pip3] Could not collect
[conda] Could not collect
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

official release 2.11, with python3.13 (works fine with python3.12)

> uv venv --python=3.13; source .venv/bin/activate
Using CPython 3.13.8
Creating virtual environment at: .venv
Activate with: source .venv/bin/activate

> uv pip install torch==2.11                                    
Resolved 10 packages in 1.84s
Prepared 4 packages in 4.19s
Installed 10 packages in 278ms
 + filelock==3.25.2
 + fsspec==2026.2.0
 + jinja2==3.1.6
 + markupsafe==3.0.3
 + mpmath==1.3.0
 + networkx==3.6.1
 + setuptools==81.0.0
 + sympy==1.14.0
 + torch==2.11.0
 + typing-extensions==4.15.0

> python -c "import torch"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import torch
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/__init__.py", line 2229, in <module>
    from torch import _VF as _VF, functional as functional  # usort: skip
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/functional.py", line 8, in <module>
    import torch.nn.functional as F
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/nn/__init__.py", line 8, in <module>
    from torch.nn.modules import *  # usort: skip # noqa: F403
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/nn/modules/__init__.py", line 157, in <module>
    from .rnn import GRU, GRUCell, LSTM, LSTMCell, RNN, RNNBase, RNNCell, RNNCellBase
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/nn/modules/rnn.py", line 486, in <module>
    class RNN(RNNBase):
    ...<329 lines>...
            return output, self.permute_hidden(hidden, unsorted_indices)
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/nn/modules/rnn.py", line 657, in RNN
    @torch._jit_internal._overload_method  # noqa: F811
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/_jit_internal.py", line 1100, in _overload_method
    _check_overload_body(func)
    ~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/_jit_internal.py", line 1015, in _check_overload_body
    parsed_def = parse_def(func)
  File "/path/to/repo/.venv/lib/python3.13/site-packages/torch/_sources.py", line 127, in parse_def
    py_ast = ast.parse(dedent_src)
  File "~/.local/share/uv/python/cpython-3.13.8-macos-aarch64-none/lib/python3.13/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
                   _feature_version=feature_version, optimize=optimize)
  File "<unknown>", line 9
    ) -> tuple[Tensor, Tensor]:
                               ^
IndentationError: expected an indented block after function definition on line 4

Thanks in advance for the support ❤️

Versions

[important] The collect_env.py script fails with the same import error, I run the it with python 3.12 but the original error happens with python 3.13

 > curl -sL https://raw.githubusercontent.com/pytorch/pytorch/main/torch/utils/collect_env.py | python

Collecting environment information...
PyTorch version: 2.11.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 14.8.4 (arm64)
GCC version: Could not collect
Clang version: 20.1.5
CMake version: version 3.30.2
Libc version: N/A

# actually python 3.13, see above
Python version: 3.12.11 (main, Oct  7 2025, 15:53:48) [Clang 20.1.4 ] (64-bit runtime)
Python platform: macOS-14.8.4-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
Caching allocator config: N/A

CPU:
Apple M3 Pro

Versions of relevant libraries:
[pip3] Could not collect
[conda] Could not collect

cc @seemethere @malfet @atalman @tinglvv @nWEIdia @ezyang @gchanan @kadeng @msaroufim @EikanWang @jgong5 @wenzhe-nrv @sanchitintel

extent analysis

Fix Plan

The issue is caused by an incompatibility between PyTorch 2.11 and Python 3.13. To fix this, you can try the following steps:

  • Downgrade Python to 3.12: Since the issue only occurs with Python 3.13, downgrading to Python 3.12 should resolve the problem.
  • Upgrade PyTorch to a version compatible with Python 3.13: If downgrading Python is not feasible, you can try upgrading PyTorch to a version that is compatible with Python 3.13.

Here's an example of how to downgrade Python using uv:

uv venv --python=3.12
source .venv/bin/activate

And here's an example of how to upgrade PyTorch:

pip install --upgrade torch

However, since the issue is caused by a syntax error in the PyTorch code, it's likely that the PyTorch version you're using is not compatible with Python 3.13. In this case, downgrading Python to 3.12 is the most straightforward solution.

Verification

To verify that the fix worked, you can try importing PyTorch again:

python -c "import torch"

If the import is successful, it means the fix worked.

Extra Tips

  • Make sure to check the PyTorch documentation for compatibility with different Python versions.
  • If you're using a virtual environment, ensure that the correct Python version is being used.
  • If you're experiencing issues with other libraries, try checking their documentation for compatibility with Python 3.13.

Example code to check Python version:

import sys
print(sys.version)

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - ✅(Solved) Fix import torch fails with release 2.11 on python 3.13, AST parsing error (IndentationError) [2 pull requests, 9 comments, 5 participants]