├── your_package/
│ ├── __**init__**.py
│ ├── [module1.py](<http://module1.py/>)
│ └── [module2.py](<http://module2.py/>)
├── .gitignore
├── LICENSE
├── [README.md](<http://readme.md/>)
├── requirements.txt
└── [setup.py](<http://setup.py/>)
“init.py”: This file makes Python treat the directories as containing packages. It can be empty or include package initializations.
This is the build script for setuptools
. It contains package metadata and dependencies.
Example from my ext2term
library available here.
from setuptools import setup, find_packages
setup(
name='ext2term',
version='0.2.0',
author='William Guerrand',
author_email='[email protected]',
description='A CLI and library for navigating ext2 filesystems',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
url='<https://github.com/yourusername/ext2term>',
packages=find_packages(),
include_package_data=True,
install_requires=[
# No dependencies, but if this where I'd put them, e.g.:
# 'requests >= 2.23.0',
],
classifiers=[
'Programming Language :: Python :: 3',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Topic :: Education',
'Topic :: System :: Filesystems',
'Intended Audience :: Developers',
'Intended Audience :: Education',
],
python_requires='>=3.4',
entry_points={
'console_scripts': [
'ext2term=ext2term.cli:main',
],
},
keywords='ext2 filesystem',
license='BSD 2-Clause License',
)
Testing library
Create virutal environment python -m venv venv
Activate environment by running source venv/bin/activate
in the same directory where the “venv” folder was created.
Run pip install -e .
to install library to virtual environment. Run again when changes made.
Register an account on PyPI: Go to https://pypi.org/account/register/ and fill out the form to create an account if you do not have one already.
Create a PyPI API Token: Log in to your PyPI account, go to the API token section under your account settings, and create a new API token. Remember to copy the token and keep it safe, as you will not be able to view it again.
Prepare your package: Make sure your setup.py
is properly filled out with all the necessary information (including the name
, version
, author
, author_email
, description
, etc.). If applicable, include other relevant files like MANIFEST.in
, LICENSE
, requirements.txt
, etc.
Build your package: Create the source distribution and wheel for your package using the following command in the root directory of your project:
python -m build
This will generate .tar.gz
and .whl
files in a new dist/
directory.
Install Twine: Twine is a utility for publishing Python packages to PyPI. If you haven’t already installed Twine, install it with:
pip install twine
Upload your package using Twine: Use Twine to upload your package to PyPI. Install with pip install twine
. Run twine upload dist/*
and paste PyPI API Token that was created in step 2.
Alternatively, you can set your token as an environment variable to avoid writing it down in the command line, leave TWINE_USERNAME as is:
**export TWINE_USERNAME=__token__
export TWINE_PASSWORD=your-api-token-here**
Once the upload is successful, your package should be live on PyPI and installable via pip install your-package-name
. Remember to increment your package version in setup.py
for future updates. Keep in mind that every new release must have a unique version number. Repeat the build and upload process for every new version you wish to publish.