Saw that 3.15 came out on 09.10 (https://docs.python.org/3.15/whatsnew/3.15.html). Two things in the notes I don't fully get: lazy import (PEP 810) and UTF-8 becoming the default (PEP 686). I have a bunch of little scripts on a Windows laptop that read and write text files. Anything to worry about?
ВопросPython 3.15 is out: what is "lazy import" and will UTF-8 by default break my scripts?
The UTF-8 one is the change that matters for you. Until now open() without an encoding argument used the system's locale encoding, which on Windows is often not UTF-8. With 3.15, UTF-8 mode is on by default, so those calls read and write UTF-8 everywhere.
If your old files were written in a Windows code page, reading them may now fail or show garbage. The clean fix is to say what you mean:
with open("old_notes.txt", encoding="cp1252") as f:
text = f.read()
with open("report.csv", "w", encoding="utf-8", newline="") as f:
f.write(text)
PEP 686 still lets you switch the mode off with PYTHONUTF8=0, but that's a stopgap rather than a fix.
And lazy import: the module is loaded not at the import line but the first time you actually use the name. Handy for command-line tools where most runs never touch a heavy library:
lazy import json
lazy from decimal import Decimal
def main() -> None:
print("fast start") # json and decimal are not loaded yet
def export(data: dict) -> str:
return json.dumps(data) # json is loaded here, on first use
It is a module-level statement, so for small scripts you won't notice much difference. Code that relies on side effects of importing a module (registering plugins, for example) should keep the normal import.
Thanks both! Last one: a couple of the scripts still run on 3.10. Worth jumping straight to 3.15 or go step by step?
3.10 is end of life since October 2026, so no more security fixes. I think one jump is fine for small scripts, but test each script after.
Писать могут только вошедшие участники. Войти