03. Modules

Assume that you were working in a session and implemented a handful of functions. When you close that session, all those functions will be lost. For the next session, you would have to tediously rewrite and import each function.

A better approach to solving this problem is to import a python script that contains all the relevant functions. Such a script is known as a module, which can be imported with a single command.

Additionally, you could download a complete set of functions that are pre-written from the web. Such extensibility is what makes Python such an attractive language for data scientists and application programmers, who are able to use these packages to ease their workflow.

Creating your own module

To create your own module, create a plain text file with the .py extension.

# module hi.py
def HelloWorld():
  print("Hello World!")

Importing Modules

Now if we are in the same folder as hi.py, and we initialize Python, we may import our module.

>>> hi.HelloWorld()
Hello World!

Accessing functions directly

Note that the function names are stored within the local symbol table, so you have to precede each function call with hi. If you'd like to access the functions directly, you may do so:

>>> from sample import HelloWorld 
>>> HelloWorld()
Hello World!

Importing all functions

Importing all modules is sometimes considered bad practice, as you may be overwriting names that already exist. However, if you are sure no important names are overwritten, and want to make your code looking cleaner, you can do so using from sample import *. This effectively imports all functions from the sample.py module.

Importing a single function

In some cases you'll want to import just one function instead of the entire module. We may do so with the following syntax.

>>> from os import getcwd
>>> getcwd()

You may also want to import using a different name (alias). To do so, use the from and as keywords.

>>> from os import getcwd as pwd
>>> pwd()

Sample modules

Two of the most commonly used modules are the OS and Random modules. Let's take a quick look at them and how they can be used.

The OS Module

The os module provides an interface to the current operating system.

>>> import os
>>> os.getcwd()
>>> os.getlogin()

The Random Module

Another important module is one that allows you to generate random numbers. This is known as the random module.

>>> import random
>>> random.randint(2,4)
# Generates a random number between 2 and 4 (inclusive)

Learn to be a Pythonista!

Programming Python

Learn to be a Pythonista! Try Python

Programming Python shows in-depth tutorials on the language's number of application domains including: system administration, GUIs, the Web, networking, front-end scripting layers, and more. This book focuses on commonly used tools and libraries to give you a comprehensive understanding of Python’s many roles in practical, real-world programming.

$ Check price
64.9964.99Amazon 4 logo(56+ reviews)

More Python resources

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources