Published on Monday, February 3, 2025
How to Create and Publish a Python Package
Posted by

Video Tutorial
Introduction
Publishing a Python package allows you to share your code with the community and make it easily installable using pip. In this guide, we'll walk through the process of creating and publishing a simple Python package named PyConsole, which enables printing messages in various colors to the console.
Step 1: Set Up Your Project
Start by creating a directory for your package and structuring it as follows:
pyconsole/
├── src/
│ ├── pyconsole/
│ │ ├── __init__.py
│ │ ├── console.py
├── .gitignore
├── LICENSE
├── README.md
├── setup.py
├── requirements.txt
1.1 Create the Project Directory
Start by creating a directory for your package:
mkdir pyconsole && cd pyconsole
1.2 Set Up a Virtual Environment
Create a virtual environment for dependency management:
python -m venv venv
source venv/bin/activate # On macOS/Linux
venv\Scripts\activate # On Windows
1.3 Install Dependencies
Install the required dependencies:
pip install colorama
Save the dependencies in a requirements.txt file:
pip freeze > requirements.txt
Step 2: Write Package Code
Inside the pyconsole directory, create console.py with the following content:
from colorama import init, Fore
class Console:
"""
A simple console utility for printing messages with color using colorama.
"""
def __init__(self):
init(autoreset=True)
def log(self, message: str, end: str = "\n") -> None:
print(message, end=end)
def info(self, message: str, end: str = "\n") -> None:
print(f"{Fore.BLUE}{message}", end=end)
def warn(self, message: str, end: str = "\n") -> None:
print(f"{Fore.YELLOW}{message}", end=end)
def error(self, message: str, end: str = "\n") -> None:
print(f"{Fore.RED}{message}", end=end)
def success(self, message: str, end: str = "\n") -> None:
print(f"{Fore.GREEN}{message}", end=end)
def secondary(self, message: str, end: str = "\n") -> None:
print(f"{Fore.LIGHTBLACK_EX}{message}", end=end)
Modify __init__.py to expose the Console class:
from .console import Console
console = Console()
Step 3: Writing a setup.py File
The setup.py file contains metadata for the package:
from setuptools import setup, find_packages
setup(
name="pyconsole",
version="1.0.0",
author="[Your Name]",
author_email="[Your Email]",
description="A simple console utility for printing messages with color using colorama",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
url="https://github.com/[username]/pyconsole",
packages=find_packages(where="src"),
package_dir={"": "src"},
install_requires=["colorama"],
classifiers=[
"Development Status :: 3 - Alpha",
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
)
Step 4 (Optional): Writing a README.md
The README.md file provides an overview of the package:
# Pyconsole
PyConsole is a Python package that enables printing messages in various colors to the console. It provides a `Console` class with colorized printing methods for messages in different colors.
## Installation
You can install Pyconsole directly from its GitHub repository using `pip`. Run the following command:
\`\`\`bash
pip install pyconsole
\`\`\`
## Usage
Once installed, you can import the Console class from the package and use its methods to print messages in different colors. Here's an example:
\`\`\`py
from pyconsole import console
# Example usage
console.info("This is an informational message")
console.warn("This is a warning message")
console.error("This is an error message")
console.success("This is a success message")
console.secondary("This is a secondary message")
\`\`\`
## Contributing
If you want to contribute to this project, feel free to fork the repository, make changes, and submit a pull request. Please make sure to review the [Contribution Guidelines](CONTRIBUTING.md) before contributing.
Step 5: Publishing to PyPI
5.1 Build the Package
Ensure build is installed:
pip install build
Run the following command:
python -m build
5.2 Upload to PyPI
Install twine:
pip install twine
Upload the package:
twine upload dist/*
Enter your PyPI credentials when prompted.
Step 6: Use Your Package
To use the package in another project, install it with:
pip install pyconsole
And import it:
from pyconsole import console
# Example usage
console.info("This is an informational message")
console.warn("This is a warning message")
console.error("This is an error message")
console.success("This is a success message")
console.secondary("This is a secondary message")
Useful Links
- 🖥️ Source Code: GitHub Repository
- 📦 PyPi Package: View on PyPi Registry
Conclusion
You’ve successfully created and published a Python package! Your package is now available for others to install and use. 🚀