Python Tutorial - Master Python Programming For Beginners from Scratch

  • Home
  • Getting Started
  • Basics
  • OOP
  • Advanced Python
  • Tkinter
Home » Python Basics » Python Packages

Python Packages

Summary: in this tutorial, you learn about the Python packages and how to use them to structure your application.

Introduction to Python packages

Suppose that you need to develop a large application that handles the sales process from order to cash.

The application will have many modules. When the number of modules grows, it’ll become difficult to keep all of them in one location.

And you may want to group modules into something meaningful.

This is where packages come into play.

Packages allow you to organize modules in the hierarchical structure.

The way Python organizes packages and modules like the Operating System structures the folders and files.

To create a package, you create a new folder and place the relevant modules in that folder.

To instruct Python to treat a folder containing files as a package, you need to create a __init__.py file in the folder.

Note that starting with Python 3.3, Python introduced the implicit namespace packages feature. This allows Python to treat a folder as a package without the __init__.py.

For example, the following picture shows the sales package that contains three modules including order, delivery, and billing:

Importing packages

To import a package, you use the import statement like this:

import package.module
Code language: Python (python)

And to access an object from a module that belongs to a package, you use the dot notation:

package.module.function
Code language: Python (python)

The following shows how to use functions in the order, delivery, and billing modules from the sales package:

# main.py import sales.order import sales.delivery import sales.billing sales.order.create_sales_order() sales.delivery.create_delivery() sales.billing.create_billing()
Code language: Python (python)

To make the code more concise, you can use the following statement to import a function from a module:

from <module> import <function>
Code language: Python (python)

For example:

# main.py from sales.order import create_sales_order from sales.delivery import create_delivery from sales.billing import create_billing create_sales_order() create_delivery() create_billing()
Code language: Python (python)

It’s possible to rename the object when importing it:

# main.py from sales.order import create_sales_order as create_order from sales.delivery import create_delivery as start_delivery from sales.billing import create_billing as issue_billing create_order() start_delivery() issue_billing()
Code language: Python (python)

In this example, we rename…

  • The create_sales_order to create_order,
  • The create_delivery to start_delivery,
  • and the create_billing to issue_billing.

Initializing a package

By convention, when you import a package, Python will execute the __init__.py in that package.

Therefore, you can place the code in the __init__.py file to initialize package-level data.

The following example defines a default tax rate in the __init__.py of the sales package:

# __init__.py # default sales tax rate TAX_RATE = 0.07
Code language: Python (python)

From the main.py file, you can access the TAX_RATE from the sales package like this:

# main.py from sales import TAX_RATE print(TAX_RATE)
Code language: Python (python)

In addition to initializing package-level data, the __init__.py also allows you to automatically import modules from the package.

For example, in the __init__.py, if you place the following statement:

# __init__.py # import the order module automatically from sales.order import create_sales_order # default sales tax rate TAX_RATE = 0.07
Code language: Python (python)

And import the sales package from main.py file, the create_sales_order function will be automatically available as follows:

# main.py import sales sales.order.create_sales_order()
Code language: Python (python)

from <package> import *

When you use the statement to import all objects from a package:

from <package> import *
Code language: Python (python)

Python will look for the __init__.py file.

If the __init__.py file exists, it’ll load all modules specified in a special list called __all__ in the file.

For example, you can place the order and delivery modules in the __all__ list like this:

# __init__.py __all__ = [ 'order', 'delivery' ]
Code language: Python (python)

And use the following import statement in the main.py:

# main.py from sales import * order.create_sales_order() delivery.create_delivery() # cannot access the billing module
Code language: Python (python)

From the main.py, you can access functions defined in the order and delivery modules. But you cannot see the billing module because it isn’t in the __all__ list.

Subpackages

Packages can contain subpackages. The subpackages allow you to further organize modules.

The following shows the sales package that contains three subpackages: order, delivery, and billing. Each subpackage has the corresponding module.

For example, you can place all other modules related to the order processing in the order subpackage:

Everything you’ve learned about packages is also relevant to subpackages.

For example, to import a function from the order subpackage, you use the following import statement:

# main.py from sales.order import create_sales_order create_sales_order()
Code language: Python (python)

Summary

  • A Python package contains one or more modules. Python uses the folders and files structure to manage packages and modules.
  • Use the __init__.py file if you want to initialize the package-level data.
  • Use __all__ variable to specify the modules that will load automatically when importing the package.
  • A package may contain subpackages.
  • Was this tutorial helpful ?
  • YesNo
Previous Python Module Search Path
Next Python __name__

Getting Started

  • What is Python
  • Install Python
  • Setup VS Code for Python
  • Develop Python Hello World Program

Python Fundamentals

  • Python Syntax
  • Variables
  • Strings
  • Numbers
  • Boolean
  • Constants
  • Comments
  • Type Conversion

Control Flow

  • if…else
  • Ternary Operator
  • for Loop
  • while Loop
  • break
  • continue
  • pass

Functions

  • Python Functions
  • Default Parameters
  • Keyword Arguments
  • Recursive Functions
  • Lambda Expressions
  • Function Docstrings

Python List

  • List
  • Tuple
  • Sorting a List in Place: sort()
  • Sorting a List: sorted()
  • Slicing a List: [::]
  • Unpacking a List
  • Iterating over a List: for loop
  • Finding Index of an Element: index()
  • Iterables
  • Transform List Elements: map()
  • Filtering List Elements: filter()
  • Reducing List Elements: reduce()
  • List Comprehensions

Python Dictionary

  • Dictionary
  • Dictionary Comprehension

Python Set

  • Set
  • Set Comprehension
  • Union of Sets
  • Intersection of Sets
  • Difference between Sets
  • Symmetric Difference of Sets
  • Subset
  • Superset
  • Disjoint Sets

Exception Handling

  • try…except
  • try…except…finally
  • try…except…else

Python Loop with Else Clause

  • for…else
  • while…else
  • do…while Emulation

More on Functions

  • Unpacking Tuples
  • The *args Parameters
  • The **kwargs Parameters
  • Partial Functions

Modules

  • Modules
  • Module Search Path
  • Python __name__
  • Packages

File I/O

  • Reading a Text File
  • Writing to a Text File
  • Creating a Text File
  • Checking If a File Exists
  • Reading from a CSV File
  • Writing to a CSV File

Managing Third-party Packages

  • Python Package Index (PyPI) & PIP
  • Python Virtual Environments
  • Install pipenv on Windows
  • Using pipenv Tool

About pythontutorial.net

Pythontutorial.net helps you master Python programming from scratch fast.

Site Links

  • Home
  • Contact
  • About
  • Privacy Policy

Recent Python Tutorials

  • Tkinter StringVar
  • Tkinter PhotoImage
  • Tkinter OptionMenu
  • Tkinter ScrolledText
  • How to Change the Appearances of Widgets Dynamically Using Ttk Style map() Method

Python References

  • Python String Methods
  • Python MySQL
  • Python PostgreSQL
  • Python Oracle
  • Python SQLite

Copyright © 2021 · by pythontutorial.net. All rights reserved. Log in