Skip to main content

A small example package

Project description

Overview

The goal of this activity is to introduce you to Python’s packaging system, known as PyPI (Python Package Index). PyPI serves as a central repository for contributed Python packages and libraries. It is accessed by the pip tool whenever you install a new package.

Instructions

Part 1

Start by creating a folder for your package inside the src directory. Since package names must be unique, name your folder using the format <your_name>lib, replacing <your_name> with your actual name. Be sure to avoid spaces or special characters in the name.

src/<your_name>lib

Next, create the following file structure for your package.

src
|__<your_name>lib
   |__ __init__.py
   |__ mod.py
|__test.py
|__README.md
|__pyproject.toml

Leave the init.py file empty. This file is required to treat the directory as a Python package, but it doesn’t need to contain any code for basic functionality.

Next, create a file named mod.py inside your package folder. This will be the sole module in your package. Add the following code to it:

def add_one(number):
    return number + 1

Add the following code to test.py. This won’t be a formal test, but rather a simple validation to ensure that your package can be properly imported and used.

from <your_name>lib import mod

print(mod.add_one(5))

Make sure you are able to run test.py from src.

After validating that your package works, add the following configuration to pyproject.toml. Be sure to replace <your_name> with your actual name.

TOML stands for Tom’s Obvious Minimal Language, a simple and readable format used for configuration files. In Python packaging, pyproject.toml defines metadata and build instructions for your package.

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "<your_name>lib"
version = "0.0.1"
authors = [
  { name="Example Author", email="author@example.com" },
]
description = "A small example package"
readme = "README.md"
requires-python = ">=3.7"
classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",
]

[project.urls]
"Homepage" = "https://github.com/pypa/sampleproject"
"Bug Tracker" = "https://github.com/pypa/sampleproject/issues"

Write a brief description of your project in README.md. This file cannot be left blank—it should explain what your package does, its purpose, and any basic usage instructions or examples.

Next, try to build your project from the activity’s main folder using the following command:

python3 -m build src

If you encounter an error saying:

No module named 'build'

It means the build module is not installed. You can fix this by running:

pip3 install build

Part 2

Now it’s time to publish your package so others can use it.

  • First, create an account at https://pypi.org/.
  • Then, go to https://pypi.org/manage/account/ and scroll down to click the “Add API token” button.
  • Give your token a name and select “Entire account (all projects)” as the scope.
  • Copy the generated token.
  • To use the token for publishing, run the following command from the activity’s main folder:
python3 -m twine upload -u __token__ -p <replace with your token> src/dist/*

If you encounter an error saying:

No module named 'twine'

It means the twine module is not installed. You can fix this by running:

pip3 install twine

You can repeat this process multiple times if needed. For example, you may want to update your package. To do that, update the version of your package in pyproject.toml. Next, remove the dist folder and rebuild. Finally, upload the new version.

To test the installation and use of your package from PyPi, create a virtual environment and try to run test.py.

More Information

PyPi user guide is available here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zacklib-0.0.1.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zacklib-0.0.1-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file zacklib-0.0.1.tar.gz.

File metadata

  • Download URL: zacklib-0.0.1.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for zacklib-0.0.1.tar.gz
Algorithm Hash digest
SHA256 26f849bf29af6838f0586618fd318f1ce16e464737f68e02a43a03c84df94589
MD5 a3341cae9887c8db6f65f0d34f6eb0b4
BLAKE2b-256 c4a4eb1778fc4981528b6860d19a7b9908ba6a5437db7f2049d2f31d4a7ff79c

See more details on using hashes here.

File details

Details for the file zacklib-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: zacklib-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for zacklib-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 60b5f50ebbf6a1fa20ec49e0ed508a64ca77f225563a172d60a8efe502ecfdca
MD5 c72c3925c5b54a4946ca4e1db3f35f6e
BLAKE2b-256 6aa8b741c73b06a1928060723256406f8c07698ec691a5e589ac318081e73d27

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page