Skip to main content

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:
  1. Where am I? - What folder is my Python script in?
  2. Where do I want to go? - What file or module do I need?
Then navigate:
  • 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.
Try this - it shows you exactly where Python is looking for files.

Finding your files

Let’s say you have this structure:
If you run 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
Python modules (importing code):
  • Use import statements
  • 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 list includes:
  1. The folder containing your script
  2. Python’s built-in library folders
  3. Installed packages

Adding your own folders

Sometimes you need Python to look in additional places:
Common example - importing from a parent folder:

Common mistakes

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.
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.
Remember: regular files use paths with slashes, Python modules use dots.
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.
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
The mental model: Know where you are, know where you want to go, then navigate with / 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