Python 3.7运行PiSugar项目遇TypeError错误求助
TypeError: 'type' object is not subscriptable in PiSugar Server with Python 3.7 Hey there, I’ve run into this exact kind of issue before with older Python versions—let’s break down what’s happening and how to fix it.
Why This Error Happens
The error points directly to this function definition:
def connect_tcp(host="127.0.0.1", port=8423, timeout: float = None) -> tuple[socket.socket, socket.socket]:
Python 3.7 doesn’t support the tuple[Type1, Type2] shorthand for type annotations. This syntax was introduced in Python 3.9 via PEP 585, so your 3.7 interpreter can’t parse it, hence the "type not subscriptable" error.
Step-by-Step Fixes
Option 1: Modify the Code to Be 3.7-Compatible (Quick Fix)
Find the file containing the connect_tcp function (it’s likely in the project’s core networking module), then make these small changes:
- At the top of the file, add this import:
import typing - Replace the return type annotation from
tuple[socket.socket, socket.socket]totyping.Tuple[socket.socket, socket.socket]
Alternatively, you can add this line at the very top of the file (before any other imports) to enable delayed annotation parsing—this lets 3.7 understand the newer syntax without rewriting the annotation:
from __future__ import annotations
Pro tip: Backup the original file before making changes to avoid breaking anything if you make a typo.
Option 2: Upgrade Python to 3.9+ (Recommended Long-Term Fix)
Python 3.7 is end-of-life (no longer receiving security updates), so upgrading to a supported version is better for both compatibility and security. On Debian:
- Update your package list:
sudo apt update - Install Python 3.9 and virtual environment support:
sudo apt install python3.9 python3.9-venv - Create a virtual environment to isolate the PiSugar project from your system’s default Python:
python3.9 -m venv pisugar-env source pisugar-env/bin/activate - Install the project’s dependencies and run it as usual:
pip install -r requirements.txt # Start the PiSugar server with your normal command
Option 3: Check for a Python 3.7-Compatible Project Version
If you can’t upgrade Python or modify code, quickly scan the project’s tags/branches to see if there’s an older release that explicitly supports 3.7. Since you mentioned developer response is slow, this might not be available, but it’s worth a quick check just in case.
内容的提问来源于stack exchange,提问作者Insanelysick

