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:- Where am I? — What folder is my Python script running from?
- Where do I want to go? — What file or module do I need?
- Into subfolders — use
/for files,.for module imports - Up to the parent — use
../for files, add tosys.pathfor 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:Finding files in your project
Given this structure: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) — useopen() with an exact path:
import statements with dots, not slashes:
How Python finds modules
When you writeimport something, Python searches a list of directories stored in sys.path:
- The folder containing the script you ran
- Python’s built-in standard library folders
- Installed third-party packages in
site-packages
Absolute vs relative imports
When working inside a package, you can import modules in two ways: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
utils.py: from mypackage.helper import greet
The mistake
If you navigate intomypackage/ and run python utils.py, Python crashes with:
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 This preserves the package hierarchy and configures
-m flag: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
FileNotFoundError: No such file or directory
FileNotFoundError: No such file or directory
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.ModuleNotFoundError: No module named 'X'
ModuleNotFoundError: No module named 'X'
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.Mixing up files and modules
Mixing up files and modules
Running from the wrong folder
Running from the wrong folder
Backslashes on macOS/Linux
Backslashes on macOS/Linux
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 -mfor anything involving packages
Organizing code
Split your code into reusable functions and files