Module Aliases and Selective Imports
Overview
Instead of importing everything from a module, you can import only what you need. This section explains how to simplify imports using aliases (like import numpy as np) and targeted imports for faster, cleaner code, making your imports more efficient and readable.
What You Will Learn in This Lesson
By the end of this lesson, you will know:
- Using aliases: Learn how to rename modules with
asfor shorter names. - Selective imports: Understand how to import only specific functions or classes.
- Import best practices: Discover when to use aliases vs full imports.
- Common patterns: See standard aliases used in the Python community.
Why This Matters
Writing numpy.array() repeatedly can be tedious. Using import numpy as np lets you write np.array() instead - much shorter! Selective imports let you bring in only what you need, making your code cleaner and faster. These techniques are widely used in professional Python code!
Step 1: Using Module Aliases
You can give a module a shorter, more convenient name using as:
# Long module name
import numpy
result = numpy.array([1, 2, 3])
# Short alias
import numpy as np
result = np.array([1, 2, 3]) # Much shorter!
Import with Alias
Use import module_name as alias to give a module a shorter name. The alias becomes the new name you use in your code.
Use the Alias
After creating an alias, use it instead of the full module name. This makes your code shorter and easier to read!
Common Aliases
Some modules have standard aliases used throughout the Python community (like np for numpy, pd for pandas).
Key Concept: Aliases are like nicknames for modules. Instead of saying the full name every time, you can use a shorter nickname. This saves typing and makes code more readable!
Mini Practice #1: Using Aliases
Try It YourselfSimulate using module aliases:
What happened? By using import math as m, you created a shorter alias for the math module. Instead of writing math.pi, you can write m.pi. This is especially useful for modules with long names!
Step 2: Selective Imports
You can import only specific functions or classes from a module:
# Import everything from math (less efficient)
import math
result = math.sqrt(16)
# Import only what you need (more efficient)
from math import sqrt, pi
result = sqrt(16) # No module prefix needed!
From...Import
Use from module import function1, function2 to import only specific functions or classes.
Direct Use
After selective import, you can use the functions directly without the module prefix. This makes code shorter!
Efficiency
Selective imports are slightly more efficient because Python only loads what you need, not the entire module.
When to Use Each Method
- Use
import math: When you need many functions from a module or want to avoid naming conflicts. - Use
from math import sqrt: When you only need one or two specific functions. - Use
import math as m: When the module name is long and you'll use it frequently.
Mini Practice #2: Selective Import
Try It YourselfTry selective import:
What happened? By using from random import randint, choice, you imported only those two functions. You can use them directly without writing random.randint() or random.choice(). This makes your code cleaner and easier to read!
Step 3: Common Import Patterns
Here are standard import patterns used in Python:
NumPy
Standard alias: np
import numpy as np
arr = np.array([1, 2, 3])
Pandas
Standard alias: pd
import pandas as pd
df = pd.DataFrame(...)
Matplotlib
Common pattern:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
Selective Import
For single functions:
from datetime import datetime
now = datetime.now()
Step 4: Combining Techniques
You can combine aliases with selective imports:
# Alias for module
import numpy as np
# Selective import with alias
from datetime import datetime as dt
# Use both
arr = np.array([1, 2, 3])
now = dt.now()
Remember: Choose import styles that make your code clearest. If you're using a module frequently, an alias might help. If you only need one function, selective import is cleaner. Mix and match based on what makes your code most readable!
End-of-Lesson Exercises
Exercise 1: Use Module Alias
Import the math module with the alias m. Then use it to calculate the square root of 25 and print the result.
Use import math as m, then m.sqrt(25).
Exercise 2: Selective Import
Use from random import randint to import only the randint function. Then generate a random number between 1 and 100 and print it.
Use from random import randint, then randint(1, 100).