cannot import name 'core' from partially initialized module 'tensorflow_datasets' most likely due to a circular import
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The error ImportError: cannot import name 'core' from partially initialized module 'tensorflow_datasets' occurs when Python detects a circular import — a situation where module A tries to import module B while module B is still importing module A. With tensorflow_datasets (TFDS), this most commonly happens when you name your own script tensorflow_datasets.py or tensorflow.py, shadowing the real library. It can also result from version mismatches between TensorFlow and TFDS.
The Error
Python shows the file path in the error message. If the path points to your own project directory instead of the installed site-packages, your file is shadowing the library.
Fix 1: Rename Your Script
The most common cause is a file named tensorflow_datasets.py or tensorflow.py in your working directory. Python searches the current directory before site-packages, so your file takes precedence.
Fix 2: Fix Version Mismatches
tensorflow_datasets has internal imports that depend on specific TensorFlow APIs. A major version mismatch causes import failures during module initialization.
Fix 3: Clean Install in a Virtual Environment
Conflicting packages in a global Python installation cause subtle import issues. A virtual environment isolates dependencies.
Fix 4: Check for Other Shadowing Modules
Any file named tensorflow.py, core.py (if imported by TFDS), or a directory named tensorflow_datasets/ with an __init__.py can shadow the real library.
Fix 5: Resolve Actual Circular Imports in Custom Code
If your own modules have circular dependencies that trigger during tensorflow_datasets initialization, restructure imports to break the cycle.
Common Pitfalls
- File named
tensorflow_datasets.pyortensorflow.py: This is the cause in the majority of cases. Python imports your local file instead of the installed package. Always check the file path shown in the error traceback. - Stale
__pycache__or.pycfiles: Even after renaming the offending file, Python may use a cached compiled version. Delete__pycache__/directories and.pycfiles. - Running from the wrong directory: If your working directory contains shadowing files,
cdto a different directory or adjustsys.pathbefore importing. - Incompatible TF and TFDS versions: TFDS 4.x requires TF 2.x. Installing mismatched versions causes internal import errors during module initialization.
- Jupyter notebook kernel caching: Jupyter kernels cache imports. After fixing the issue, restart the kernel (
Kernel > Restart) to clear the cached module state.
Summary
- The error is almost always caused by a local file named
tensorflow_datasets.pyshadowing the real package - Check the file path in the error traceback — it reveals which file Python is importing
- Rename any local files that shadow TensorFlow module names
- Delete
__pycache__/and.pycfiles after renaming - Ensure compatible versions of
tensorflowandtensorflow-datasetsare installed - Use virtual environments to avoid dependency conflicts
Related reading
- Cannot import name 'dtensor' from 'tensorflow.compat.v2.experimental
- Cannot import name 'load_img' from 'keras.preprocessing.image
- cannot import name 'string_int_label_map_pb2
- Cannot import name 'tf_utils' when using importing keras
- cannot import name 'DEFAULT_CIPHERS' from 'urllib3.util.ssl_' on AWS Lambda using a layer
- Cannot import name 'model_lib_v2' from 'object_detection
- Cannot import Tensorflow for GPU on Windows 10
- Cannot import tensorflow in python after source build
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.