Rahul R

Mar 20, 20231 min

How to create a python package for distribution

To create a Python package for deployment to PyPI, you can follow these steps:

  • Choose a name for your package and create a new directory with that name.

  • Inside the package directory, create a setup.py file. This file contains metadata about your package, such as its name, version, and author. Here's an example:

from setuptools import setup
 

 
setup(
 
name="mypackage",
 
version="1.0.0",
 
author="Your Name",
 
author_email="your.email@example.com",
 
description="A short description of your package",
 
long_description=open("README.md").read(),
 
long_description_content_type="text/markdown",
 
url="https://github.com/yourusername/mypackage",
 
packages=["mypackage"],
 
classifiers=[
 
"Programming Language :: Python :: 3",
 
"License :: OSI Approved :: MIT License",
 
"Operating System :: OS Independent",
 
],
 
)

  • Create a README.md file in the package directory. This file should contain a detailed description of your package, along with any usage instructions or examples.

  • Create a LICENSE file in the package directory. This file should contain the text of the license under which you're releasing your package.

  • Write your package code and save it in a Python file or a package directory under the same name as your package.

  • Optionally, include any dependencies your package requires in a requirements.txt file.

  • Build your package by running the following command in the package directory:

python setup.py sdist bdist_wheel

  • This will create a source distribution (sdist) and a binary distribution (bdist_wheel) of your package in a dist directory.

  • Upload your package to PyPI by running the following command:

twine upload dist/*

This will upload all the distributions in the dist directory to PyPI.

Once your package is uploaded to PyPI, it can be installed using pip. For example:

pip install mypackage

    90
    0