Whenever you create a new Python program, you are creating a module. In other words, a module is just a Python program. A module is useful, however, because it can be imported into another module. Any variables created in the module will then be available in the other module. So far, we have been creating individual Python programs and running them. But over time, we will have many programs. It will be important to be able to reuse work we have done in new programs. We also want to be able to use stuff created by other programmers.
Modules are sometimes also known as “packages” or “libraries.” Typically, a package is a collection of one or more modules. However, the words are all more or less interchangeable.
import helper
print("The other module has a variable named scores with the value", helper.scores)
If you have two Python files (or modules,) then you can create variables in one module and then import those variables into the other module.
To import data from another module, you use the import statement.
There are three different ways to import a module.
The simplest is shown here, where we write the keyword import and the name of the module.
Once a module is imported, you can reference its definitions using the name of the module, a period (.), and the name of the variable you want to use.
When you run this example, you will see that there is a second Python file named helper.py with a variable named scores defined inside:
scores = "10, 30, 50, 20"
You can also open helper.py directly.
The module is imported as helper so that we can access the string variable.
from math import pi
print("The value of pi is", pi)
It can be bothersome to repeat the module name each time you want to use a variable from the module.
Therefore, Python has a convenient syntax for importing the variables directly.
This version uses the from keyword, followed by the module name, the import keyword, and then the name of the variable or other definition you want to use.
Afterwards, you can use the variable as if it were defined in that file.
from string import digits
print("The digits are", digits)
Python has many useful built-in modules called the Standard Library.
Furthermore, Python programmers have created many other “third-party libraries” that are easy to install.
Many of these modules come prepackaged in versions of Python that are easy to obtain.
We will not cover all these modules in this course, but you will often come across them when solving real-world problems.
The string module shown here has string variables that you can import, which hold various strings of characters.
For example, the digits variable holds all the digits 0123456789.
from math import e
print("The value of e is", e)
The math module has mathematical constants like e (which is 2.718281).
The programmers who created Python had to define these variables and put them in files that you can import even if you cannot normally see them.
There are a lot of standard libraries that are very convenient as we will see!
> pip install pygame
> pip install requests
> pip install matplotlib
Although you will not need to do it in this environment, it is very easy to install new modules from a command line.
Python has a special tool named pip to install a module.
You will need to know the exact module name when installing a library.
Make sure you have the right module name; when you install a new module, you are trusting the developer to not install anything malicious! Some bad actors create packages with similar names to other popular libraries to try and trick unsuspecting developers into installing their version of the package instead.
Most modern editors also include a package manager that lets you install packages without writing code.
You can’t install new libraries for the exercises in this course, but you can still import modules that an exercise gives you.
Most editors allow you to create additional Python files that can be imported into your main Python file.
Some exercises in this course come with such an additional file.
In some cases, we’ll have already created the file for you, but in others we may ask you to write the new module yourself.
When an exercise gives you a file, its description links to it, and the file is available both when you run your code and when you submit it.
You can import it just like a file in your local filesystem.
For example, an upcoming exercise gives you a file named my_second_module.py.
Its module name is my_second_module: you leave out the .py when you import it.