Sub topic 4
Getting and detecting a free-threaded interpreter
This chapter shows how to obtain a free-threaded CPython build and how to confirm what you are running. It assumes you know basic Python, have used threading or concurrent.futures, understand the difference between threads and processes, and know what a race condition and a lock are. The earlier chapters covered what the GIL is and why PEP 703 proposes removing it. Here the question is practical: which python is on your machine, and is its GIL actually off?
Two different questions
Free-threaded Python needs two separate checks, because "no GIL" has two layers.
- Build: Was this interpreter compiled as a free-threaded build? This is fixed when CPython is built and cannot change while the program runs.
- Runtime state: Is the GIL enabled in this particular running process? A free-threaded build can still turn the GIL on, for example when you ask for it or when it loads an incompatible C extension.
A standard build always has the GIL. A free-threaded build usually runs without it, but not always.
| Build type | GIL state at runtime | What it means |
|---|---|---|
| Standard | Enabled | Normal CPython. Threads take turns running Python bytecode. |
| Free-threaded | Disabled | Threads can run Python code in parallel on multiple cores. |
| Free-threaded | Enabled | A free-threaded binary with the GIL switched back on. Behaves like a standard build for threading. |
The table has no "standard build with the GIL disabled" row, because a standard build cannot run without the GIL.
Getting a free-threaded interpreter
Free-threaded builds first shipped as an optional, experimental build in Python 3.13. They are separate from the default interpreter. You install one next to your regular Python and call it by a name ending in t, such as python3.13t. The t stands for "threaded" and appears in several places: the executable name, the ABI flags, and the wheel tags of compatible packages.
Several routes work. Pick the one that fits your platform.
- python.org installers (Windows and macOS): The installer has a custom option to include the free-threaded binaries. On Windows, the launcher then runs it with
py -3.13t. - uv: It can download a free-threaded build for you, with no compiler needed.
- pyenv: It lists free-threaded versions with a
tsuffix, such as3.13t. - conda-forge: It provides free-threaded Python packages as a separate variant.
- Building from source: You configure CPython with
--disable-gil.
Here is the uv route and the source build on Linux or macOS:
# Install a free-threaded interpreter with uv
uv python install 3.13t
# Create a virtual environment that uses it
uv venv --python 3.13t .venv-ft
source .venv-ft/bin/activate
# Confirm which interpreter you are using
python -VV# Alternative: build CPython from source without the GIL
# (run inside a CPython source checkout)
./configure --disable-gil
make -j4
./python -VVThe version banner printed by -VV for a free-threaded build includes the words "free-threading build". For example, it reads Python 3.13.x experimental free-threading build (...). That text is a quick visual check, but you should not parse it in code.
A virtual environment created from python3.13t stays free-threaded. When you install packages into it, pip selects wheels tagged for the free-threaded ABI, which look like cp313t-cp313t in the filename. A package with only standard cp313 wheels will not match. Depending on the package, pip then either builds it from source or fails. This is the first place you will notice compatibility gaps, and a later chapter covers it in detail.
Detecting the build and the GIL state in code
Two small checks answer the two questions from the start of the chapter.
import sys
import sysconfig
# 1 on a free-threaded build, 0 or None on a standard build
print("Free-threaded build:", sysconfig.get_config_var("Py_GIL_DISABLED"))
# Whether the GIL is active in this running interpreter
print("GIL enabled:", sys._is_gil_enabled())Run it with a standard 3.13 interpreter and you will typically see 0 and True. On python3.13t with default settings, you should see 1 and False.
Each check has a specific meaning:
sysconfig.get_config_var("Py_GIL_DISABLED")reports a build-time setting. It is1for a free-threaded build. For a standard build, it is0orNone, depending on platform and version, so test for truth and not for the exact value0.sys._is_gil_enabled()reports the runtime state of the current process. It exists from Python 3.13 onward. Calling it on Python 3.12 or earlier raisesAttributeError.
The leading underscore on _is_gil_enabled marks it as a semi-private name. It is the documented way to ask this question in 3.13, but treat it defensively in code that must run on several versions.
A reusable helper
Library code and test suites often need one function that handles all versions and returns a clear answer. This one combines both checks and tolerates older interpreters:
import sys
import sysconfig
def threading_mode():
"""Return a short description of the interpreter's GIL situation."""
built_free_threaded = bool(sysconfig.get_config_var("Py_GIL_DISABLED"))
# sys._is_gil_enabled exists only on Python 3.13 and later.
check = getattr(sys, "_is_gil_enabled", None)
gil_enabled = check() if check is not None else True
if not built_free_threaded:
return "standard build, GIL always enabled"
if gil_enabled:
return "free-threaded build, but the GIL is enabled"
return "free-threaded build, GIL disabled"
if __name__ == "__main__":
print(sys.version)
print(threading_mode())The branch order matters. On a standard build, the first if returns before the runtime state is considered. On Python 3.12 and earlier, check is None, so gil_enabled defaults to True, which is correct because those versions always have the GIL.
How the pieces fit together
The decision path below shows the order in which the checks give you an answer.
Common errors when checking
Most confusion comes from the same few causes.
Checking the wrong interpreter. If you install python3.13t but run python script.py, you are testing your default build. Print sys.executable and sys.version at the top of any experiment so you know which binary produced the output. Inside a virtual environment, python points to the interpreter the environment was created from.
Assuming a free-threaded build means the GIL is off. The build only makes it possible. If _is_gil_enabled() returns True on a free-threaded build, something turned the GIL on. The usual cause is importing a C extension module that has not declared support for running without the GIL. In that case the interpreter re-enables the GIL for the rest of the process and emits a warning. Later chapters explain this rule and how to override it. Check the GIL state after your imports finish, not before.
Checking too early. The runtime state can change during startup. Calling sys._is_gil_enabled() as the first line of a script, before importing your dependencies, can report False even though a later import turns the GIL back on. For a trustworthy answer, check at the point where your threaded work begins.
Relying on timing. A program that runs faster on four threads is evidence of parallelism but not proof. Other effects, such as I/O overlapping, can produce speedups under the GIL. Use the two checks above for the facts, and use timing only to measure performance.
Quick verification checklist
After installing, run through these steps once:
- Run
python3.13t -VVand confirm the banner mentions free-threading. - Run the detection script and confirm
Py_GIL_DISABLEDis1and_is_gil_enabled()isFalse. - Create a virtual environment from this interpreter, activate it, and rerun the script to confirm the environment inherited the build.
- Install the packages you depend on and rerun the script after importing them. Confirm the GIL is still disabled.
Step 4 is the one people skip, and it is the one that catches an incompatible extension before it surprises you in a benchmark.
Exercise
On a machine with a free-threaded build, your script prints Free-threaded build: 1 and GIL enabled: True after importing a third-party package. The same script before the import printed GIL enabled: False. What most likely happened, and how can you confirm the cause?
Show answer
The package contains a C extension that does not declare support for running without the GIL, so the interpreter re-enabled the GIL when the extension was imported. The build is still free-threaded, which is why Py_GIL_DISABLED stays 1. To confirm, import the packages one at a time and print sys._is_gil_enabled() after each import. The first import after which it returns True is the culprit. The warning the interpreter emits at that moment also names the module.
Exercise
Why does the helper function threading_mode default gil_enabled to True when sys._is_gil_enabled is missing?
Show answer
The function is missing only on Python 3.12 and earlier. Those versions have no free-threaded build, so the GIL is always enabled. Defaulting to True gives the correct answer there, and it avoids an AttributeError.