Python
ImportError
ModuleNotFoundError
troubleshooting
programming

ImportError No module named 'nets'

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

ImportError: No module named 'nets' usually means Python cannot find a package called nets on the current import path. In practice, that often happens because nets is expected to be a local project package rather than a separately installed library, especially in older TensorFlow Slim or research-model codebases.

Start by checking your project layout

Suppose your project looks like this:

text
1project/
2  main.py
3  nets/
4    __init__.py
5    resnet.py

Then an import such as:

python
from nets import resnet

works only if Python is running with project/ on sys.path. The simplest reliable way is usually to run from the project root:

bash
python main.py

Or, if the code is part of a package, run it as a module:

bash
python -m project.main

Many import errors happen not because the code is wrong, but because the file was launched from the wrong working directory or with the wrong module path.

Inspect what Python is actually searching

You can print the import search path directly:

python
1import sys
2
3for path in sys.path:
4    print(path)

If the directory that contains nets/ is not present, Python has no way to resolve import nets.

That is the core rule behind this error. Python imports package names from directories on sys.path, not from wishful thinking about where the code "should" be.

If nets belongs to an external repo, install or expose it properly

In older machine learning projects, nets often comes from repository code rather than a pip package. For example, TensorFlow Slim examples historically expected a repository layout where nets/ lived beside other project folders.

In that situation, the right fix is usually one of these:

  • clone or restore the full repository structure
  • run the script from the repository root
  • install the project in editable mode

Editable install example:

bash
pip install -e .

That makes the package importable from the environment without relying on ad hoc working-directory behavior.

Avoid the sys.path.append(...) reflex

You can patch imports by modifying sys.path:

python
1import sys
2from pathlib import Path
3
4sys.path.append(str(Path(__file__).resolve().parent))
5from nets import resnet

This sometimes helps during quick experiments, but it is usually not the best long-term fix. It hides the packaging problem instead of solving it.

A cleaner project structure is better:

text
1project/
2  pyproject.toml
3  src/
4    myapp/
5      __init__.py
6      nets/
7        __init__.py
8        resnet.py

Then import through the actual package:

python
from myapp.nets import resnet

Confirm it is really the package you think it is

One more subtle issue: sometimes the code was copied from a tutorial that assumes a repository-specific nets package exists, but your local environment only has TensorFlow installed, not the companion project files. In that case, pip install nets is usually the wrong move because the missing package may never have been intended as a standalone public dependency.

So before installing random packages, answer this question:

Where was nets supposed to come from?

That answer determines whether you need:

  • the missing repository folder
  • a fixed working directory
  • a proper package install
  • a corrected import path

Common Pitfalls

The biggest mistake is assuming every missing import can be fixed with pip install <name>. Many missing modules are project-local packages, not published libraries.

Another common issue is running a script from inside a subdirectory rather than the project root, which changes sys.path in ways the original project did not expect.

People also patch sys.path permanently instead of packaging the project properly. That can make local scripts work while leaving tests, tooling, or deployments fragile.

Finally, check for missing __init__.py files in older package layouts. Modern Python supports namespace packages, but many older projects still assume classic package markers.

Summary

  • 'No module named 'nets' means Python cannot see a package named nets on its import path.'
  • First verify the project layout and the directory from which the script is running.
  • In many ML codebases, nets is a local repository package rather than a standalone pip dependency.
  • Prefer proper packaging or running the module from the project root over patching sys.path.
  • Do not install random packages until you know where the missing nets package was supposed to come from.

Related reading
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
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.