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: Importing Python Modules and Packages

Capítulo 51

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

10.2 Python Modules and Packages: Importing Python Modules and Packages

Python is a high-level programming language that is extremely versatile and powerful. One of the reasons for its popularity is its rich and extensive standard library, which provides a wide variety of modules and packages to perform a multitude of tasks. In this chapter, we'll explore the concept of modules and packages, and how to import them in Python.

What are Modules and Packages?

In Python, a module is a file containing definitions of functions, classes, and variables, along with the desired execution instructions. In other words, it's a way to organize related code into a single logical unit for easier reuse and maintenance.

A package, on the other hand, is a way to organize related modules in a directory structure. So, a package is a collection of modules in a directory that also contains a special file called __init__.py, which tells the Python interpreter that the directory is a package.

Importing Modules

To use the functions and classes defined in a module, you must first import the module. The 'import' keyword is used for this. For example, to import the 'math' module, which contains a series of math functions, you would write:

import math

You now have access to all functions and classes defined in the 'math' module. For example, you can use the 'sqrt' function to calculate the square root of a number:

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 math
print(math.sqrt(16)) # prints: 4.0

If you only want to import a specific function from a module, you can use the 'from' keyword together with 'import'. For example:

from math import sqrt
print(sqrt(16)) # prints: 4.0

This is useful when you want to avoid having to prefix all function calls with the module name.

Importing Packages

Importing packages is similar to importing modules. Suppose you have a package called 'mypackage' which contains a module called 'mymodule'. You can import the module as follows:

import mypackage.mymodule

You can now access functions and classes in 'mymodule' by prefixing them with 'mypackage.mymodule'. For example:

import mypackage.mymodule
print(mypackage.mymodule.myfunction())

Again, if you want to avoid the prefix, you can use 'from' and 'import' together:

from mypackage import mymodule
print(mymodule.myfunction())

Conclusion

Python modules and packages provide an effective way to organize related code into logical units for easier reuse and maintenance. They also allow you to take advantage of Python's extensive standard library and other third-party libraries to perform a wide range of tasks. Importing modules and packages is a fundamental skill every Python programmer should master.

Now answer the exercise about the content:

What are modules and packages in Python and how are they used?

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

You missed! Try again.

Modules and packages in Python serve to organize related code into logical units, facilitating reuse and maintenance. A module is a file containing definitions of functions, classes, and variables. A package is a collection of modules organized in a directory, with an __init__.py file indicating it's a package. Both are imported using the import keyword, allowing access to their features in your code.

Next chapter

Python Modules and Packages: Creating and Organizing Python Packages

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