How to Package a Python Library as a Zip and Install It via pip
This step‑by‑step guide shows how to structure a Python project, write a setup.py and README, add code and tests, build a source distribution, create a zip package, upload it to PyPI with Twine, and finally install the library using pip.
1. Prepare Project Structure
Start with a conventional layout for a library named example_package. The top‑level directory contains the package source, a test suite, documentation, and packaging files.
example_package/
├── example_package/
│ ├── __init__.py
│ ├── module1.py
│ └── module2.py
├── tests/
│ ├── __init__.py
│ ├── test_module1.py
│ └── test_module2.py
├── README.md
├── setup.py
└── requirements.txt example_package/– main package directory. tests/ – unit‑test code. README.md – project description. setup.py – packaging and installation configuration. requirements.txt – third‑party dependencies.
2. Write setup.py
The setup.py file is consumed by setuptools to define how the library is built and distributed. Below is a minimal example.
from setuptools import setup, find_packages
setup(
name='example_package',
version='0.1',
packages=find_packages(),
install_requires=[
# list dependencies here, e.g. 'requests',
],
include_package_data=True,
zip_safe=False,
author='Your Name',
author_email='[email protected]',
description='A sample Python library',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
url='https://github.com/yourusername/example_package',
classifiers=[
'Programming Language :: Python :: 3',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
],
python_requires='>=3.6',
)3. Write README.md
The README, written in Markdown, provides an overview, installation instructions, and usage examples.
# example_package
This is a sample Python library.
## Installation
Install with pip:
```sh
pip install example_package
```4. Write Code and Tests
Implement library functionality in module1.py and create matching unit tests.
def some_function():
return "Hello, World!" import unittest
from example_package import module1
class TestModule1(unittest.TestCase):
def test_some_function(self):
self.assertEqual(module1.some_function(), "Hello, World!")
if __name__ == '__main__':
unittest.main()5. Build the Distribution
From the project root, run the following command to create a source distribution (sdist). The resulting archive appears in the dist/ folder.
python setup.py sdist6. Create a Zip Distribution
To generate a zip‑format package, add the --formats=zip option.
python setup.py sdist --formats=zipThis produces dist/example_package-0.1.zip.
7. Publish and Install via pip
Upload the distribution files to PyPI using Twine, then install the package with pip.
twine upload dist/* pip install example_package8. Conclusion
By following these eight steps—structuring the project, configuring setup.py, documenting with a README, writing code and tests, building a source and zip distribution, publishing to PyPI, and installing with pip—you can create a zip‑based Python library that other developers can easily install and use.
References
Python official documentation: https://docs.python.org/3/
Setuptools documentation: https://setuptools.readthedocs.io/en/latest/
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development & AI Practice
DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
