Skip to main content
Path errors are probably the most frustrating thing beginners encounter in Python. You write code that works perfectly in one situation and then a FileNotFoundError or ModuleNotFoundError appears the moment you move a file or run your script from a different folder. The good news is that once you understand two simple questions — where am I? and where do I want to go? — almost every path problem becomes straightforward to diagnose and fix. Don’t worry if parts of this page don’t click immediately; bookmark it and come back whenever you run into a path issue.

The mental model

When working with multiple files, always ask yourself two things:
  1. Where am I? — What folder is my Python script running from?
  2. Where do I want to go? — What file or module do I need?
Then navigate accordingly:
  • Into subfolders — use / for files, . for module imports
  • Up to the parent — use ../ for files, add to sys.path for imports
  • Same folder — just use the name directly

The current working directory

Python uses a “current working directory” as the starting point for all relative paths. Find out where that is:
Run this whenever you’re confused about why a path isn’t working — it tells you exactly where Python is looking.

Finding files in your project

Given this structure:
When you run script.py:

Files vs modules — an important difference

Python treats regular data files and Python source files differently when you try to access them: Regular files (CSV, TXT, JSON) — use open() with an exact path:
Python modules (importing code) — use import statements with dots, not slashes:

How Python finds modules

When you write import something, Python searches a list of directories stored in sys.path:
The search order is:
  1. The folder containing the script you ran
  2. Python’s built-in standard library folders
  3. Installed third-party packages in site-packages

Absolute vs relative imports

When working inside a package, you can import modules in two ways:
Always prefer absolute imports. They’re clearer, less fragile, and won’t break if you move a file. Relative imports also cause a confusing error if you run the file directly as a script:ImportError: attempted relative import with no known parent package

Package initialization: __init__.py and __all__

A directory becomes a Python package when it contains an __init__.py file. This file runs automatically when the package is imported. You can control what gets exposed publicly using __all__:

The __name__ == "__main__" pattern

When Python runs a file directly, it sets the special variable __name__ to "__main__". When that same file is imported by another script, __name__ is set to the module’s actual name instead. Use this to write code that only runs when the file is executed directly:

Running modules and fixing import errors

The scenario

Inside utils.py: from mypackage.helper import greet

The mistake

If you navigate into mypackage/ and run python utils.py, Python crashes with:
Python adds the directory of the executed script to sys.path. It sees mypackage/ — not myproject/ — so it doesn’t know mypackage exists one level up.

The solutions

1

Solution 1 — Run as a module with -m (Recommended)

Always run from the project root using the -m flag:
This preserves the package hierarchy and configures sys.path correctly.
2

Solution 2 — Set PYTHONPATH

Tell Python where the project root is:

Adding folders to Python’s search path manually

Sometimes you need Python to look in an extra location:

Common mistakes

Python can’t find your data file. The path is wrong or you’re running from an unexpected directory.
Fix: Use the correct relative path from os.getcwd(), or use an absolute path.
Python can’t find the module you’re importing.
Fix: Make sure the module’s directory is in sys.path, or use the -m flag to run from the project root.
Fix: Navigate to the correct directory in your terminal, or use the VS Code Play button (it always runs from the file’s directory).
Always use forward slashes in path strings — they work everywhere.

Keep it simple

Everyone hits path confusion at first. For now:
  • Keep related files in the same folder whenever possible
  • Use the VS Code Play button — it’s predictable
  • When confused, print os.getcwd() immediately to see where Python is looking
  • Run scripts from the project root using python -m for anything involving packages

Organizing code

Split your code into reusable functions and files