What are the different ways I can import a module?


#1

Question

What are the different ways I can import a module?

Answer

So far we’ve learned about a few different ways to import modules and the pros and cons associated with them. They are:

  1. import module_name : This is safe because it forces us to write the module name and then the functions we want to use and prevents name overlap.
  2. from module_name import function_name : This method is useful in small programs where you know you won’t have a function with the same name and don’t want to write the module name every time you use it.
  3. from module_name import * : This last approach is generally a bit more risky unless your program is so small that it makes no difference, simply because it can cause name overlap which causes unexpected behavior.

FAQ: Learn Python: Functions - Review: Modules