Importing keras fails if running with -OO, because docstrings are stripped out but the code relies on its presence. Therefore python programs using keras cannot be run at the highest python optimization level.
This is the case after fixing https://github.com/tensorflow/tensorflow/issues/96900 by PR https://github.com/tensorflow/tensorflow/pull/96906.
To reproduce:
$ python3 -OO -c 'import keras'
2025-07-14 15:11:46.826177: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2025-07-14 15:11:46.834852: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:467] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1752498706.846011 582751 cuda_dnn.cc:8579] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
E0000 00:00:1752498706.849585 582751 cuda_blas.cc:1407] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
W0000 00:00:1752498706.857981 582751 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1752498706.857995 582751 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1752498706.857998 582751 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1752498706.857999 582751 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
2025-07-14 15:11:46.860649: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/user/.local/lib/python3.12/site-packages/keras/__init__.py", line 7, in <module>
from keras import _tf_keras as _tf_keras
File "/home/user/.local/lib/python3.12/site-packages/keras/_tf_keras/__init__.py", line 1, in <module>
from keras._tf_keras import keras
File "/home/user/.local/lib/python3.12/site-packages/keras/_tf_keras/keras/__init__.py", line 7, in <module>
from keras import activations as activations
File "/home/user/.local/lib/python3.12/site-packages/keras/activations/__init__.py", line 7, in <module>
from keras.src.activations import deserialize as deserialize
File "/home/user/.local/lib/python3.12/site-packages/keras/src/__init__.py", line 1, in <module>
from keras.src import activations
File "/home/user/.local/lib/python3.12/site-packages/keras/src/activations/__init__.py", line 34, in <module>
from keras.src.saving import object_registration
File "/home/user/.local/lib/python3.12/site-packages/keras/src/saving/__init__.py", line 7, in <module>
from keras.src.saving.saving_api import load_model
File "/home/user/.local/lib/python3.12/site-packages/keras/src/saving/saving_api.py", line 7, in <module>
from keras.src.legacy.saving import legacy_h5_format
File "/home/user/.local/lib/python3.12/site-packages/keras/src/legacy/saving/legacy_h5_format.py", line 9, in <module>
from keras.src import optimizers
File "/home/user/.local/lib/python3.12/site-packages/keras/src/optimizers/__init__.py", line 2, in <module>
from keras.src.optimizers.adadelta import Adadelta
File "/home/user/.local/lib/python3.12/site-packages/keras/src/optimizers/adadelta.py", line 133, in <module>
Adadelta.__doc__ = Adadelta.__doc__.replace(
^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'replace'