Python's syntax allows for code to be significantly shortened by using something called modules. Similar to header files in C++, modules are a storage place for the definitions of functions. They are separated into common uses, such as the time module, which provides functions for time related uses.

Method 1
Method 1 of 2:

Using the from-import instruction

The from-import instruction imports functions from a module and lets you use them like functions from the core Python. You don't see that the functions belong to the module.

  1. 1
    Find the module. Locate the module that you will be importing. A complete list of built in modules can be found here (v2.7) and here (v3.5).
  2. 2
    To import a specific function from a specific module, write:
     from 
     [ 
     module 
     ] 
     import 
     [ 
     function 
     ] 
    
    This will tell the script you are using a specific function from a specific module.
    • For example, to import the randint function from the random module and print a random number using that function, you would write:
       from 
       random 
       import 
       randint 
       print 
       ( 
       randint 
       ( 
       0 
       , 
       5 
       )) 
      
    Advertisement
  3. 3
    Separate multiple functions from the same module with commas (,). The structure looks like this:
     from 
     [ 
     module 
     ] 
     import 
     [ 
     function 
     ], 
     [ 
     otherFunction 
     ], 
     [ 
     anotherFunction 
     ], 
     ... 
    
    • For example, to import the randint and random functions from the random module and print random numbers using these functions, you would write:
       from 
       random 
       import 
       randint 
       , 
       random 
       print 
       ( 
       randint 
       ( 
       0 
       , 
       5 
       )) 
       print 
       ( 
       random 
       ()) 
      
  4. 4
    Import entire modules using a * instead of a function name. The structure looks like this:
     from 
     [ 
     module 
     ] 
     import 
     * 
    
    • For example, to import the entire random module and then print a random number with its randint function, you would write:
       from 
       random 
       import 
       * 
       print 
       ( 
       randint 
       ( 
       0 
       , 
       5 
       )) 
      
  5. 5
    Import multiple modules by writing multiple from-import instructions. You should start a new line for each instruction to keep the code readable, although separating them with a ; also works.
    • For example, to import the randint function from the random module and the sqrt function from the math module and then print a result from both functions, you would write:
       from 
       random 
       import 
       randint 
       from 
       math 
       import 
       sqrt 
       # Would also work, but hard to read: 
       # from random import randint; from math import sqrt 
       print 
       ( 
       randint 
       ( 
       0 
       , 
       5 
       )) 
       print 
       ( 
       sqrt 
       ( 
       25 
       )) 
      
    Advertisement
Method 2
Method 2 of 2:

Using the import instruction

The import instruction imports functions from a module and leaves it visible that the functions are from that module. When using a function imported with the import instruction, you have to write the module name and a dot (.) before it. The import instruction doesn't allow to import a single function from a module without also importing all others.

  1. 1
    Find the module. Locate the module that you will be importing. A complete list of built in modules can be found here (v2.7) and here (v3.5).
  2. 2
    To import a module, write with the following structure:
     import 
     [ 
     module 
     ] 
    
    • For example, to import the random module and then print a random number with its randint function:
       import 
       random 
       print 
       ( 
       random 
       . 
       randint 
       ( 
       0 
       , 
       5 
       )) 
      
  3. 3
    Separate multiple modules with a comma (,). The structure is:
     import 
     [ 
     module 
     ], 
     [ 
     otherModule 
     ], 
     [ 
     anotherModule 
     ], 
     ... 
    
    You can also make multiple import instructions on multiple lines if that appears more legible or makes more sense in your specific case.
    • For example, to import the random and math modules and then print the results of the randint and sqrt functions that are included in these modules, you would write:
       import 
       random 
       , 
       math 
       print 
       ( 
       random 
       . 
       randint 
       ( 
       0 
       , 
       5 
       )) 
       print 
       ( 
       math 
       . 
       sqrt 
       ( 
       25 
       )) 
      
    Advertisement

Expert Q&A

Ask a Question

      Advertisement

      About this article

      Thanks to all authors for creating a page that has been read 24,634 times.

      Is this article up to date?

      Advertisement