Fixes · Checked on 2026-10-03
SyntaxError: unterminated string literal
The code is fine. Something between the agent and Python changed it.
The error
File "<string>", line 1
print("done
^
SyntaxError: unterminated string literal (detected at line 1)
Why it happens
When an agent runs Python as python -c "…" or through a heredoc, the code goes through the shell's
quoting before Python sees it. Two things go wrong:
- A quote inside the code ends the shell's string early, so Python gets only part of the line.
-
An escape meant for Python, like
\ninside a string, arrives as a real line break. Python then sees a string that stops at the end of a line.
The fix
Write the code to a .py file with the agent's file tool (in Claude Code, the Write tool) and run
the file. A file is read as written; nothing on the way rewrites it.
python check.py
Keep python -c for one-liners with no quotes inside, such as
python -c "import sys; print(sys.version)".
How DMN learned it
It came up, and was fixed, in 11 of our own sessions. DMN kept the fix as one line:
Python 'unterminated string literal' comes from shell quoting mangling inline code; write the code to a
.py file with the Write/Edit tool and run python on it, not python -c or sed.
DMN reads your agents' past sessions on your machine. When the same error is fixed the same way in two or more of them, it keeps the fix as a one-line lesson. When the error turns up again, Claude Code is told the fix straight away, and any agent can search what earlier sessions found. How DMN keeps what agents learn.