Python Modules

Modules are a great way to keep your code well organized, as they allow you to reuse your code across multiple files. You can also make use of the many useful modules included in Python's extensive standard library.

A module is simply a file containing Python definitions and statements, and it has a file extension of .py. You can import modules into other modules in order to make use of the code that the imported modules contain.

Python also comes with a library of standard modules. Some modules are built into the Python interpreter, which means that you can use them immediately.

Here's an example, where we import the random module in order to generate a random number:

Result

72

We import the random module because it contains code that we require. Specifically, it contains the randint() function that generates the random number.

If we didn't use import random we would've ended up with NameError: name 'randint' is not defined being returned instead of the random number.

Also notice that we prepend the function name with its module name, separated by a dot (random.randint()). This is required due to the fact that we only specified random when importing the module. We didn't specify the functions within that module, so we need to qualify our function call with the module name.

There are a couple of other ways of doing this though.

Assign a Local Name

If you don't like prefixing functions with the module name, you can assign it a local name. This allows you to call that function without the prefix.

Here's how to do that:

Result

82

Variation of the import Statement

There's a variation of the import statement that allows you to name the functions to import from the module. When you do this, you can call the functions without having to qualify them with the module name.

It goes like this:

Result

93

You can import multiple functions by separating them with a comma:

Result

94
40

Create a Module

Let's take the two functions that we created previously and put them into a new module called calculations.py.

So here's what calculations.py looks like:

Now we can call that module and its functions from within another file. Note that you don't need to specify the .py when importing the module.

Here's an example of importing our new module and then calling its functions.

Result

800591878564
1312
320775
0.3292806484295846
-662

Python Packages

The above code example assumes that the module is in the current folder or the module search path.

You can put your modules into folders so that they're well organized. You could create one folder for all modules, or you could create multiple folders, each for a different group of modules. Either way, the process for accessing them is the same.

To access modules within a folder, you can use dot (.) notation.

For example, if we move the above calculations.py file to a folder called mypackage, we can access the module using mypackage.calculations. So we would change the first line of the above example to this:

You can nest packages inside each other. If you do this, simply use dot notation for the full path. So if your module path was inside a second level package, you'd use something like this myPackage.anotherPackage.calculations.

Package Initialization

There's an important thing to remember when creating packages. Each package must contain a file called __init__.py.

This file is required before Python treats it as a package. This can be an empty file, but it can also execute initialization code for the package or set the __all__ variable.

So the first thing to do when creating a package is to create an empty __init__.py file. Of course, you can add code to it if you need to, but if you have no code to add, just leave it empty.

So your package structure might look something like this:

Result

mypackage/
		__init__.py
		package1/
				__init__.py
				module1.py
				module2.py
		package2/
				__init__.py
				module1.py
				module2.py				

Try to give your packages and modules concise but descriptive names. Don't call them mypackage or module1 etc like I've done here. I've only done that to demonstrate the concept of packages.

Other ways to Import Packages

There's more than one way to import a package (or a module inside a package). The method you use will determine whether or not you need to prefix your function names with the module and/or package name.

Here are the variations:

Module Search Path

When you (try to) import a module, the interpreter goes through a process to look for it. The interpreter first searches for a built-in module of that name. If not found, it then searches for a file of that name in a list of directories given by the sys.path variable. The sys.path variable is initialized from these locations: