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 directories stored in sys.path. You can see these places by running:
The lookup order includes:
  1. The folder containing the script you ran.
  2. Python’s built-in standard library folders.
  3. Installed third-party packages (located in site-packages directory).

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.
Best Practice: Always prefer Absolute Imports.Absolute imports are much clearer, easier to read, and don’t break if you move a file to a different directory. Furthermore, if you run a file containing relative imports directly as a script (e.g., python mypackage/utils.py), Python will raise: ImportError: attempted relative import with no known parent package.

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:
And inside utils.py, you have an absolute import: from mypackage.helper import greet.

The Mistake

If you navigate inside the mypackage/ 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

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 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.

Organizing code

Learn to split code into reusable functions and files