Modules and Packages in Python-Complete Tutorial

Modules and packages in Python

This Modules and Packages in Python-Complete Tutorial is made for you to give complete knowledge of the Modules and Packages in Python. At the end of this tutorial, you will have full knowledge of the Modules and Packages in Python.

Hello, & Welcome! to the Modules and Packages in Python-Complete Tutorial.

Part 15- Modules and Packages in Python-Complete Tutorial

In this article of Python Tutorial, you will learn following-

  1. What are the Modules in Python?
  2. Packages in Python.
  3. How to Import Modules?
  4. How to create and Import a Package?

What are the Modules in Python?

A Module is a section or segment in which different functions are present and these functions perform some different operations.

A module helps you to logically organize your code. Suppose you have to perform some specific operation in your code and your code is too long and messy, so instead of writing more code, you can simply import the module. After importing the module in your code you can use it in your program.

By doing so, you save your time and your code will not become messy. Because writing the full code again in your program makes it messy and unable to read.

By importing modules in your program, you can simply access its functionality in your code.

Packages in Python.

A Package is nothing but a collection of modules. It is also imported into programs.

In Package, several modules are present, which you can import in your code.

Suppose, you are working on any project in python, and which require a lot of code with different classes and functions. So instead of writing all code in one file, you need to divide it into different files. Which you can import it later whenever you have need of this code.

For example-

You are working on one file named Classes.py and there are lots of code including different functions and attributes. And you want to do more work, but a file with lots of code looks messy. So instead of writing all code in one file, you can create a new file with the name Test.py. In Test.py you can use all the functions and attributes of Classes.py.

In the next section, we will see how you can import a module into your new file Test.py.

How to Import Modules?

As we are discussing, to import functions and attributes of Classes.py into Test.py file. So, for importing any module in a file you need to write-

from Classes import Planet

Here, Classes is the name of a class from where you are importing modules. And Planet is the name of the module, which means what you want to import.

So, after writing this line of code, you can use the Planet class inside your Test.py file.

This means you can write this code in your Test.py file and it works perfectly fine because you have imported Planet class here.

from Classes import Planet
Class Test
    shape="round"
naboo=Planet("naboo",300,8,'Naboo')
print(f'Name:{naboo:name}')

Here, we are using Planet class and its attribute inside our Test class.

How to create and Import a Package?

A Package is a collection of modules. To create a package, you need to create a folder, suppose you create a folder name as Space.

To tell python that Space is a package, just create an __init__.py file. Which is enough to tell python that Space is a package.

Don’t write anything in __init__.py file, just left it blank.

then,

Inside Space package create a new file named Planet.py file. This Planet.py acts as a module for the Space package.

Then, in Planet.py file just paste the code from the previous planet.py file.

NOTE- Name of files may be anything it is up to you, these names are just for reference 🙂

So, after pasting your code in Planet.py file, create one more file inside the Space package name as calc.py, where different functions are performed.

After creating calc.py, create one file outside from the Space Package just name as Classes.py and in that file import packages and its modules.

Are you Lost with too many file names?

Don’t worry! we will revise all the files which we have created.

  • One Package – Space
    • __init__.py
    • Planet.py (act as a Module of a Package)Paste your code from the previous file
    • calc.py (Different functions are performed here)
  • Classes.py (Outside of the Package)

I hope now you understand 🙂

Import a Package-

Now, let’s import the package and its modules in the Classes.py file. To import package Space and its module in Classes.py file, you need to write following line of code-

from Space.Planet import Earth

Here, as you know Space is a package name. Planet is the module name. And Earth is the class name inside the Planet module.

You can import multiple functions and classes from the same module just by writing comma (,).

Let’s see in the following example-

from Space.calc import earth_mass,earth_vol

Here, Space is a package name, calc is module name and earth_mass and earth_vol are the function name inside the calc module.

NOTE- By importing packages and modules in another file. And when you run this file the output of all modules which you have imported will be shown on your file.

That’s all for the Modules and Packages in Python. I hope now you have a better understanding of the Modules and Packages in Python.

Congratulations! You successfully learned the Modules and Packages in Python.

Range() Function in Python-Complete Tutorial

In the next tutorial, we will start learning List Comprehensions in Python.

Till then, Enjoy Learning Python!

Are you looking for Best Books on Python for Beginners and for Intermediate/Experts?

Check these Books, These Books are the best selling and covered everything in detail.

I hope you will find these Books helpful-

Best Selling Python Books for Beginners

Best Selling Python Books for Intermediates/Experts

All the Best!

Back to Python Tutorial

Thank YOU!

Though of the Day…

“Live as if you were to die tomorrow. Learn as if you were to live forever.” 

Mahatma Gandhi

3 thoughts on “Modules and Packages in Python-Complete Tutorial”

  1. Pingback: Method and Attributes in Python-Complete Tutorial for Everyone(2020)

    1. In my previous tutorial “Method and Attributes in Python,” I have created Planet class, so I mentioned that Planet class code. but This code may be anything, it’s up to you. Here I mentioned just for reference.

Leave a Comment

Your email address will not be published. Required fields are marked *