Python project files use extensions to indicate their purpose. Understanding the main ones will prevent common editing and execution mistakes.
Understanding the Concept
A .py file holds Python source. A .ipynb file is a Jupyter notebook. A .pyi file is a type stub that describes interfaces for type-checking tools. A project might also include .txt data, .json configuration, or a pyproject.toml file containing project metadata. Files named __init__.py have a special role in ordinary Python packages, which you will learn later. The existence of a file extension does not make its contents valid.
When and Why You Would Use This
Start with a simple folder containing main.py and perhaps a data directory. Add more folders only when needed. Do not mix your virtual environment internals with source code.
Practical Example
my_project/
main.py
data/
sample.txt
pyproject.toml # Optional project metadata
Understanding the Example
This is a sample directory tree rather than executable code. The project entry point for a tiny program might be main.py. A real application can have many modules and packages instead.
Try It Yourself
Create main.py and a data directory. Identify which files you would write yourself and which might be generated by tooling.
Common Beginner Mistakes
- Renaming a text file from .txt to .py and assuming this fixes invalid Python syntax.
- Editing bytecode cache files instead of original Python source.
- Placing every file in the same directory after the project grows.
Key Takeaways
- Use .py for Python source and .ipynb for notebooks.
- Project layout should reflect the work being organized.
- Extensions help tools identify content but do not validate it.