Link Search Menu Expand Document

Imported Functions

Table of contents

  1. Description
  2. Import Statement
  3. import module_name
  4. from module_name import *
  5. from module_name import function1, function2

Description

You can access functions written in other python files (“modules”) by importing the file into your own program. You import that module into your python file with an import statement.

Import Statement

The import statement(s) are always the first line(s) of code in your program (after your docstring). No other code should be written before them. There are three different ways to import functions from modules:

  1. import module_name
  2. from module_name import *
  3. from module_name import function1, function2

import module_name

  • Creates a reference to the module only.
  • Function names in your program must be preceded by the module name then the dot operator.
  • You can call any function inside the module.
  • This method reduces the readability of your code.

from module_name import *

  • Creates references to all functions in a module
  • Function names in your program do not need to be preceded by the module name.
  • You can call any function inside the module.
  • This method clutters the namespace.

from module_name import function1, function2

  • Creates references only to the specific function(s) you list.
  • Function names in your program do not need to be preceded by the module name.
  • You can only call the functions you list in the import statement.
  • This method is best practice.