Every Python program you write starts as a text file with a .py extension. This file is called a source file, and it contains Python statements that the interpreter reads and executes. Up until now, you have probably been typing code into the interactive interpreter or running single files with a few lines. As your programs grow, understanding how source files work as both scripts and modules becomes essential for organizing your code.
A single .py file can serve two different roles depending on how you use it. When you pass it to the Python interpreter as a command-line argument, it runs as a script: Python executes every statement from top to bottom and then exits. When you use the import statement to load it from another file, it acts as a module: Python executes the file once to define its functions and variables, then makes those definitions available to the importing code. The same file can handle both roles gracefully with a simple built-in pattern.
Running a file as a script
The simplest way to execute Python code is to put it in a file and run it with the python command. You create a file, give it a name ending in .py, write your statements, and then run it from the terminal. Python reads the file, compiles it to bytecode, and executes the statements in order from top to bottom. When the last statement finishes, the program exits.
Here is a small script that prints a greeting and a calculation:
name = "Maya"
score = 95
print("Hello, " + name)
print("Your score is", score)Save these lines in a file called greet.py and run it with python greet.py. Python executes the three statements in sequence and shows the output. The file does not need any special header or boilerplate. Every top-level statement in the file runs exactly once, in the order it appears.
Using a file as a module
The same .py file can also be loaded by another Python file using the import statement. When you import a file, Python executes it once to define its contents, then makes those definitions available through the module name. This is how you reuse functions and variables across multiple files without copying and pasting code.
When Python imports a file, it sets a special built-in variable called name to the name of the module. When you run a file directly as a script, Python sets name to the string 'main' instead. This difference lets you write code that behaves one way when run as a script and another way when imported as a module. The standard pattern looks like this:
def greet(name):
print("Hello, " + name)
if __name__ == "__main__":
greet("Maya")When this file runs as a script, name equals 'main', the condition is true, and greet is called. When another file imports this one, name equals the module name, the condition is false, and only the function definition is executed. The greet function becomes available to the importer, but the call at the bottom does not run. This pattern is used in nearly every Python file that is designed to be both reusable and runnable.
How Python finds your files
When you import a module, Python searches for it in a specific order. First, it checks if the name belongs to a built-in module compiled into the interpreter. If not, it searches for a .py file with the matching name in a list of directories stored in sys.path. This list starts with the directory containing the script you ran, followed by directories listed in the PYTHONPATH environment variable, and finally the standard library and site-packages directories where third-party packages are installed.
For simple projects where all your .py files are in the same directory, importing just works. When you start organizing code into subdirectories and packages, you will need to understand the search path in more detail. For now, keeping all your source files in one folder is the simplest way to practice importing and reusing code. If you have read about Python identifiers and naming rules, you already know how to name your .py files correctly: use lowercase letters, underscores for spaces, and avoid names that clash with Python keywords or standard library modules.
A mental model for source files
Think of each .py file as having two possible identities. As a script, it is the entry point of a program: it runs once, does its job, and exits. As a module, it is a library of definitions: it runs once to set up its contents, then waits to be used by other code. The name check is the switch that lets a file know which identity it is using.
This dual nature is what makes Python projects scalable. You start with a single script in a single file. When it grows too large, you extract reusable functions into separate files and import them back. Those files can later be organized into packages and distributed as libraries. The journey from a one-file script to a multi-file project is natural in Python, and it all starts with understanding how source files work. If you are ready to see these concepts combined into a complete working program, the article on building your first Python console program ties everything together.
Rune AI
Key Insights
- Python source files end in .py and contain statements that execute from top to bottom.
- Running a file as a script sets name to 'main'; importing sets it to the module name.
- The if name == 'main' pattern lets a file work as both a script and an importable module.
- Python searches for modules using sys.path, starting with the current directory.
Frequently Asked Questions
What is the difference between running a Python file and importing it?
Can I run a Python script without the .py extension?
Why does Python create a __pycache__ folder?
Conclusion
Every Python program you write lives in a .py file. Running a file as a script executes its code from top to bottom. Importing a file as a module loads its definitions once and makes them available for reuse. Understanding this dual nature of source files is the foundation for organizing larger programs across multiple files and eventually building your own modules and packages.
More in this topic
Python Keywords Explained
Learn what Python keywords are, which words are reserved, and why you cannot use them as variable or function names.
Getting User Input in Python
Learn how to use Python's input() function to collect information from the user and build interactive programs.
Printing Output in Python
Learn how to use Python's print() function to display messages, format output, and control how text appears on screen.