Skip to content

Instantly share code, notes, and snippets.

@gene1wood
Last active June 28, 2023 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gene1wood/d7f7f11ed75bcd14ba26f21db6fe5cb3 to your computer and use it in GitHub Desktop.
Save gene1wood/d7f7f11ed75bcd14ba26f21db6fe5cb3 to your computer and use it in GitHub Desktop.
My process for creating a new python package
my-package-name
├── my_module_name
│   └── __init__.py
├── LICENSE.txt
├── README.rst
├── requirements.txt
└── pyproject.toml
  • Create a Python package name which is ideally all lower case with no dashes but may contain dashes (but not underscores)

    • Example : my-package-name
    • Set parent directory to package name
  • Create a Python module name which is ideally all lower case with no dashes or underscores but may contain underscores (but not dashes)

    • Example : my_module_name
    • Set child directory to module name : my-package-name/my_module_name
  • Copy pyproject.toml from https://github.com/pypa/sampleproject

  • Create a README.md

  • Choose a license ( http://choosealicense.com/ )

    • Get the "license" identifier value from the "Optional" section or the URL or SPDX ( https://spdx.org/licenses/ )
    • Copy paste the license text into LICENSE.txt
  • Update pyproject.toml values

    • name : my-package-name
    • version
    • description
    • keywords
    • authors
    • maintainers
    • Set classifiers ( https://pypi.python.org/pypi?%3Aaction=list_classifiers )
    • Set dependencies
    • Remove project.optional-dependencies unless you need them
    • Set project.urls
    • Remove project.scripts unless your project surfaces any command line scripts
    • Remove tool.setuptools as we're using hatch
    • Update build-system to use hatch
      • [build-system]
        requires = ["hatchling"]
        build-backend = "hatchling.build"
        
    • Create requirements.txt : ?
  • No need to create MANIFEST.in as it's only required in Python 2.6

  • Test installation

    virtualenv /tmp/testinstall
    . /tmp/testinstall/bin/activate
    pip install /path/to/mypackage
    
  • Create github repo with package name and commit

  • Ensure you have hatch installed

    python3 -m pip install --user --upgrade hatch
    
  • Build sign and upload to pypitest ( https://hynek.me/articles/sharing-your-labor-of-love-pypi-quick-and-dirty/ )

    hatch build
    hatch publish --repo https://test.pypi.org/legacy/
    
  • Verify everything looks good at https://testpypi.python.org/pypi/mypackagename/

  • Test installation in a virtualenv

    virtualenv /tmp/testinstall
    . /tmp/testinstall/bin/activate
    pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple mypackagename
    
  • Sign and upload to pypi

    hatch publish --repo  https://upload.pypi.org/legacy/
    
  • Verify everything looks good at https://pypi.python.org/pypi/mypackagename

  • Delete build artifcats

    rm -rf dist /tmp/testinstall
    

TODO

Explore using cookiecutter to automate all this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment