MCS-201 · Unit 12

Modules and Packages

Section 2 of 6

Block 3 · Unit 12Modules and Packages9 min · Section 2 of 6

12.2The Module Search Path

Covers IGNOU MCS-201 §12.3

import series works when series.py is in the same folder as your program. Move to another folder and the same import fails. This section explains why.

sys.path

When you import a module, Python looks through a list of folders, in order, and uses the first matching file it finds. That list is sys.path:

How sys.path is built (in this order)
1. The folder of the script being run
   (or '' = current directory, in the interactive shell)
2. Folders listed in the PYTHONPATH environment variable, if it is set
3. Installation folders: the standard library and site-packages
   (where pip installs third-party packages)

If no folder contains the module, Python raises ModuleNotFoundError: No module named '...'.

Making your own module importable

MethodHowLasts
Keep the module next to the scriptPut series.py in the program’s folderAlways (simplest)
Append to sys.path in the programsys.path.append("/path/to/folder") before importingOnly for that run
Set PYTHONPATHe.g. export PYTHONPATH=/path/to/folder (Linux/macOS) or System Properties → Environment Variables (Windows)Every run in that environment
Install it as a packagepip install a packaged project (pip install -e . during development)Permanently

Key takeaways

  • sys.path is the ordered list of folders searched on import.
  • It starts with the script’s folder, then PYTHONPATH, then the installation folders.
  • A missing module raises ModuleNotFoundError.
  • Extend the path with sys.path.append, PYTHONPATH, or by installing the code.

Practice questions

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

Question 1 · multiple choice

Which variable holds the list of folders searched for modules?

Show answer

Answer: B. sys.path

sys.path is the search list. os.path is a module of path functions, and sys.modules holds modules already loaded.

Question 2 · multiple choice

Which error is raised when a module cannot be found?

Show answer

Answer: D. ModuleNotFoundError

Import failures for missing modules raise ModuleNotFoundError, a subclass of ImportError.

Question 3 · multiple choice

Which folder is searched first when you run python app.py?

Show answer

Answer: A. The folder containing app.py

The script’s own folder is the first entry of sys.path, which is why modules placed beside the script import easily.

Question 4 · multiple choice

What does this program print?

Python · quiz_path.py
import sys
before = len(sys.path)
sys.path.append("/tmp/my_modules")
print(len(sys.path) - before, sys.path[-1])
Show answer

Answer: C. 1 /tmp/my_modules

sys.path is an ordinary list; append adds one entry at the end, whether or not the folder exists.

Question 5 · multiple choice

How long does a sys.path.append() change last?

Show answer

Answer: B. Until the program ends

sys.path is rebuilt every time Python starts, so changes made at run time are not saved.

Question 6 · short answer

List three ways to make a module stored in D:\mylib importable from any of your programs, and say which lasts only for the current run.

Show worked answer

(1) Add D:\mylib to the PYTHONPATH environment variable, which affects every Python run that sees that variable. (2) Call sys.path.append(r"D:\mylib") at the start of a program before importing, which lasts only for the current run. (3) Package the code and install it with pip, which places it in site-packages, a folder that is always on the path. (Copying the module into the program’s own folder also works, for that program only.)

↑ ↓ move↵ openTab switch type