This guide will help you publish the pyheroapi package to PyPI.
- Python 3.8+ installed
- PyPI account at https://pypi.org/account/register/
- Test PyPI account at https://test.pypi.org/account/register/
- API tokens from both PyPI and Test PyPI
pip install --upgrade pip
pip install build twine keyringpython -m keyring set https://test.pypi.org/legacy/ __token__
# Enter your Test PyPI API token when promptedpython -m keyring set https://upload.pypi.org/legacy/ __token__
# Enter your PyPI API token when promptedpip install -e .[dev]We've provided a convenient script to handle the entire process:
# Run all checks, tests, and build the package
python scripts/build_and_publish.py all
# Publish to Test PyPI first
python scripts/build_and_publish.py test-pypi
# After testing, publish to production PyPI
python scripts/build_and_publish.py pypi# Format code
black pyheroapi/ tests/ examples/
isort pyheroapi/ tests/ examples/
# Type checking
mypy pyheroapi/pytest tests/ -v --cov=pyheroapi# Clean previous builds
rm -rf dist/ build/ *.egg-info/
# Build the package
python -m build
# Check the package
twine check dist/*twine upload --repository testpypi dist/*# Create a new virtual environment for testing
python -m venv test_env
source test_env/bin/activate # On Windows: test_env\Scripts\activate
# Install from Test PyPI
pip install --index-url https://test.pypi.org/simple/ pyheroapi
# Test the installation
python -c "from pyheroapi import KiwoomClient; print('✅ Import successful')"twine upload dist/*Before publishing a new version:
- Update the version in
pyproject.toml - Update the changelog in
README.md - Create a git tag for the release:
git tag v0.1.0
git push origin v0.1.0- All tests pass
- Code is formatted and linted
- Version number updated
- Changelog updated
- Documentation is up to date
- Examples work correctly
- Package builds without errors
- Published to Test PyPI and tested
- Git tag created for release
-
Authentication Failed
- Make sure your API tokens are correctly configured
- Check that you're using the right repository URL
-
Package Already Exists
- PyPI doesn't allow overwriting existing versions
- Increment the version number in
pyproject.toml
-
Import Errors After Installation
- Check that all dependencies are correctly specified
- Verify the package structure is correct
-
Build Failures
- Ensure all required files are included in
MANIFEST.in - Check that
pyproject.tomlis correctly configured
- Ensure all required files are included in
- Check the PyPI documentation
- Visit the Python Packaging Guide
- Ask questions on Stack Overflow
- Never commit API tokens to version control
- Use environment variables or keyring for sensitive credentials
- Consider using GitHub Actions for automated publishing
- Regularly rotate your PyPI API tokens
Consider setting up GitHub Actions for automated testing and publishing:
- Create
.github/workflows/publish.yml - Set up PyPI API tokens as GitHub secrets
- Automatically publish on git tags
This will ensure consistent releases and reduce manual errors.