Introduction
Jab hum apna Python project complete kar lete hain, to next step hota hai use distribute ya deploy karna. Packaging ka matlab hota hai apne code ko ek structured format me banana jisse koi aur easily install aur use kar sake. Deployment ka matlab hai apne code ko real-world me run karna — jaise kisi server, PyPI, ya app ke form me.
1️⃣ Project Structure
Ek professional Python package ka structure kuch aisa hota hai:
my_project/
│
├── my_module/
│ ├── __init__.py
│ ├── main.py
│ └── utils.py
│
├── setup.py
├── README.md
├── requirements.txt
└── LICENSE
__init__.py file package ke andar honi chahiye — ye batata hai ki ye directory ek Python module hai.
setup.py file me installation aur package information hoti hai.
2️⃣ setup.py File
Ye file Python ke packaging system ko batata hai ki project kaunse modules include kare aur unhe kaise install kare.
from setuptools import setup, find_packages
setup(
name='my_project',
version='1.0.0',
author='Aapka Naam',
author_email='you@example.com',
description='Ek simple Python package example',
packages=find_packages(),
install_requires=['requests'],
)
find_packages() automatically sab modules detect karta hai, aur install_requires me dependencies likhte hain.
3️⃣ requirements.txt
Ye file me sab libraries list hoti hain jo aapka project use karta hai.
requests==2.31.0
numpy==1.26.0
pandas==2.2.1
Install karne ke liye user likhta hai:
pip install -r requirements.txt
4️⃣ Package Banana (Build Karna)
Apna package build karne ke liye pehle dependencies install karo:
pip install setuptools wheel
Phir command run karo:
python setup.py sdist bdist_wheel
Ye command `.tar.gz` aur `.whl` files banayegi jo aap distribute kar sakte ho.
5️⃣ Local Installation
Apna package test karne ke liye local system me install karo:
pip install .
Ye command current directory ka package system me install kar degi.
6️⃣ PyPI me Upload Karna
Agar aap chahte ho ki koi bhi aapka package pip se install kar sake, to use PyPI pe upload karo.
pip install twine
twine upload dist/*
Ye command aapke package ko PyPI pe upload karti hai.
Uske baad koi bhi likh sakta hai: pip install my_project.
7️⃣ Python App Deploy Karna
Python apps ko deploy karne ke liye alag-alag platforms use hote hain:
- Flask/Django Apps: Render, Railway, AWS, Heroku.
- FastAPI: Host on Vercel ya DigitalOcean.
- CLI Tools: PyPI ke through installable.
# Example: Simple Flask App
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello from AIkiPadhai!"
if __name__ == '__main__':
app.run()
Flask app ko production me run karne ke liye gunicorn aur nginx use karte hain.
8️⃣ Versioning & Best Practices
- Semantic Versioning follow karo —
major.minor.patch(e.g., 1.0.0). - Always add
README.mdaurLICENSEfile. - GitHub repo maintain karo aur version control rakho.
- Automate build aur testing via GitHub Actions.