What’s New In Python 3.9¶
- Release
3.9.0a0
- Date
March 15, 2020
This article explains the new features in Python 3.9, compared to 3.8.
For full details, see the Misc/NEWS file.
Note
Prerelease users should be aware that this document is currently in draft form. It will be updated substantially as Python 3.9 moves towards release, so it’s worth checking back even after reading earlier versions.
Summary – Release highlights¶
New Features¶
Other Language Changes¶
builtins.__import__()
now raisesImportError
instead ofValueError
as used to occur when a relative import went past its top-level package. (Contributed by Ngalim Siregar in bpo-37444.)Python now gets the absolute path of the script filename specified on the command line (ex:
python3 script.py
): the__file__
attribute of the__main__
module,sys.argv[0]
andsys.path[0]
become an absolute path, rather than a relative path. These paths now remain valid after the current directory is changed byos.chdir()
. As a side effect, a traceback also displays the absolute path for__main__
module frames in this case. (Contributed by Victor Stinner in bpo-20443.)In development mode and in debug build, encoding and errors arguments are now checked on string encoding and decoding operations. Examples:
open()
,str.encode()
andbytes.decode()
.By default, for best performances, the errors argument is only checked at the first encoding/decoding error, and the encoding argument is sometimes ignored for empty strings. (Contributed by Victor Stinner in bpo-37388.)
New Modules¶
None yet.
Improved Modules¶
threading¶
In a subinterpreter, spawning a daemon thread now raises an exception. Daemon threads were never supported in subinterpreters. Previously, the subinterpreter finalization crashed with a Python fatal error if a daemon thread was still running. (Contributed by Victor Stinner in bpo-37266.)
pprint¶
pprint
can now pretty-print types.SimpleNamespace
.
(Contributed by Carl Bordum Hansen in bpo-37376.)
importlib¶
To improve consistency with import statements, importlib.util.resolve_name()
now raises ImportError
instead of ValueError
for invalid relative
import attempts.
(Contributed by Ngalim Siregar in bpo-37444.)
Optimizations¶
Build and C API Changes¶
Add a new public
PyObject_CallNoArgs()
function to the C API: call a callable Python object without any arguments. It is the most efficient way to call a callable Python object without any argument. (Contributed by Victor Stinner in bpo-37194.)
Deprecated¶
Currently
math.factorial()
acceptsfloat
instances with non-negative integer values (like5.0
). It raises aValueError
for non-integral and negative floats. It is deprecated now. In future Python versions it will raise aTypeError
for all floats. (Contributed by Serhiy Storchaka in bpo-37315.)The
parser
module is deprecated and will be removed in future versions of Python. For the majority of use cases users can leverage the Abstract Syntax Tree (AST) generation and compilation stage, using theast
module.
Removed¶
The undocumented
sys.callstats()
function has been removed. Since Python 3.7, it was deprecated and always returnedNone
. It required a special build optionCALL_PROFILE
which was already removed in Python 3.7. (Contributed by Victor Stinner in bpo-37414.)The
sys.getcheckinterval()
andsys.setcheckinterval()
functions have been removed. They were deprecated since Python 3.2. Usesys.getswitchinterval()
andsys.setswitchinterval()
instead. (Contributed by Victor Stinner in bpo-37392.)The C function
PyImport_Cleanup()
has been removed. It was documented as: “Empty the module table. For internal use only.” (Contributed by Victor Stinner in bpo-36710.)_dummy_thread
anddummy_threading
modules have been removed. These modules were deprecated since Python 3.7 which requires threading support. (Contributed by Victor Stinner in bpo-37312.)aifc.openfp()
alias toaifc.open()
,sunau.openfp()
alias tosunau.open()
, andwave.openfp()
alias towave.open()
have been removed. They were deprecated since Python 3.7. (Contributed by Victor Stinner in bpo-37320.)The
isAlive()
method ofthreading.Thread
has been removed. It was deprecated since Python 3.8. Useis_alive()
instead. (Contributed by Dong-hee Na in bpo-37804.)
Porting to Python 3.9¶
This section lists previously described changes and other bugfixes that may require changes to your code.
Changes in the Python API¶
builtins.__import__()
andimportlib.util.resolve_name()
now raiseImportError
where it previously raisedValueError
. Callers catching the specific exception type and supporting both Python 3.9 and earlier versions will need to catch both:except (ImportError, ValueError):