Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
A Python package is a collection of related modules organized in a directory hierarchy. Packages help you manage and organize your code, making it more maintainable and reusable. In this tutorial, we'll cover how to create a Python package, install it, and use it in your projects.
Let's create a simple package called mypackage
with the following structure:
mypackage/ __init__.py module_a.py module_b.py
mypackage
.mypackage
, create an empty file called __init__.py
. This file is required for Python to treat the directory as a package, and it can also be used to initialize package-level variables or run package-level code.module_a.py
and module_b.py
inside the mypackage
directory, representing two modules within the package. Add the following code to each module:module_a.py:
def hello_a(): return "Hello from module A!"
module_b.py:
def hello_b(): return "Hello from module B!"
To make the package available for use in other projects, you can either install it locally or package it for distribution.
mypackage
and run the following command:pip install -e mypackage
The -e
flag installs the package in "editable" mode, meaning changes to the package's source code will be immediately reflected in the installed package.
setup.py
file, which provides metadata and dependencies for your package. In the parent directory of mypackage
, create a setup.py
file with the following content:from setuptools import setup, find_packages setup( name="mypackage", version="0.1", packages=find_packages(), )
Now, you can build and install the package using:
python setup.py sdist pip install .
Once the package is installed, you can import and use its modules and functions in your projects:
from mypackage.module_a import hello_a from mypackage.module_b import hello_b print(hello_a()) # Output: Hello from module A! print(hello_b()) # Output: Hello from module B!
Alternatively, you can use the following import statement:
import mypackage.module_a as ma import mypackage.module_b as mb print(ma.hello_a()) # Output: Hello from module A! print(mb.hello_b()) # Output: Hello from module B!
In summary, Python packages help you organize and manage your code by grouping related modules in a directory hierarchy. To create a package, make a directory with an __init__.py
file and the desired modules. Then, install the package locally or package it for distribution. Finally, import the package's modules and functions in your projects to use them.
Creating and organizing Python packages:
mypackage/ ������ __init__.py ������ module1.py ������ module2.pyYou can then import modules from the package as follows:
from mypackage import module1
Installing Python packages with pip:
pip
is a package installer for Python. You can use it to install packages from the Python Package Index (PyPI).pip install package_name
Exploring third-party Python packages:
pip search package_name
Dependencies and requirements in Python packages:
requirements.txt
file or within your setup.py
.requirements.txt
):requests==2.26.0
Versioning and updating Python packages:
==
, >=
, <
, etc., in requirements.txt
to specify versions.requests>=2.26.0,<3.0.0
Publishing and distributing Python packages:
twine
for secure and efficient distribution.twine upload dist/*
Packaging and distributing Python modules:
setuptools
to package and distribute your Python modules.setup.py
):from setuptools import setup setup( name='mypackage', version='0.1', packages=['mypackage'], install_requires=[ 'requests>=2.26.0,<3.0.0', ], )
Virtual environments and Python package management:
python -m venv myenv