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 places. You can see these places:- The folder containing your script
- Python’s built-in library folders
- Installed packages
Adding your own folders
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.
Working with files
Apply these concepts to read and write files