Concepts / The import Statement

The import Statement

The import statement is how you tell Python that your program wants to use a module.

  • Programming

Why Modules Matter

A Python program can use functionality provided by a module. The import statement is how you tell Python that your program wants to use a module. This article follows what happens when the program imports the sys module.

Tracing import sys

What do you think happens?

When Python executes import sys, what is the main result?

  • Python is told that the program wants to use the sys module.

  • Python creates a new operating system.

  • Python directly imports only the argv variable.

Reveal answer
Answer:

Python is told that the program wants to use the sys module.

The import statement communicates the program's intention to use a module. For import sys, Python looks for the sys module and makes it available for use.

Python programimport sysrequests the sys modulesys moduleavailable to the programexecutesmakes available
What changes when Python executes import sys, and how does the program gain access to the module?

What Python Looks For

When Python executes import sys, it looks for the sys module. In this case, sys is one of Python's built-in modules, so Python knows where to find it. The sys module contains functionality related to the Python interpreter and its environment, meaning the system in which the interpreter operates.

Key point

The import statement does not describe the contents of every module. It tells Python which module the program wants to use. In the case of import sys, the requested module is sys.

Choosing an Import Form

Worked example

Importing sys or importing argv

Compare the two ways of requesting functionality mentioned in the source material.

  1. 1
    Module import

    import sys tells Python that the program wants to use the sys module.

  2. 2
    Direct import

    from sys import argv directly imports the argv variable from sys, so the program can avoid typing sys. every time it refers to argv.

  3. 3
    Practical choice

    The source recommends generally avoiding the direct-import form and using the import statement instead because this helps avoid name clashes and makes the program more readable.

Result

import sys is generally the preferred form in this comparison, while from sys import argv is available when direct access to argv is wanted.

FormWhat it requestsPractical note
import sysThe sys moduleGenerally preferred because it helps avoid name clashes and supports readability
from sys import argvThe argv variable from sysAvoids typing sys. when referring to argv, but is generally discouraged in favor of import

The two import forms described in the source material

Mistakes with Imports

  • Common mistake

    Treating import sys as if it directly imports only argv.

    What it looks like

    The statement import sys requests the sys module, whereas from sys import argv requests the argv variable from that module.

    Why it fails

    These two forms request different things.

    Do this instead

    Use import sys when the intended module is sys; use from sys import argv only when direct importing of argv is specifically wanted.

  • Common mistake

    Assuming that the sys module is unrelated to Python itself.

    What it looks like

    Describing sys as an arbitrary collection of application data.

    Why it fails

    The source identifies sys as containing functionality related to the Python interpreter and its environment.

    Do this instead

    Remember that sys is associated with the interpreter and its environment.

  • Common mistake

    Choosing direct importing without considering name clashes or readability.

    What it looks like

    Using from sys import argv simply to shorten references.

    Why it fails

    The source cautions that this form can be less desirable because the program may encounter name clashes and become less readable.

    Do this instead

    Generally prefer import sys unless there is a reason to use the direct-import form.

Practice the Decision

EASY

You want your program to use functionality from the sys module. Decide which import form is generally recommended by the source, then explain why the alternative might be chosen and what trade-off it introduces.

Hints
  • Distinguish importing the module from importing argv directly.
  • Consider name clashes and readability.
  • State what Python is being told when the import statement executes.

Key Takeaways

  1. 1
    The import statement tells Python that a program wants to use a module.
  2. 2
    When Python executes import sys, it looks for the built-in sys module and makes it available to the program.
  3. 3
    The sys module contains functionality related to the Python interpreter and its environment.
  4. 4
    from sys import argv directly imports argv, but the source generally recommends using import instead.
  5. 5
    Using import generally helps avoid name clashes and makes a program more readable.

At a glance

  • The import statement communicates that a program wants to use a module.
  • Python knows where to find sys because it is a built-in module.
  • The sys module concerns the Python interpreter and its environment.
  • from sys import argv is an alternative that directly imports argv.
  • The general recommendation is to prefer import because it helps avoid name clashes and improves readability.

Related concepts

More concepts starting with T