Python Docs
Python Importing Modules — Full Cheat Sheet
Importing a Module
After creating a Python module, you can include it in another program using the import statement.
import mymodule
mymodule.greeting("Jonathan")Always call functions using:
module_name.function_name
Creating an Alias (as keyword)
The as keyword lets you rename a module at import time. Helpful when module names are long.
import mymodule as mx
a = mx.person1["age"]
print(a)Import Specific Items
Using the from keyword, you can import only what you need.
from mymodule import person1
print(person1["age"])After importing specific items, you can use them directly without writing the module name.
Importing Built-in Modules
Python includes many useful built-in modules such as platform, math, random, os, etc.
import platform
x = platform.system()
print(x)Using the dir() Function
The dir() function lists all functions, classes, and variables inside a module.
import platform
x = dir(platform)
print(x)You can use dir() on any module — built-in or user-defined.
Why Import Modules?
- Reuse code across files
- Organize programs better
- Access Python’s built-in functionality
- Avoid repeating functions unnecessarily