The import Statement
The import statement is how you tell Python that your program wants to use a module.
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?
Reveal 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.
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.
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
Importing sys or importing argv
Compare the two ways of requesting functionality mentioned in the source material.
- 1Module import
import sys tells Python that the program wants to use the sys module.
- 2Direct 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.
- 3Practical 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.
import sys is generally the preferred form in this comparison, while from sys import argv is available when direct access to argv is wanted.
| Form | What it requests | Practical note |
|---|---|---|
| import sys | The sys module | Generally preferred because it helps avoid name clashes and supports readability |
| from sys import argv | The argv variable from sys | Avoids 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 likeThe statement import sys requests the sys module, whereas from sys import argv requests the argv variable from that module.
Why it failsThese two forms request different things.
Do this insteadUse 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 likeDescribing sys as an arbitrary collection of application data.
Why it failsThe source identifies sys as containing functionality related to the Python interpreter and its environment.
Do this insteadRemember that sys is associated with the interpreter and its environment.
Common mistake Choosing direct importing without considering name clashes or readability.
What it looks likeUsing from sys import argv simply to shorten references.
Why it failsThe source cautions that this form can be less desirable because the program may encounter name clashes and become less readable.
Do this insteadGenerally prefer import sys unless there is a reason to use the direct-import form.
Practice the Decision
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
- 1The import statement tells Python that a program wants to use a module.
- 2When Python executes import sys, it looks for the built-in sys module and makes it available to the program.
- 3The sys module contains functionality related to the Python interpreter and its environment.
- 4from sys import argv directly imports argv, but the source generally recommends using import instead.
- 5Using 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.