Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Why importing star is a bad idea in python
Importing all methods from a module using from module import * (star import) is considered a bad practice in Python for several important reasons.
Problems with Star Imports
Star imports create the following issues ?
- Namespace pollution − All module functions are imported into the current namespace
- Name conflicts − Imported functions can override your local functions
- Unclear code origin − Difficult to identify which module a function belongs to
- IDE limitations − Code editors cannot provide proper autocompletion and error detection
Creating the Sample Module
First, let's create a simple module file called sample.py ?
# sample.py file
def add(a, b):
return a + b
def multiply(a, b):
return a * b
Example 1: Name Conflict with Star Import
When using star import, local functions can conflict with imported ones ?
# Simulating: from sample import *
def add(a, b): # This would be imported from sample
return a + b
# Local function with same name
def add(*nums):
return sum(nums)
# Which add() function will be called?
result = add(1, 2, 3, 4, 5)
print(f"Result: {result}")
print(f"Function used: Local add() with *args")
Result: 15 Function used: Local add() with *args
Example 2: Proper Import Method
Using specific imports or module imports makes code clearer and prevents conflicts ?
# Simulating: import sample
class sample:
@staticmethod
def add(a, b):
return a + b
# Local function with same name
def add(*nums):
return sum(nums)
# Now we can use both functions clearly
local_result = add(1, 2, 3, 4, 5)
module_result = sample.add(1, 2)
print(f"Local add(): {local_result}")
print(f"Module add(): {module_result}")
Local add(): 15 Module add(): 3
Better Import Practices
Here are recommended alternatives to star imports ?
# Method 1: Import specific functions
# from sample import add, multiply
# Method 2: Import module with alias
# import sample as sp
# Method 3: Import module normally
# import sample
# Example showing clear namespace
import math
def sqrt(x):
return f"Custom sqrt: {x ** 0.5}"
# Clear distinction between functions
custom_result = sqrt(16)
math_result = math.sqrt(16)
print(f"Custom function: {custom_result}")
print(f"Math module: {math_result}")
Custom function: Custom sqrt: 4.0 Math module: 4.0
When Star Imports Might Be Acceptable
Star imports are occasionally acceptable in these limited cases ?
- Interactive sessions − Quick testing in Python shell
-
Well-designed modules − Modules explicitly designed for star import (like
tkinter.constants) - Internal modules − Within the same package where you control all names
Conclusion
Avoid star imports in production code to maintain clean namespaces and code clarity. Use specific imports or module imports instead to prevent name conflicts and make your code more maintainable and readable.
