How to Set the Keras Backend with KERAS_BACKEND
Keras 3 doesn't do the maths itself: it runs on a backend, such as TensorFlow, JAX or PyTorch, and picks one when you import it. The KERAS_BACKEND environment variable is how you choose. This guide checks which backend Keras 3.15.1 uses, then switches it to JAX for one command and inside a script.
Syntax
KERAS_BACKEND=<backend> <command>
$env:KERAS_BACKEND = "<backend>"
os.environ["KERAS_BACKEND"] = "<backend>"
| Part | What it means |
|---|---|
KERAS_BACKEND | The environment variable Keras reads when it's imported |
<backend> | The backend's name. The documentation lists "jax", "torch" and "tensorflow" |
<command> | The command to run with that backend. Written this way, the variable applies to that one command only |
$env:KERAS_BACKEND | How PowerShell sets it. It stays set until you close the window or remove it |
os.environ[...] | Sets it from inside a script. It must come before the line import keras |
The Keras documentation gives both ways to choose: "You can export the environment variable KERAS_BACKEND or you can edit your local config file at ~/.keras/keras.json to configure your backend." And one rule: "The backend must be configured before importing Keras, and the backend cannot be changed after the package has been imported."
The Example
A uv project, keras-backend-demo, with TensorFlow added by uv add tensorflow. It checks which backend Keras picked, adds JAX, and switches to it.
Prerequisites
- A uv project with Keras and TensorFlow.
uv add tensorflowinstalls both. This guide uses Keras 3.15.1 and TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.
Step 1: Check Which Backend Keras Uses
uv run python -c "import keras; print(keras.config.backend())"
tensorflow

keras.config.backend() returns the name of the backend Keras is using. Nothing has chosen one yet, so Keras used its default: TensorFlow. The command works unchanged in PowerShell, like every uv run command on this page.
Step 2: See Where the Default Is Saved
- macOS / Linux
- Windows (PowerShell)
cat ~/.keras/keras.json
Get-Content $HOME/.keras/keras.json
{
"floatx": "float32",
"epsilon": 1e-07,
"backend": "tensorflow",
"image_data_format": "channels_last"
}

The first time Keras is imported, it writes this file with its settings if the file doesn't exist yet. "backend": "tensorflow" is the default for every project on your computer that doesn't set KERAS_BACKEND.
Step 3: Add a Second Backend
Keras can only switch to a backend that's installed. Add JAX:
uv add jax
Resolved 35 packages in 1.92s
Built keras-backend-demo @ file:///Users/you/source/keras-backend-demo
Prepared 4 packages in 21.89s
Uninstalled 1 package in 2ms
Installed 4 packages in 27ms
+ jax==0.12.1
+ jaxlib==0.12.1
~ keras-backend-demo==0.1.0 (from file:///Users/you/source/keras-backend-demo)
+ scipy==1.18.1

uv added jax 0.12.1 and the two packages it requires that the project didn't have yet, jaxlib and scipy. The line with ~ is the project itself, which uv uninstalled and installed again.
Step 4: Switch the Backend for One Command
- macOS / Linux
- Windows (PowerShell)
KERAS_BACKEND=jax uv run python -c "import keras; print(keras.config.backend())"
$env:KERAS_BACKEND = "jax"
uv run python -c "import keras; print(keras.config.backend())"
jax

Keras ran on JAX, while keras.json still says "tensorflow": printing the file again showed it unchanged. The variable wins over the file, and only for as long as it's set:
- macOS and Linux:
KERAS_BACKEND=jaxin front of a command sets it for that command only. The next command runs on TensorFlow again. - PowerShell:
$env:KERAS_BACKENDstays set until you close the window.Remove-Item Env:KERAS_BACKENDremoves it, and the check printstensorflowagain.
This first run on JAX took 25 seconds. The JAX runs after it took 1 to 2 seconds.
Step 5: Set the Backend Inside a Script
When a script should always run on one backend, set the variable in the script itself, before it imports Keras:
import os
os.environ["KERAS_BACKEND"] = "jax"
import keras
print(keras.config.backend())
![The in_code.py script in a macOS-style code window: it imports os, sets os.environ["KERAS_BACKEND"] to "jax", then imports keras and prints keras.config.backend().](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/ai/keras/2026/keras-backend-05-code.webp)
os.environ holds the running program's environment variables, so this line sets KERAS_BACKEND for the script, and import keras on the next line reads it.
uv run python in_code.py
jax

JAX again, with nothing set in the terminal. The order matters: the import keras line has to come after the line that sets the variable.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Choosing a backend that isn't installed | Before JAX was added, KERAS_BACKEND=jax stopped at ModuleNotFoundError: No module named 'jax' | Add it first: uv add jax |
Setting os.environ["KERAS_BACKEND"] after import keras | Keras keeps the backend it already picked: the script printed tensorflow | Set it before import keras |
| Writing the name with a capital letter or a typo | KERAS_BACKEND=Jax stopped at ValueError: Unable to import backend : Jax | Write it in lowercase, as the documentation does: jax, torch or tensorflow |
FAQ
Can I change the default for every project?
Yes: change "backend" in ~/.keras/keras.json. With "backend": "jax" in the file, the check from Step 1 printed jax with no variable set. It changes every project on your computer that doesn't set KERAS_BACKEND, so the variable is the safer choice for one project.
Can I change the backend after importing Keras?
The documentation says no: "the backend cannot be changed after the package has been imported". Keras does have keras.config.set_backend(), but its documentation warns that "Using this function is dangerous and should be done carefully", and here keras.config.backend() still printed tensorflow straight after keras.config.set_backend("jax"). Set KERAS_BACKEND before import keras instead.
Conclusion
Keras 3.15.1 ran on TensorFlow, its default, until something chose otherwise. KERAS_BACKEND=jax switched it to JAX for one command, os.environ switched it inside a script before import keras, and ~/.keras/keras.json holds the default for everything else.
Additional Resources
- Configuring your backend — the Keras documentation.
- keras.config.backend — the function that reports the backend.
- Installing JAX — JAX's own installation guide.
