Sub topic 4
Getting and detecting a free-threaded interpreter
This chapter assumes you can run Python scripts, use virtual environments, and know that the GIL (Global Interpreter Lock) serializes bytecode execution in standard CPython. You will install a free-threaded interpreter and check what you are running.
A separate build, not a switch
Free-threading ships as a different build of CPython, available from Python 3.13 and marked experimental there. A standard interpreter cannot turn it on. You have to install the free-threaded build, which is usually a second executable next to your normal one.
The executable carries a t suffix: python3.13t instead of python3.13.
Ways to get one
| Method | Command or step |
|---|---|
| python.org installer (Windows, macOS) | Choose the custom install and tick the free-threaded binaries option |
| uv | `uv python install 3.13t` |
| pyenv | `pyenv install 3.13t` |
| Build from source | `./configure --disable-gil`, then `make` |
The --disable-gil configure flag is what defines the free-threaded build. Once installed, create a virtual environment from it as usual:
python3.13t -m venv .venv-ft
source .venv-ft/bin/activate
python -VVThe -VV output names the build. A free-threaded interpreter reports "free-threading build" in its version string.
Two separate questions
Detection needs two checks, because they answer different questions.
- Was this interpreter built without the GIL? This is fixed at build time.
- Is the GIL active right now? A free-threaded build can still run with the GIL on.
The first answer comes from sysconfig. The second comes from sys._is_gil_enabled(), which exists only in Python 3.13 and later. This script handles both and also runs on older versions:
import sys
import sysconfig
def describe_interpreter():
built_free_threaded = bool(sysconfig.get_config_var("Py_GIL_DISABLED"))
# sys._is_gil_enabled() does not exist before Python 3.13.
# Older interpreters always have the GIL.
check = getattr(sys, "_is_gil_enabled", None)
gil_enabled = check() if check is not None else True
print("Version:", sys.version.split()[0])
print("Free-threaded build:", built_free_threaded)
print("GIL enabled:", gil_enabled)
if not built_free_threaded:
print("Result: standard build, threads share one GIL.")
elif gil_enabled:
print("Result: free-threaded build, but the GIL is on.")
else:
print("Result: threads can run Python code in parallel.")
if __name__ == "__main__":
describe_interpreter()On a standard 3.13 build, Py_GIL_DISABLED is 0 (or None on some platforms) and the GIL is enabled. On python3.13t you normally see 1 and False.
The _is_gil_enabled name starts with an underscore, so treat it as a diagnostic tool rather than a stable API for application logic.
Why the GIL can be on in a free-threaded build
Two things can turn the GIL back on in a free-threaded build:
- You ask for it with
PYTHON_GIL=1or-X gil=1. The next chapter covers these controls. - You import a C extension that has not declared free-threading support. The interpreter re-enables the GIL and prints a warning.
A practical check
Put the detection in your test setup or at program start, and log it. When a benchmark shows no speedup, the first thing to learn is whether the GIL was actually off at that point. Run the check after importing your dependencies, because the import is what may flip the state.
Exercise
On python3.13t, sysconfig.get_config_var("Py_GIL_DISABLED") returns 1, but sys._is_gil_enabled() returns True. Give two possible causes and say which check tells you the build type.
Show answer
The GIL may have been forced on with PYTHON_GIL=1 or -X gil=1, or an imported C extension did not declare free-threading support and the interpreter re-enabled the GIL. The Py_GIL_DISABLED config variable tells you the build type (free-threaded). sys._is_gil_enabled() tells you the current runtime state.