Skip to main content

A small test package by setuptools

Project description

build a python package

This example repo:

  • uses build, setuptools and pyproject.toml to build package
  • uses setup.py to build package
  • uses twine to upload package
  • uses pip or setup.py to install package
pyenv shell 3.9.7
python -m venv venv
source venv/bin/activate

python -m pip install --upgrade pip build wheel setuptools twine

Ref: python package: https://packaging.python.org/en/latest/tutorials/packaging-projects/

Ref: setuptools guide: https://setuptools.pypa.io/en/latest/userguide/index.html

use native build

build with pyproject.toml

rm -rf build dist *.egg-info
python -m build

upload to PyPI

First, register PyPI account if not yet.

Upload to package index PyPI using twine:

# optional: --skip-existing
python -m twine upload --skip-existing dist/*

Uploading distributions to https://upload.pypi.org/legacy/
Enter your username: binlecode
Enter your password:
Uploading my_pkg_binlecode-0.0.3-py3-none-any.whl
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.0/9.0 kB  00:00  3.2 MB/s
Uploading my_pkg_binlecode-0.0.3.tar.gz
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.1/9.1 kB  00:00  5.7 MB/s

View at:
https://pypi.org/project/my-pkg-binlecode/0.0.3/

Alternatively, PyPI supports api token to replace interactive user credentials. First, get API token from pypi.org, then assign it to env var TWINE_USERNAME.

TWINE_USERNAME=token TWINE_PASSWORD=pypi-XXX... python -m twine upload --skip-existing dist/*

Replace env var setting with $HOME/.pypirc:

[pypi]
  username = __token__
  password = pypi-XXX...

use setuptools and setup.py

python setup.py sdist generates source distribution:

  • dist folder that contains <package-name>-<version>.tar.gz
  • .egg-info folder

A source distribution contains source code. That includes not only Python code but also the source code of any extension modules (usually in C or C++) bundled with the package. With source distributions, extension modules are compiled on the user’s side rather than the developer’s.

Source distributions also contain a bundle of metadata sitting in a directory called <package-name>.egg-info. Egg distribution format is being replaced by wheel distribution format.

bdist and bdist_wheel

bdist means build distribution, which is not necessarily binary.

python setup.py bdist generates:

  • dist/-..tar.gz, which is the default type of built distribution for the current platform
  • build/bdist.
  • build/lib folder that includes modules

python setup.py bdist_wheel generates:

  • dist/---.whl

A wheel file is essentially a zip archive with metadata of supported python versions and platforms.

Usually only source and wheel distributions should be generated and uploaded to package index (PyPI) for download and install.

rm -rf build dist *.egg-info
python setup.py sdist bdist_wheel

upload package to PyPI (First, register PyPI account if not yet.):

python -m twine upload --skip-existing dist/*

install

pip install <package-name> is a general way of installing package. pip always prefers wheel distribution over source distribution. If wheel distribution is available for the target platform, source distribution will be used to build package at client side.

To install from local, for example, the package project folder, pip install . installs the package from current folder.

Local install is handy for development mode, where -e/--editable flag is enabled to instruct python to track change in target package project folder: pip install --editable ..

pip install on wheel skips setup.py execution, if wheel is not available, pip has to:

  • download the source distribution and extract it
  • run python setup.py install on the extracted folder to build and install

Inside package folder, use --editable flag for development mode: python setup.py install --editable ..

pyproject.toml

A later PEP517 standard defines pyproject.toml as the new standard for packaging and distributing python modules.

If there's no pyproject.toml available, setuptools will fall back to setup.py file.

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_pkg_binlecode-0.1.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

my_pkg_binlecode-0.1.1-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file my_pkg_binlecode-0.1.1.tar.gz.

File metadata

  • Download URL: my_pkg_binlecode-0.1.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for my_pkg_binlecode-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8f200c60e671c2372d8002f4045bcccd0e6d1e8c4f44dbfa24ea648fe4036b48
MD5 317f2c677b0c91fef046cb479015c999
BLAKE2b-256 92c24035fef6f4cbde695310d8b8036d70f5b1846c7689c8e7c7f9bb4084b530

See more details on using hashes here.

File details

Details for the file my_pkg_binlecode-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for my_pkg_binlecode-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0e0c5519e69c72326a891342fccc10755ee0e9e919aa25ffc890139337094475
MD5 5d850ba99545a45b02a7354ae06713db
BLAKE2b-256 0ece37be83ea7c841ea569190b576131d45c0a50d998423abb97afe2af49ddab

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