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 called PyPi (Python Package Index). PyPi maintains a directory service for contributed packages that is accessible by pip whenever the tool is used to install a new package.

Part 1

Begin by creating a folder for your package under src. Because the name of your package has to be unique, name it as "<your_name>lib", replacing <your_name> accordingly (don't use spaces).

src/<your_name>lib

Next, create the following file structure for your package.

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

Leave init.py blank. init.py is required to import the directory as a package, and can be left empty. Next, add the following code in mod.py, which will be the single module in your package.

def add_one(number):
    return number + 1

Add the following code in test.py. It will not be a proper test, just a validation that the 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 the validation, add the following in pyproject.toml, making sure to update <your_name>. TOML stands for Tom's Obvious Minimal Language and it is a configuration format.

[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 something about your project in README.md (it cannot be left blank).

Next, try to build your project using:

python3 -m build src

If you get an error saying "No module named build" it means that you need to install build using pip3. After a successful build, a dist folder will be created (under src).

Part 2

Next, it is time to publish your package so others can use. You need an account in https://pypi.org/. Then, go to https://pypi.org/manage/account/ and scroll down so you can click on the "Add API token" button. Give your token a name and select "Entire account (all projects)" as the scope. Copy the token and use it to run the following:

python3 -m twine upload -u __token__ -p <replace with your token> src/dist/*

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 run test.py.

More Information

PyPi user guide is available here.

recovery codes:

dd856bd3d545a0b8 04c8dc0bb27fe6ff a9c94004a5def8fd 537ee4d328687a1d 0f16f2d3e2bcb3f7 1748bc9343cb3737 3555aa069aa6ca49 35b37afd0d53c581

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

bsisay2001lib-0.0.1.tar.gz (2.2 kB view details)

Uploaded Source

Built Distribution

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

bsisay2001lib-0.0.1-py3-none-any.whl (2.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: bsisay2001lib-0.0.1.tar.gz
  • Upload date:
  • Size: 2.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for bsisay2001lib-0.0.1.tar.gz
Algorithm Hash digest
SHA256 5c2accb0ac0ef66c88af4cb75958fa9556696e773cc125251c1d096629430896
MD5 73006d042646306cb18e1e3934333791
BLAKE2b-256 3618d2275fd7c48e12327ac4b167c2ee5b1d9bb02d5af74471b69f04f267ee0a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bsisay2001lib-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 2.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for bsisay2001lib-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 63de158852839bbad48faa306a458914704b10ff4222b24b72c8b40d1748a30e
MD5 717ca4998c107f16d1e22d112df5f123
BLAKE2b-256 dfdfab5bb614758e35eab72f1d57900333e19a63fb6daffeed0a985a264be1bb

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