Why is "import *" bad?

It is recommended to not to use import * in Python.

Can anyone please share the reason for that, so that I can avoid it doing next time?

You’re absolutely right to question the use of import * — it’s generally discouraged in Python for several important reasons:


Problems with import *

  1. Namespace Pollution
  • It imports everything from the module into the current namespace.
  • This can overwrite existing variables or functions without warning.
from math import *
from statistics import *

# Which sqrt() are you calling now?
print(sqrt(9))
  1. Reduced Readability and Maintainability
  • Other readers (and even your future self) can’t tell where a name came from.
  • If you write sqrt(), it’s unclear if it’s from math, numpy, or somewhere else.
  1. Tools and IDEs Can’t Help You
  • Static analysis tools, linters, and autocompletion often can’t determine where functions or classes originated from.
  1. Debugging Becomes Harder
  • If something breaks, it’s more difficult to trace the origin of a name or behavior.
  1. Possible Import Loops or Conflicts
  • Especially in large projects, import * can lead to subtle circular dependencies.

Best Practices Instead

  • Use explicit imports:
from math import sqrt, pi
  • Or import the module:
import math
math.sqrt(16)

This makes your code clearer, safer, and easier to debug or scale.