Understanding Python import Statements: Importing Modules and Specific Members
This article explains the two primary Python import syntaxes—importing an entire module versus importing selected members—detailing their differences, usage of aliases, multiple module imports, and includes runnable code examples that illustrate how each form affects name resolution.
Python provides two main forms of the import statement: import module_name [as alias], ... to import an entire module, and from module_name import member_name [as alias], ... to import specific members.
The first form makes all module members accessible via the module name (or its alias), while the second allows direct use of selected members without a prefix, though from module_name import * is discouraged.
Examples demonstrate importing the whole sys module and accessing sys.argv[0] , assigning an alias with import sys as s , and importing multiple modules using import sys, os to access os.sep .
Running these snippets shows the expected output, illustrating how the import style influences name resolution in Python programs.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.