ImportError cannot import name 'set_random_seed' from 'tensorflow' CUserspolonAnaconda3libsite-packagestensorflow__init__.py
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
When developing machine learning models with TensorFlow, you will inevitably run into import errors as the library evolves across major versions. One particularly confusing error is ImportError: cannot import name 'set_random_seed' from 'tensorflow'. This happens because tf.set_random_seed was removed in TensorFlow 2.x and replaced with a new API location, so code written for TensorFlow 1.x breaks when run against a newer installation.
Why This Error Occurs
In TensorFlow 1.x, you could set a global random seed for reproducibility like this:
When TensorFlow 2.0 arrived, the team reorganized the entire API surface. Many top-level functions were moved into submodules, renamed, or removed entirely. The function tf.set_random_seed was replaced by tf.random.set_seed. If you attempt the old import on a TensorFlow 2.x installation, Python raises an ImportError because the name simply does not exist at tensorflow.__init__ anymore.
The Fix: Use the Updated API
The correct way to set a global random seed in TensorFlow 2.x is:
This single change resolves the import error. The function behaves the same way as the old one: it seeds TensorFlow's internal random number generators so that operations like weight initialization, dropout, and data shuffling produce reproducible results.
Handling Code That Must Support Both Versions
If you maintain a library or codebase that needs to run on both TensorFlow 1.x and 2.x, you can write a compatibility wrapper:
This approach uses hasattr to detect which API is available at runtime, avoiding the ImportError entirely.
Full Reproducibility Requires More Than One Seed
Setting the TensorFlow seed alone does not guarantee fully reproducible results. Python's built-in random module and NumPy also use their own random states. For true reproducibility, seed all three:
Setting PYTHONHASHSEED ensures that Python's hash-based operations (like dictionary ordering in older Python versions) are also deterministic. This is especially important when your data pipeline includes shuffling or hashing steps.
Other Commonly Moved Functions in TensorFlow 2.x
The set_random_seed rename is not an isolated case. Here are several other functions that moved between versions:
If you are migrating a large TensorFlow 1.x codebase, the official migration script can automate many of these renames:
This command-line tool scans your code and rewrites the deprecated API calls to their TensorFlow 2.x equivalents.
Common Pitfalls
- Copying old tutorials verbatim. Many TensorFlow 1.x tutorials still rank highly in search results. Always check the TensorFlow version the tutorial targets before copying import statements.
- Using
tf.compat.v1as a permanent fix. Whiletf.compat.v1.set_random_seedworks in TensorFlow 2.x, relying on the compatibility module indefinitely means you miss performance improvements and new features in the native 2.x API. - Forgetting to seed NumPy and Python's random module. TensorFlow operations may call into NumPy internally, so seeding only TensorFlow does not guarantee reproducibility across your entire pipeline.
- Assuming GPU results will be deterministic. Even with all seeds set, GPU floating-point operations can produce non-deterministic results due to parallel reduction ordering. Set
TF_DETERMINISTIC_OPS=1if you need strict determinism. - Installing mismatched TensorFlow and Keras versions. In TensorFlow 2.x, Keras is bundled as
tf.keras. Installing a standalonekeraspackage alongside TensorFlow can cause conflicting imports and confusing errors.
Summary
- The
ImportErrorforset_random_seedoccurs because TensorFlow 2.x moved this function totf.random.set_seed. - Replace
tf.set_random_seed(seed)withtf.random.set_seed(seed)to fix the error immediately. - For cross-version compatibility, use
hasattrchecks to call the correct function at runtime. - Full reproducibility requires seeding Python, NumPy, and TensorFlow together, and optionally enabling deterministic GPU operations.
- Use the
tf_upgrade_v2migration tool to automatically update deprecated API calls across an entire codebase.
Related reading
- ImportError cannot import name 'to_categorical' from 'keras.utils' /usr/local/lib/python3.7/dist-packages/keras/utils/__init__.py
- ImportError Could not import the Python Imaging Library PIL required to load image files on tensorflow
- ImportError Failed to import any qt binding, Python - Tensorflow
- ImportError libcublas.so.10.0 cannot open shared object file No such file or directory
- In-graph replication vs Between-graph replication
- In-order iterator for binary tree
- ImportError cannot import name 'url' from 'django.conf.urls' after upgrading to Django 4.0
- ImportError Could not find 'cudart64_100.dll

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.