What is a module search path in Python?

Experience Level: Junior
Tags: Python

Answer

When the Python interpreter executed the import statement, it tries to search for the module in search path.

In the example below, the interpreter sees a module basic_math is to be imported, it searches for the file name basic_math.py in the search path.

It looks into the following folders:

  • The folder from which the input script was run (or the current folder if the interpreted is executed interactively)
  • The list of folders from PYTHONPATH environment variable (if it is defined)
  • An installation-dependent list of folders configured at the time Python is installed

Example:

File main.py

import basic_math

File basic_math.py

def add(x, y):
  return x+y

Python for beginners
Python for beginners

Are you learning Python ? Try our test we designed to help you progress faster.

Test yourself