Python package on PyPI
Python package and publish to PyPI
In this tutorial am using the plotlyplus package as an example. The package can be found at: https://github.com/jgbrainstorm/plotlyplus. In this package, there are two python files, plotplyplus.py and plotlyplus_safe.py.
Step 1: Structure Your Package
On your local computer, create the folder structure as follow
plotlyplus/
|-- plotlyplus/
| |-- __init__.py
| |-- plotlyplus.py
| |-- plotlyplus_safe.py
|-- tests/
| |-- __init__.py
| |-- test_plotlyplus1.py # Optional but recommended
| |-- test_plotlyplus2.py # Optional but recommended
|-- setup.py
|-- README.md
|-- LICENSE
Step 2: Prepare the Package Files
__init__.py: Inside this file, you should import the relevant classes or functions from both plotlyplus.py and plotlyplus_safe.py:
from .plotlyplus import *
from .plotlyplus_safe import *
setup.py: Define metadata about your package. The install_requires section should list all external dependencies your package needs (like pandas and plotly).
Here is a template for the setup.py file:
from setuptools import setup, find_packages
setup(
name='plotlyplus',
version='0.1.0',
author='Your Name',
author_email='your.email@example.com',
description='Enhance pandas DataFrame with direct Plotly Express plotting functions, including safe versions',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
url='https://github.com/yourusername/plotlyplus',
packages=find_packages(),
classifiers=[
'Programming Language :: Python :: 3',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
],
install_requires=[
'pandas>=1.0.0',
'plotly>=4.0.0'
],
python_requires='>=3.6',
)
README.md and LICENSE: These files will contain the description of your package and the licensing information, respectively. See the git repo for example.
Step 3: Create account at PyPI if you have not yet.
In the account setting part, create the token. Follow the instruction there to use __token__ as user name and the token as password in the uploading step 5 below.
Step 4: Building Your Package locally.
Install required tools:
You need setuptools and wheel to create distribution packages.
python3 -m pip install --user --upgrade setuptools wheel
2.Create the distribution package:
Navigate to the directory containing setup.py and run:
python3 setup.py sdist bdist_wheel
This command should output a lot of text and once completed, will generate files in the dist directory.
Step 5: Uploading the Distribution to PyPI
The recommended tool for uploading your package to PyPI is twine because it securely authenticates you to PyPI over HTTPS.
Install Twine: python3 -m pip install --user --upgrade twine
Upload your package: python3 -m twine upload dist/*
You will be prompted for your PyPI username and password. Following the instruction in step 3 for the user name and password.
Step 6: Verifying upload
go to: https://pypi.org/project/plotlyplus/