Skip to main content

How to create a package in python.

Project description

The goal of the exercise is to learn how to build a package and understand the magnificence of PyPi.

https://www.blog.pythonlibrary.org/2021/09/23/python-101-how-to-create-a-python-package/

https://www.geeksforgeeks.org/how-to-build-a-python-package/

https://www.freecodecamp.org/news/build-your-first-python-package/

https://betterscientificsoftware.github.io/python-for-hpc/tutorials/python-pypi-packaging/

1 - Bringing out the __init__s

Something that you’ll always find in every Python package is an init.py file. This file will tell Python to treat directories as modules (or sub-modules). Inside that package directory, alongside your python files, create a file called init.py. This file can be empty, and it denotes the directory as a python package. When you pip install, this directory will be installed and become importable.

Very simply, it can be empty or it will hold the names of all the methods in all the Python files that are in its immediate directory.

A typical init.py file has the following format:

from file import method

'method' is a function that is present in a file called 'file.py'

When building packages in Python, you are required to add an init.py file in every sub-directory in your package. These sub-directories are the sub-modules of your package.

2 - How to set up setup.py

Within the base-verysimplemodule folder (and in the same directory as our module verysimplemodule ), we need to add a setup.py file. This file is essential if you intend to build the actual module in question.

The setup.py file will contain information about your package, specifically the name of the package, its version, platform-dependencies and a whole lot more.

With that done, all we have to do next is run the following command in the same directory as base-verysimplemodule:

python setup.py sdist bdist_wheel

This will build all the necessary packages that Python will require. The sdist and bdist_wheel commands will create a source distribution and a wheel that you can later upload to PyPi.

3 - How to upload your package to PyPi

PyPi is the official Python repository where all Python packages are stored. You can think of it as the Github for Python Packages.

To make your Python package available to people around the world, you’ll need to have an account with PyPi.

With that done, we’re all set to upload our package on PyPi. Remember the source distribution and wheel that were built when we ran python setup.py ? Well, those are what will actually be uploaded to PyPi.

But before you do that, you need to install twine if you don’t already have it installed.

pip install twine.

twine upload dist/*

4

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

my_minipack-1.0.1.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

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

my_minipack-1.0.1-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file my_minipack-1.0.1.tar.gz.

File metadata

  • Download URL: my_minipack-1.0.1.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.7

File hashes

Hashes for my_minipack-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d0a2272716fc9eba6c48530dc5025819b9c48c6cf156d13a189e1e21fc26e24e
MD5 afebd421628472039ba97799abf6902a
BLAKE2b-256 2e6d1eb44f4439e699ba56b09d3610837f18986d17b10a15f1d028aa416ef534

See more details on using hashes here.

File details

Details for the file my_minipack-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: my_minipack-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.7

File hashes

Hashes for my_minipack-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e8ee225e545c48aa052c98916506c1d7d42ad5fc353240deaa5e4fc418517d34
MD5 44348369f7c0c4cfd080da0c92ed0574
BLAKE2b-256 31f35945ec0501ade7d7a587cc463d6d962ac61a9e44b58f0d70d20db82f3f7c

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