The import Statement
How It Works First, we import the sys module using the import statement. Basically, this translates to us telling Python that we want to use this module. The sys module contains functionality related to the Python interpreter and its environment i.e. the sys tem.
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
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.
Module import: import sys tells Python that the program wants to use the sys module.
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.
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.
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 |
Mistakes with Imports
Treating import sys as if it directly imports only argv.
These two forms request different things.
Fix:
Use import sys when the intended module is sys; use from sys import argv only when direct importing of argv is specifically wanted.Assuming that the sys module is unrelated to Python itself.
The source identifies sys as containing functionality related to the Python interpreter and its environment.
Fix:
Remember that sys is associated with the interpreter and its environment.Choosing direct importing without considering name clashes or readability.
The source cautions that this form can be less desirable because the program may encounter name clashes and become less readable.
Fix:
Generally 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
- The import statement tells Python that a program wants to use a module.
- When Python executes import sys, it looks for the built-in sys module and makes it available to the program.
- The sys module contains functionality related to the Python interpreter and its environment.
- from sys import argv directly imports argv, but the source generally recommends using import instead.
- Using import generally helps avoid name clashes and makes a program more readable.
Key Takeaways
- 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.