Member-only story
8 Python Import Tricks You’ll Wish You Knew Sooner
2 min readOct 8, 2024
1. Use Aliases to Shorten Module Names
When dealing with long module names, use aliases to shorten them for cleaner code:
import numpy as np
import pandas as pd
This reduces repetitive typing and keeps your code more readable.
2. Selective Imports to Save Memory
Instead of importing the entire module, selectively import only what you need to optimize memory usage:
from math import sqrt, pi
This loads only the functions sqrt and pi, avoiding the overhead of the whole module.
3. Import Multiple Modules in One Line
For small scripts or to keep imports tidy, import multiple modules on a single line:
import os, sys, json
4. Relative Imports for Better Package Management
In large projects, use relative imports to make code modular and avoid name clashes:
from .subpackage import module