Python packages

Pypi

  • PyPi (Python Packaging index)
    • Repository called by pip install
    • The python utility twine helps uploading the package

Twine

  • Install
    1
    python -m pip install twine
  • Generate distribution
    1
    2
    3
    # go to the directory of the code you want to upload
    python -m setup.py sdist
    # the distribution has been created under a 'dist folder'
  • Upload
    1
    python -m twine upload dist/Helloworld-1.0.tar.gz

Package content

  • The directory name must be the same name you want to give to the package (consider chacking if the name is available in pypi)

    1. .py python code files
      • remove print instructions
      • you need to have a class, and a method inside the class
    2. setup.py code file
    3. __init__.py code file
    4. setup.cfg file
    5. README.md file
    6. License File
  • Example:

    1
    2
    3
    4
    5
    6
    7
    setup.py
    README.md
    LICENSE.txt (inside the folder)
    Helloworld (Folder)
    ||-- __init__.py (inside the folder)
    ||-- Helloworld.py (inside the folder)
    ||-- setup.cfg (inside the folder)

The setup.py file

  • Example:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    import io
    import os
    import sys
    import setuptools
    from setuptools import find_packages, setup, Command

    with open("README.md", "r") as fh:
    long_description = fh.read()

    setup(
    # directory name
    name = 'Helloworld',
    # same as above
    packages = ['Helloworld'],
    version = '1.0',
    license='GPLv3',
    description = "Python library that says Hello World when called.",
    # pass it to readme
    long_description=long_description,
    long_description_content_type="text/markdown",
    author = 'XXX',
    author_email = 'xxx@email.com',
    url = 'https://gitlab.com/xxx/Helloworld',
    # my package
    py_modules=['mypackage'],
    entry_points={'console_scripts': ['mycli=mymodule:cli'],},
    # tags
    keywords = ['Simple'],
    # scale it as the library grows
    classifiers=[
    'Development Status :: 4 - Beta',
    'Intended Audience :: Developers',
    'Topic :: Software Development :: Build Tools',
    'License :: GNU General Public License (GPL) :: GPLv3',
    'Programming Language :: Python :: 3.4',
    'Programming Language :: Python :: 3.5',
    'Programming Language :: Python :: 3.6'
    ],
    # minimum python version required
    python_requires='>=3.1',
    )

The __init__.py file

  • Either leave it empty, so python autogenerates, or import class name and method
    1
    from Helloworld.Helloworld import Helloworld

The setup.cfg file

  • It helps handling the metadata
    1
    2
    [metadata]
    description-file = README.md

The readme.md file

  • Markdown file presenting your package and giving installation instructions

The license.txt file