3.8. ModulesĀ¶

A module is a file containing Python definitions and statements intended for use in other Python programs. There are many Python modules that come with Python as part of the standard library. Providing additional functionality through modules allows you to only use the functionality you need when you need it, and it keeps your code cleaner.

Functions imported as part of a module live in their own namespace. A namespace is simply a space within which all names are distinct from each other. The same name can be reused in different namespaces but two objects canā€™t have the same name within a single namespace. One example of a namespace is the set of street names within a single city. Many cities have a street called ā€œMain Streetā€, but itā€™s very confusing if two streets in the same city have that name! Another example is the folder organization of file systems. You can have a file called todo in your work folder as well as your personal folder, but you know which is which because of the folder itā€™s in; each folder has its own namespace for files. Note that human names are not part of a namespace that enforces uniqueness; thatā€™s why governments have invented unique identifiers to assign to people, like passport numbers.

The Python Documentation site is an extremely useful reference for all aspects of Python. The site contains a listing of all the standard modules that are available with Python (see Global Module Index). You will also see that there is a Standard Library Reference and a Tutorial as well as installation instructions, how-tos, and frequently asked questions. We encourage you to become familiar with this site and to use it often.

If you have not done so already, take a look at the Global Module Index. Here you will see an alphabetical listing of all the modules that are available as part of the standard library. Find the turtle module.

3.8.1. Importing ModulesĀ¶

In order to use Python modules, you have to import them into a Python program. That happens with an import statement: the word import, and then the name of the module. The name is case-sensitive. Roughly translated to English, an import statement says ā€œthereā€™s some code in another file; please make its functions and variables available in this file.ā€ More technically, an import statement causes all the code in another file to be executed. Any variables that are created and functions that are defined during that execution can then be used in the current file.

By convention, all import commands are put at the very top of your file. They can be put elsewhere, but that can lead to some confusion, so itā€™s best to follow the convention.

Where do these other files that you can import come from? They may be standard library modules that are available when you install Python, or they may be code files that you wrote yourself, or they could be code that someone else wrote and you downloaded on to your computer.

For example, if you have a file myprog.py in directory ~/Desktop/mycode/, and myprog.py contains a line of code import morecode, then the python interpreter will look for a file called morecode.py, excecute its code, and make its variables, objects and functions available for reference in the rest of the code in myprog.py.

Note that it is import morecode, not import morecode.py, but the other file has to be called morecode.py.

In some sense, you can think of importing as being similar to including the code from a module into the code you are writing, as depicted in this diagram:

../_images/modules_diagram.png

As shown in the diagram, you need to remember that the two sets of code have different namespaces, which typically means that you access the variables and objects in the imported module by using something like module_name.variable_name.

The tests you see in your problem sets make use of a Python module that is in the standard library, called unittest. Right now, you canā€™t see the code that causes those tests to run, because we have hidden it from you, but in later courses, you will learn how to write your own Unit Tests for code, and to do so, you will need to write an import statement at the beginning of your programs. Even before you learn how to write your own tests, you will see code for Unit Tests in your problem set files.

Donā€™t overwrite standard library modules!

It is possible to overwrite a standard library. For example, if you create a file random.py in the same directory where myprog.py lives, and then myprog.py invokes import random, it will import your file rather than the standard library module. Thatā€™s not usually what you want, so be careful about how you name your python files!

3.8.2. Syntax for Importing Modules and FunctionalityĀ¶

When you see imported modules in a Python program, there are a few variations that have slightly different consequences.

  1. The most common is import morecode. That imports everything in morecode.py. To invoke a function f1 that is defined in morecode.py, you would write morecode.f1(). Note that you have to explicitly mention morecode again, to specify that you want the f1 function from the morecode namespace. If you just write f1(), python will look for an f1 that was defined in the current file, rather than in morecode.py.

  2. You can also give the imported module an alias (a different name, just for when you use it in your program). For example, after executing import morecode as mc, you would invoke f1 as mc.f1(). You have now given the morecode module the alias mc. Programmers often do this to make code easier to type.

  3. A third possibility for importing occurs when you only want to import SOME of the functionality from a module, and you want to make those objects be part of the current moduleā€™s namespace. For example, you could write from morecode import f1. Then you could invoke f1 without referencing morecode again: f1().

Note: Python modules and limitations with activecode

Throughout the chapters of this book, activecode windows allow you to practice the Python that you are learning. We mentioned in the first chapter that programming is normally done using some type of development environment and that the activecode used here was strictly to help us learn. It is not the way we write production programs.

To that end, it is necessary to mention that many of the modules available in standard Python will not work in the activecode environment. In fact, only turtle, math, random, and a couple others have been ported at this point. If you wish to explore any additional modules, you will need to run from the native python interpreter on your computer.

Check your understanding

You have attempted of activities on this page