Skip to main content

What is a Module?

A module is easiest to picture as a code library: a single file that holds a set of functions you would like to bring into your application.


Create a Module

There is nothing special to set up. Put the code in a file whose name ends in .py and you have a module:

Example

Put this code into a file called mymodule.py
main.py
def greeting(name):
  print("Hello, " + name)

Use a Module

With the file saved, the import statement brings the module into your program:

Note: To call one of its functions, write the module name, a dot, and the function name: module_name.function_name.

Example

Bring in mymodule and run its greeting function:
Prompt
Import the module named mymodule and call the greeting function with the argument "Jonathan". The module contains a function greeting(name) that prints "Hello, " + name.
Output: complete Python program

Variables in Module

Functions are not the only thing a module can hold. It can also store variables of any type, such as lists, dictionaries and objects:

Example

Bring in mymodule and read a value from its person1 dictionary:
Prompt
Import the module named mymodule and access the person1 dictionary's "age" key. Print the age value.
Output: complete Python program

Example

This code goes in mymodule.py
main.py
person1 = {
  "name": "John",
  "age": 36,
  "country": "Norway"
}

Naming a Module

The file name is entirely up to you, as long as it ends with the .py extension.


Re-naming a Module

The as keyword lets you give a module a different name, an alias, at the moment you import it:

Example

Refer to mymodule by the alias mx:
Prompt
Import the module named mymodule with the alias mx. Access person1["age"] through the alias and print it.
Output: complete Python program

Built-in Modules

Python ships with a number of modules of its own, ready to import at any time.

Example

Bring in the platform module and call one of its functions:
Prompt
Import the built-in platform module. Use platform.system() to get the system name and print it.
Output: complete Python program

Using the dir() Function

To see what a module offers, call the built-in dir() function, which lists the names of all its functions and variables:

Note: dir() works on every module, including those you write yourself.

Example

Print every name defined in the platform module:
Prompt
Import the platform module. Use the dir() function to list all names in the platform module and print the result.
Output: complete Python program

Import From Module

When you need just one piece of a module, the from keyword lets you import that piece alone.

Note: Items imported with from are used without the module prefix. Write person1["age"], not mymodule.person1["age"].

Example

Bring in nothing but the person1 dictionary:
Prompt
Import only the person1 dictionary from the module named mymodule using the from keyword. Print the "age" value from person1.
Output: complete Python program

Example

Here, mymodule holds one function and one dictionary:
main.py
def greeting(name):
  print("Hello, " + name)

person1 = {
  "name": "John",
  "age": 36,
  "country": "Norway"
}