Python Package Index (PyPI)

Loading

1. What is PyPI?

The Python Package Index (PyPI) is the official repository of Python packages. It allows developers to publish, share, and install Python libraries and tools easily.

Website: https://pypi.org/
Package Manager: pip (used to install packages from PyPI)


2. Installing Packages from PyPI

To install a package from PyPI, use:

pip install package_name

Example: Install requests

pip install requests

To install a specific version:

pip install requests==2.26.0

To install the latest compatible version:

pip install "requests>=2.0,<3.0"

To install multiple packages from a file:

pip install -r requirements.txt

3. Searching for Packages on PyPI

Use the PyPI website to search for packages:
🔗 https://pypi.org/search/

Or use pip search (may require pip install pip_search):

pip_search requests

4. Managing Installed Packages

4.1 List Installed Packages

pip list

4.2 Show Package Details

pip show requests

4.3 Upgrade a Package

pip install --upgrade requests

4.4 Uninstall a Package

pip uninstall requests

5. Publishing Your Own Package to PyPI

5.1 Preparing Your Package

A Python package needs:

  • A structured directory
  • A setup.py or pyproject.toml
  • A README.md (optional but recommended)

Example Project Structure:

my_package/
│── my_package/
│ │── __init__.py
│ │── module1.py
│── pyproject.toml # Required for modern packaging
│── README.md

5.2 Creating pyproject.toml (Recommended for Modern Packages)

[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "my_package"
version = "0.1.0"
authors = [{name = "Your Name", email = "your@email.com"}]
description = "A sample Python package"

5.3 Building the Package

First, install required tools:

pip install build

Then, build the package:

python -m build

🔹 This creates a dist/ folder containing .tar.gz and .whl files.

5.4 Uploading to PyPI

Install Twine:

pip install twine

Upload the package:

twine upload dist/*

Enter your PyPI username and password when prompted.


6. Installing a Package from TestPyPI (Testing Environment)

To upload a package to TestPyPI (for testing before release):

twine upload --repository testpypi dist/*

To install from TestPyPI:

pip install --index-url https://test.pypi.org/simple/ my_package

7. Summary of Common PyPI Commands

CommandDescription
pip install package_nameInstall a package from PyPI
pip install package==1.0.0Install a specific package version
pip install -r requirements.txtInstall from a requirements file
pip listList installed packages
pip show package_nameShow details of an installed package
pip uninstall package_nameUninstall a package
pip install --upgrade package_nameUpgrade a package
python -m buildBuild a package for distribution
twine upload dist/*Upload a package to PyPI
pip install --index-url https://test.pypi.org/simple/ my_packageInstall from TestPyPI

Leave a Reply

Your email address will not be published. Required fields are marked *