'module' object has no attribute 'SummaryWriter'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The error 'module' object has no attribute 'SummaryWriter' usually means PyTorch TensorBoard support is being imported from the wrong place, the environment is missing a required package, or a local file is shadowing the real module. The fix is generally straightforward once you verify the import path and the installed versions. In most modern PyTorch setups, SummaryWriter should come from torch.utils.tensorboard.
Use the Correct Import
The normal import looks like this:
This is the supported PyTorch-side wrapper around TensorBoard logging.
The error often appears when code tries something like:
That can work in some environments, but it is clearer and safer to import SummaryWriter directly from the module where it is defined.
Make Sure TensorBoard Is Installed
PyTorch integrates with TensorBoard, but the Python tensorboard package still needs to exist in the environment.
Check installation:
If TensorBoard is missing:
Then retry the import:
Missing TensorBoard support is a common reason for the import path behaving unexpectedly.
Watch for Local Module Shadowing
A very common Python bug is naming a local file or folder the same as a real package. If your project contains files such as these:
- '
tensorboard.py' - '
torch.py' - '
torch/'
Python may import your local file instead of the real package.
Quick check:
If the printed path points into your project in an unexpected way, you have a shadowing problem. Rename the local file or directory and retry.
Verify the PyTorch Version
Older PyTorch versions may not expose TensorBoard integration in the same way as current ones. Check the installed version:
If you are on an old version, upgrading may solve the issue:
When working in managed environments, do not assume the notebook kernel or virtual environment is using the same interpreter where you ran pip.
Use a Minimal Smoke Test
When the import works, run a tiny end-to-end example to confirm logging is functional.
Then launch TensorBoard:
If the dashboard shows the scalar series, the environment is configured correctly.
Avoid Ambiguous Import Patterns
Prefer explicit imports in examples and production code:
Avoid patterns that rely on assumptions about module attributes unless you have verified them in the exact environment you deploy.
Also keep one Python environment per project when possible. Mixed global and virtual-environment installs are a frequent source of these module errors.
Common Pitfalls
The biggest mistake is importing SummaryWriter from the wrong place or through an unexpected alias and assuming the issue is deep inside PyTorch.
Another issue is missing the tensorboard package even though torch itself is installed correctly.
Developers also often overlook local filename shadowing. A stray torch.py or tensorboard.py file in the project can break imports in confusing ways.
Summary
- Import
SummaryWriterfromtorch.utils.tensorboard. - Confirm both
torchandtensorboardare installed in the active environment. - Check for local files that shadow package imports.
- Verify the PyTorch version if the API seems missing.
- Use a small smoke test plus
tensorboard --logdirto confirm the setup works end to end.

