Free Ebook cover System creation course with Python and Django complete

System creation course with Python and Django complete

New course

176 pages

Python modules and packages

Capítulo 49

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

One of the key features of a modern programming language is the ability to manipulate and organize code into reusable blocks. In Python, these blocks are called modules and packages. We'll explore these concepts in detail in this chapter of our Complete Systems Building with Python and Django e-book course.

What are modules?

In Python, a module is a file containing Python definitions and instructions. The file name is the module name with the suffix '.py' added. Within a module, the module name (as a string) is available as the value of the global variable '__name__'. A module allows you to logically organize your Python code.

For example, a file called 'example.py' in Python is a module called 'example'. We can define functions and variables in this module that can be used in other modules through the import process.

Importing Modules

We can use any Python module in another module simply by using the 'import' keyword. For example, if we have a module called 'example' and we want to use the function defined in it called 'my_function', we can do it like this:

import example
example.my_function()

Importing with alias

In Python, modules can be imported using aliases. This is done using the 'as' keyword. This is useful in cases where the module name is long and we want a shorter way to refer to it. For example:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

import example as ex
ex.my_function()

What are packages?

Packages are a way to organize related Python modules in a folder. In simple terms, a package is a folder that contains multiple modules. One important folder that a Python package must contain is the '__init__.py' file. This file can be empty, but it must be present in the folder.

For example, we might have a package called 'my_package' that contains two modules: 'module1' and 'module2'. The directory structure will look like this:

my_package/
    __init__.py
    module1.py
    module2.py

Importing modules from a package

We can import modules from a package using the 'from' keyword combined with 'import'. For example, if we want to import 'module1' from package 'my_package', we can do it like this:

from my_package import module1
module1.my_function()

In summary, Python modules and packages are powerful tools for organizing and reusing code. They allow developers to write more readable code and keep the code clean and organized. Throughout this course, you will have many opportunities to use modules and packages as you build systems with Python and Django.

I hope this chapter has provided you with a clear understanding of modules and packages in Python. In the next chapter, we'll explore more advanced aspects of Python programming.

Now answer the exercise about the content:

What is a module in Python and how can it be used?

You are right! Congratulations, now go to the next page

You missed! Try again.

A Python module is a file containing Python definitions and instructions, allowing for logical code organization. Functions and variables in a module can be used in other modules through the import process.

Next chapter

Python Modules and Packages: Introduction to Python Modules and Packages

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.