Python Modules, Packages, and Conventions

Python Modules, Packages, and Conventions

In this tutorial, we'll discuss how to use modules and packages in Python, as well as some best practices and conventions to follow

Python is a powerful and flexible programming language that has been widely adopted in many industries. One of the reasons for its popularity is its rich ecosystem of modules and packages. In this tutorial, we'll discuss how to use modules and packages in Python, as well as some best practices and conventions to follow when working with them.


A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py. For example, if we have a file named with the following contents:

def say_hello(name):
    print(f"Hello, {name}!")

We can import it into another file using the import statement:

import hello


This will output Hello, World! to the console.

We can also import specific functions or variables from a module using the from ... import statement:

from hello import say_hello


This will output Hello, Python! to the console.


A package is a way of organizing related modules together. A package is simply a directory containing a special file called The file can be empty, or it can contain Python code that is executed when the package is imported.

Let's say we have a directory structure like this:


We can import the modules like this:

import my_package.module1
import my_package.module2


Or we can use the from ... import ... statement to import specific functions or variables:

from my_package.module1 import function1
from my_package.module2 import variable2


This will execute function1() from and print the value of variable2 from


When working with modules and packages, it's important to follow some conventions to make your code more readable and maintainable. Here are some best practices to follow:

  • Use descriptive names for modules and packages. This will make it easier for other developers to understand what your code does.

  • Use lower_case_with_underscores for module and package names. This is the recommended naming convention in Python.

  • Use relative imports within a package. For example, if you have a module named and another module named in the same package, you should use the following import statement in

      from . import module1

    This tells Python to look for in the same package as

  • Avoid using * in from ... import ... statements. This can make it difficult to understand which functions or variables are being imported, and it can also lead to naming conflicts.

  • Use if __name__ == "__main__": to define a block of code that should only be executed if the module is run as the main program. This is a common convention in Python, and it allows you to write code that can be both imported as a module and run as a standalone program.


Python modules and packages are powerful tools for organizing and reusing code. By following some simple conventions, you can make your code more readable, maintainable, and reusable.

I'd love to connect with you via Twitter & LinkedIn

Happy Coding!