Python packages tutorial shows how to work with packages in Python. Packages are namespace containers for modules.
last modified March 15, 2024
Python packages tutorial shows how to work with packages in Python. Packages are namespace containers for modules.
A Python package is a collection of Python modules. Packages help organize related modules into a single directory hierarchy. Packages are created by adding a special init.py file to a directory.
This example shows how to create a basic Python package.
mypackage/init.py
version = ‘1.0’
main.py
#!/usr/bin/python
import mypackage
print(mypackage.version)
We create a package called mypackage with an init.py file. The file can be empty or contain initialization code.
This example shows a package with multiple modules.
mypackage/greet.py
def say_hello(): print(“Hello from greet module”)
main.py
#!/usr/bin/python
from mypackage.greet import say_hello
say_hello()
We add a greet.py module to our package and import its function in main.py.
Different ways to import package contents.
main.py
#!/usr/bin/python
import mypackage.greet from mypackage import greet from mypackage.greet import say_hello
mypackage.greet.say_hello() greet.say_hello() say_hello()
The example shows three ways to import and use package contents.
Using all to control what gets imported with from package import *.
mypackage/init.py
all = [‘greet’]
main.py
#!/usr/bin/python
from mypackage import *
greet.say_hello()
The all list defines which modules are imported when using from package import *.
Creating and using subpackages.
mypackage/subpkg/init.py
mypackage/subpkg/utils.py
def helper():
print("Helper function from utils")
main.py
#!/usr/bin/python
from mypackage.subpkg.utils import helper
helper()
We create a subpackage subpkg with its own init.py and a utils.py module.
Installing third-party packages using pip.
Terminal
$ pip install requests $ pip install numpy pandas –user $ pip install -r requirements.txt
The example shows common pip commands for installing Python packages.
Creating a distributable package with setup.py.
setup.py
from setuptools import setup, find_packages
setup( name=“mypackage”, version=“0.1”, packages=find_packages(), install_requires=[ ‘requests>=2.25.1’, ], )
The setup.py file defines package metadata and dependencies.
Using relative imports within a package.
mypackage/subpkg/mod.py
from ..greet import say_hello
def mod_func(): say_hello() print(“From mod module”)
The example shows how to use relative imports with .. notation.
Creating namespace packages without init.py files.
ns_pkg/pkg1/mod1.py
def ns_func(): print(“Namespace package function”)
main.py
#!/usr/bin/python
from ns_pkg.pkg1.mod1 import ns_func
ns_func()
Python 3.3+ supports namespace packages that don’t require init.py.
Including non-Python files in packages.
setup.py
from setuptools import setup
setup( name=“mypackage”, version=“1.0”, package_data={ ‘mypackage’: [‘data/*.txt’], }, )
The example shows how to include data files in your package distribution.
In this article we have worked with Python packages.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.