10. Python modules and packages

Página 49

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:

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.

Next page of the Free Ebook:

5010.1. Python Modules and Packages: Introduction to Python Modules and Packages

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text