tensorflow
tensorflow_datasets
circular import
python error
module import

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.

Practice ML system design

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
1# ImportError: cannot import name 'core' from partially initialized module
2# 'tensorflow_datasets' (most likely due to a circular import)
3# (/path/to/your/project/tensorflow_datasets.py)
4
5import tensorflow_datasets as tfds

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

bash
1# Check the file path in the error message
2# If it shows: /your/project/tensorflow_datasets.py — that's the problem
3
4# Rename your file
5mv tensorflow_datasets.py my_dataset_loader.py
6
7# Also remove the compiled cache
8rm -f tensorflow_datasets.pyc
9rm -rf __pycache__
python
1# Verify the correct module is loaded
2import tensorflow_datasets as tfds
3print(tfds.__file__)
4# Should show: .../site-packages/tensorflow_datasets/__init__.py

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

bash
1# Check installed versions
2pip show tensorflow tensorflow-datasets
3
4# Install compatible versions
5pip install --upgrade tensorflow tensorflow-datasets
6
7# Or pin specific compatible versions
8pip install tensorflow==2.15.0 tensorflow-datasets==4.9.4
9
10# For TF 2.x, TFDS 4.x is typically required
11# For TF 1.x, TFDS 1.x or 2.x
python
1# Verify compatibility at runtime
2import tensorflow as tf
3import tensorflow_datasets as tfds
4
5print(f"TF version: {tf.__version__}")
6print(f"TFDS version: {tfds.__version__}")

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

bash
1# Create a clean virtual environment
2python -m venv tfds_env
3source tfds_env/bin/activate  # Linux/macOS
4# tfds_env\Scripts\activate   # Windows
5
6# Install fresh
7pip install tensorflow tensorflow-datasets
8
9# Test the import
10python -c "import tensorflow_datasets as tfds; print(tfds.__version__)"

Conflicting packages in a global Python installation cause subtle import issues. A virtual environment isolates dependencies.

Fix 4: Check for Other Shadowing Modules

python
1import sys
2
3# Check if any project files shadow tensorflow modules
4import importlib
5
6for module_name in ['tensorflow', 'tensorflow_datasets', 'google']:
7    spec = importlib.util.find_spec(module_name)
8    if spec and spec.origin:
9        print(f"{module_name}: {spec.origin}")
10        # Should point to site-packages, not your project
11
12# Check sys.path for problematic entries
13for path in sys.path:
14    print(path)
15# The current directory '' or '.' appears first — files here shadow packages
bash
1# Check for problematic files in your project
2ls *.py | grep -E "^(tensorflow|google|core)\.py$"
3
4# Also check for __init__.py files creating packages
5ls -la tensorflow_datasets/ 2>/dev/null

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

python
1# BAD: circular import between your modules
2# file: data_loader.py
3import tensorflow_datasets as tfds
4from model import build_model  # model.py imports data_loader
5
6# file: model.py
7from data_loader import load_data  # circular!
8
9# FIX: use lazy imports inside functions
10# file: model.py
11def train():
12    from data_loader import load_data  # import only when needed
13    data = load_data()
14    # ...
15
16# FIX: restructure to avoid the cycle
17# file: config.py (shared module, no imports from model or data_loader)
18# file: data_loader.py (imports config, not model)
19# file: model.py (imports config and data_loader)

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.py or tensorflow.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 .pyc files: Even after renaming the offending file, Python may use a cached compiled version. Delete __pycache__/ directories and .pyc files.
  • Running from the wrong directory: If your working directory contains shadowing files, cd to a different directory or adjust sys.path before 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.py shadowing 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 .pyc files after renaming
  • Ensure compatible versions of tensorflow and tensorflow-datasets are installed
  • Use virtual environments to avoid dependency conflicts

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design