"import" Statement to Load Modules

This section provides a quick introduction of 'import' statement, which loads modules, creates objects, assigns them to variables.

What Is "import" Statement? An "import" statement is a simple statement that imports modules of pre-written Python code into memory.

"import" statements have several forms:

1. Import Top Module - It loads the given module into memory and create a "module" object using one of these syntaxes:

import module_name as variable_name
import module_name

The "module" object created will represent the top module. Its reference will be assigned to the variable if provided. If no variable name is provided, the module name will be used as the variable name. For example:

# import "sys" module and assign it to "s"
>>> import sys as s
>>> type(s)
<class 'module'>

>>> s.version
'3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) ...'

# delete variable "s"
# the referenced "module" object may stay in memory
>>> del s

# import "sys" module and assign it to "sys"
>>> import sys
>>> type(sys)
<class 'module'>

>>> sys.version
'3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) ...'

>>> del sys

2. Import Sub-Module - It loads the sub-module into memory and create a "module" object using one of these syntaxes:

import top_module.sub_module as variable_name
import top_module.sub_module

If a variable name is provided, a "module" object will be created for the sub-module. Its reference will be assigned to the variable. For example:

>>> import dateutil.parser as p
>>> p
<module 'dateutil.parser' from '.../dateutil/parser/__init__.py>

>>> dateutil
NameError: name 'dateutil' is not defined

>>> dateutil.parser
NameError: name 'dateutil' is not defined

>>> del p

However, if no variable name is provided, importing a sub-module behaves like importing the top module. For example:

>>> import dateutil.parser
>>> dateutil
<module 'dateutil.parser' from '.../dateutil/__init__.py>

>>> dateutil.parser
<module 'dateutil.parser' from '.../dateutil/parser/__init__.py>

>>> del dateutil

3. Import Module Member - It loads the given medule into memory and create an object for the given member using one of these syntaxes:

from top_module import member as variable_name
from top_module import member

from top_module.sub_module import member as variable_name
from top_module.sub_module import member

The given member of module can be a sub-module, a class, a method, or an attribute. When an object of the given member is created, its reference will be assigned to the variable if provided. If no variable name is provided, the member name will be used as the variable name. For example:

# import a module member
>>> from dateutil import parser as p
>>> type(p)
<class 'module'>

# import a class member
>>> from dateutil.parser import parserinfo as info
>>> type(info)
<class 'type'>

# import a method member
>>> from dateutil.parser import isoparse as iso
>>> type(iso)
<class 'method'>

# import an attribute member
>>> from sys import version as v
>>> v
'3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) ...'

4. List Import - It loads multiple top modules, sub-modules, or members in a single "import" statement using one of these syntaxes:

import top_module_1[.sub_module_1] [as var_1], top_module_2 [as var_2], ...
from top_module.[sub_module] import mem_1 [as var_1], mem_2 [as var_2], ...

For example:

>>> from dateutil import parser as p, zoneinfo as z

>>> p.parse('2022-04-01')
datetime.datetime(2022, 4, 1, 0, 0)

>>> z.gettz('Europe/Paris')
tzfile('Europe/Paris')

5. Star Import - It loads the module into memory and create an object for each member of the given module using one of this syntax:

from top_module.[sub_module] import *

The object created for each member will be assigned to the member name as the variable name. The start import statement is actually pretty dangerous. If an existing variable has the same name as a loaded module member, its object reference will be replaced for the loaded member object. For example:

# variable refers to my version string
>>> version = "v2.13, 2022"
>>> version
'v2.13, 2022'

# reference in "version" gets replaced by sys.version
>>> from sys import *
>>> version
'3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) ...]'

Table of Contents

 About This Book

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

 Built-in Data Types

 Variables, Operations and Expressions

Statements - Execution Units

 What Is Statement

 "pass" Statement - Do Nothing Statement

 Expression Statement - One Expression Only

 "=" Statement - Assignment Statement

 "del" Statement - Delete Statement

"import" Statement to Load Modules

 "if" Statement for Conditional Execution

 "while" Statement for Execution Loop

 "for" Statement for Iterative Execution

 "try" Statement to Catch Execution

 "with" Statement for Context Manager

 "match" Statement for Pattern Match

 Function Statement and Function Call

 Iterators and Generators

 List, Set and Dictionary Comprehensions

 Classes and Instances

 Modules and Module Files

 Packages and Package Directories

 "sys" and "os" Modules

 "pathlib" - Object-Oriented Filesystem Paths

 "pip" - Package Installer for Python

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab

 References

 Full Version in PDF/EPUB