MCS-201 · Unit 12

Modules and Packages

Section 3 of 6

Block 3 · Unit 12Modules and Packages8 min · Section 3 of 6

12.3Modules versus Scripts

Covers IGNOU MCS-201 §12.4

Every .py file can be used in two ways: run directly as a program, or imported as a module. The difference lies in how it is used, not in how it is written, and Python tells the file which way it is being used.

Two roles of a .py file

ScriptModule
PurposeA program that performs a task when runA library of definitions for other files
How it is usedpython report.py in a terminal, or Run in an IDEimport report from another file
Value of __name__"__main__"The module’s name, e.g. "report"
Typical contentsStatements that do the work, input and outputFunctions, classes and constants

When you run a script from a terminal, the path is relative to the current folder: run python report.py in the folder that contains it, or give its path, as in python projects/report.py. python -m module_name runs a module found on sys.path as a script.

The main guard

Python sets the variable __name__ in every module. Code placed under the main guard runs only when the file is executed directly:

The main guard
if __name__ == "__main__":
    statements that run only when the file is executed as a script

Key takeaways

  • The same .py file can be a script (run) or a module (imported).
  • __name__ is "__main__" when run, the module name when imported.
  • Put script-only code under if __name__ == "__main__":.
  • Run scripts with python file.py, or python -m module for a module on the path.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is __name__ inside tools.py when another file runs import tools?

Show answer

Answer: B. "tools"

An imported module’s __name__ is its module name; only the file being run gets "__main__".

Question 2 · multiple choice

Why is the main guard used?

Show answer

Answer: C. To run some code only when the file is executed directly

Guarded code is skipped during import, so the module’s functions can be reused without side effects.

Question 3 · multiple choice

calc.py contains the code below. What is printed when another file runs import calc?

Python · calc.py
print("A")
if __name__ == "__main__":
    print("B")
print("C")
Show answer

Answer: C. A C

Top-level statements run on import (A and C); the guarded B runs only when calc.py is the main program.

Question 4 · multiple choice

Which command runs a module that is on sys.path as a script?

Show answer

Answer: B. python -m module

The -m option searches sys.path for the module and runs it with __name__ set to "__main__".

Question 5 · multiple choice

Which line is a correctly written main guard?

Show answer

Answer: D. if __name__ == "__main__":

It needs the comparison operator ==, double underscores on both names, and quotes around "__main__".

Question 6 · short answer

Explain what happens, step by step, when use_temperature.py imports temperature.py from the example above.

Show worked answer

Python finds temperature.py on sys.path, compiles it and runs its top-level code once with __name__ set to "temperature". The def creates the function c_to_f, and the top-level print shows “temperature.py loaded, name = temperature”. The test __name__ == "__main__" is false, so the demonstration block is skipped. Control returns to use_temperature.py, which calls temperature.c_to_f(25) and prints 77.0.

↑ ↓ move↵ openTab switch type