pyodide / pyodide/pyodide

SyntaxError: unterminated string literal when using \n in string with pyodide.runPython()

Open
#5,993 4 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
14.8k
Forks
1k
Avg merge
20h 29m
Merged PRs (30d)
19

Description

## 🐛 Bug

When using `pyodide.runPython()` with a string containing a `\n` newline character inside a Python string literal, a `SyntaxError: unterminated string literal` is thrown, even though the string is properly terminated.

### To Reproduce

```html






async function main(){
let pyodide = await loadPyodide();
pyodide.runPython("print('hello\nworld')");
}
main();

```

### Expected behavior

The code should execute without errors and print:
```
hello
world
```

### Environment

- Pyodide Version: 0.29.0
- Browser version: 135.0.7049.72
- Any other relevant information: The error occurs when a newline escape sequence (`\n`) is included inside a single-quoted Python string passed to `runPython()`.

### Additional context

The error message indicates that the Python parser detects an unterminated string literal at the line containing `print('hello`, suggesting that the `\n` escape sequence is being interpreted as a literal newline in the Python source code rather than a string escape, causing the string to be split across lines and appear unterminated.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.