Python paths
Understanding how Python finds files
Python needs to know where to find the files you tell it to use, and understanding how it does this is a common hurdle for new programmers.
The most confusing part of Python
Many new Python developers struggle with "paths." This is how your program finds other files it needs, like code you've written or libraries you've installed.
It can be frustrating when Python says a module isn't found, especially when you know it's right there. Let's clear up the confusion.
The mental model
Think of Python as having a list of places it checks whenever you ask it to import something. It goes through this list, one by one, looking for the file.
If it finds the file in any of those places, great! If it goes through the entire list and doesn't find it, you get an ImportError.
The simple rule
The simplest way to ensure Python finds your files is to keep them all in the same directory, especially when you're starting out.
my_project/
├── main.py
└── helper.pyIn main.py, you can import helper directly.
Finding your files
When you run a Python script, the directory where that script lives is automatically the first place Python looks for other files.
cd my_project
python main.pyBecause you ran main.py from within the my_project directory, Python knows to look in my_project first.
Files vs modules - key difference
A "file" is just a .py document. A "module" is a .py file that Python can import.
When you import my_module, Python looks for a file named my_module.py. If it finds a directory named my_module that contains an __init__.py file, that directory also counts as a module (a "package").
How Python finds modules
Python uses a list of directories called the "Python Path" (or sys.path) to find modules. You can see what's in it.
import sys
print(sys.path)This list typically includes your current directory, standard library locations, and site-packages for installed third-party libraries.
Adding your own folders
For bigger projects, you might want to organize code into separate folders outside your main script's directory. There are a few ways to tell Python about these folders.
The most common and recommended way for small projects is to place an empty __init__.py file inside any folder you want Python to treat as a package. Then you can import from it relative to your project's root.
my_project/
├── main.py
└── utils/
└── __init__.py
└── string_helpers.py# in main.py
from utils import string_helpers
print(string_helpers.capitalize_word("hello"))Common mistakes
- Running a file as a script when it's supposed to be imported as a module.
- Not having an
__init__.pyfile in a directory you want to treat as a package. - Trying to import code that is located in a different directory without correctly setting up your project or
sys.path.
Keep it simple
For now, try to keep your Python files in the same directory, or use the __init__.py trick for simple subfolders. This covers most beginner scenarios.
As your projects grow, you'll naturally learn more advanced ways to manage module imports, often with tools that handle the path setup for you automatically.