The most confusing part of Python
Understanding paths is probably the most confusing part when starting with Python. You’ll make mistakes with file paths and imports, and that’s totally fine. That’s how everyone learns. This page gives you a quick introduction. Some parts might not be completely clear right now, but as you practice, it will click. Come back to this page when you run into path issues - it happens to everyone!The mental model
When working with multiple files, always think about two things:- Where am I? - What folder is my Python script in?
- Where do I want to go? - What file or module do I need?
- Going down into subfolders: Use
/for files,.for imports - Going up to parent folders: Use
../for files, add to sys.path for imports - Same folder: Just use the filename
The simple rule
When Python runs, it has a “current working directory” - the folder it’s currently in. All file paths start from there.Finding your files
Let’s say you have this structure:script.py:
Files vs modules - key difference
Python handles regular files and Python modules differently: Regular files (CSV, TXT, JSON):- Use
open()or file-reading functions - Need the exact path from your current directory
- Use forward slashes:
data/sales.csv
- Use
importstatements - Python searches in multiple locations (sys.path)
- Use dots instead of slashes:
folder.module
How Python finds modules
Python looks for modules (other.py files) in specific directories stored in sys.path. You can see these places by running:
- The folder containing the script you ran.
- Python’s built-in standard library folders.
- Installed third-party packages (located in
site-packagesdirectory).
Absolute vs. Relative Imports
Within a package, you can refer to other modules in two ways:- Absolute Import: Uses the full path from the project root.
- Relative Import: Uses leading dots (
.) to refer to modules relative to the current module.
Package Initialization: __init__.py and __all__
When you import from a package, Python runs the package’s __init__.py file. You can define what gets exposed to the public API when a user imports your package using the special __all__ list:
The __name__ == "__main__" Block
When a Python file is run directly, Python sets a special built-in variable __name__ to "__main__". If the file is imported by another script, __name__ is set to the file’s actual module name instead.
We use this block to write code that only executes when the file is run directly, but is ignored when imported as a library:
Running Modules & Troubleshooting Import Errors
When working with local modules and packages, developers frequently run into path-related errors. Here is how to understand and solve them:The Scenario
Suppose you have a project structured like this:utils.py, you have an absolute import: from mypackage.helper import greet.
The Mistake
If you navigate inside themypackage/ folder and run python utils.py, Python will crash with:
ModuleNotFoundError: No module named 'mypackage'.
Why? Because Python adds the directory of the executed script (myproject/mypackage/) to sys.path. It does not know that mypackage exists one folder level up.
The Solutions
Solution 1: Run as a module using the -m flag (Recommended)
Always run your Python scripts from the project root directory (e.g., myproject/) using the -m (module) flag. This preserves the package hierarchy and correctly configures sys.path.
Solution 2: Set the PYTHONPATH Environment Variable
You can manually tell Python where to look for modules by setting the PYTHONPATH environment variable to your project root directory.
- macOS / Linux:
- Windows (PowerShell):
Adding your own folders manually
Sometimes you need Python to look in additional places:Common mistakes
FileNotFoundError: No such file or directory
FileNotFoundError: No such file or directory
This happens when Python can’t find your file. The file path is wrong or you’re running from a different folder than expected.Fix: Use the correct path from your current directory, or use absolute paths.
ModuleNotFoundError: No module named 'X'
ModuleNotFoundError: No module named 'X'
Python can’t find the module you’re trying to import. It’s not in any of the folders Python searches.Fix: Make sure the module is in one of those folders, or add its folder to sys.path.
Mixing up files and modules
Mixing up files and modules
Remember: regular files use paths with slashes, Python modules use dots.
Running from the wrong folder
Running from the wrong folder
Your script works in VS Code but not in terminal? You’re probably in a different folder.Fix: Navigate to the right folder in terminal, or use the VS Code play button.
Using backslashes on Mac/Linux
Using backslashes on Mac/Linux
Windows uses backslashes, but Mac/Linux use forward slashes. Use forward slashes - they work everywhere!
Keep it simple
Remember, everyone struggles with paths at first. For now:- Keep related files in the same folder
- Use the VS Code play button (it’s predictable)
- When confused, print
os.getcwd()to see where you are - Come back to this page when you hit path errors
/ for files. For imports, same folder needs no path, subfolders use ., parent folders need sys.path.
Organizing code
Learn to split code into reusable functions and files