Developers write hundreds and thousands of lines of code for real-world applications that go to market. There are even projects that are several million lines long. We cannot write such a large project in a single file; the team would be confused by their code and could not maintain and update it. Therefore, we should divide the code into parts. It is a principle used in many programming languages. Python uses modules for this purpose.
In Python, any code file is called a module. There is no difference between the terms. It is best to name files using snake_case, with lower-case letters and an underscore separating words. Think snakes and pythons. It makes it easier for all Python developers to read the finished code,
How to use modules
Working with code of thousands of lines is much easier if we split it into several modules. In this case, developers usually work with the main file and put the individual functions in different modules. The modules are then imported into main.py using the import
using the 'import' keyword. As a result, the development team gets readable code that's easy to maintain.
To practice, let's try creating our module. To do this, we'll create a file called greeting.py. Then, inside this file, we'll define the say_hi()
function and the name
variable:
# file: greeting.py
def say_hi(): # Defining the function
print('Hi!')
name = 'Bob' # Defining the variable
The welcome module is ready. It can print the string Hi!
and handle the variable name
. But it is useless unless we build it into the rest of the program operation. To use our module, you need to import it into the main module, main.py.
There are three ways to do this in Python:
- Importing an entire module
- Importing individual definitions from a module
- Importing all the contents of a module at once
Here we'll only look at the first and second ways. We will discuss the third in detail in the next lesson.
How to import an entire module
The easiest way is to use the import
keyword with the file name without the .py. Go to main.py file and import our greeting.py module there:
# file: main.py
import greeting
The import was successful. We access the welcome module directly from the main file. The module contents are available via a dot. It is how you can call a module function or a separate variable:
# Calling a function from the module
greeting.say_hi() # => Hi!
# Displaying a separate variable
print(greeting.name) # => Bob
It is the most common way because this code is easy to read. Any developer can tell at a glance that the variable used or the function called is part of a particular module.
How to import individual definitions
Sometimes the first method isn't suitable — when you need only several functions or variables from a long and complex module, for example. In this case, the second way of importing will help. We should write the keyword from
with the module name without the .py extension.
Then, on the same line, enter the keyword import
with the names of the definitions we want to use. It is what it looks like in the code:
# file: main.py
from greeting import say_hi, name # Importing the individual module components
print(name) # Using the imported variable
say_hi() # Calling the imported function
In the next lesson, we'll deepen our knowledge of this topic and look at the third way of importing a module.