Skip to main content

<short description for the tool>

Project description

How to Build and Distribute a CLI Tool with Python

Requirements

  1. virtualenv: n isolated python env where we can test our tools/scripts etc while not installing them on our system globally.
pip install virtualenv
  1. wheel: a packaging mechanism for python tools/packages.
pip install wheel
  1. setuptools: a library that we will use to package our tool.
pip install --upgrade setuptools
  1. twine: used for distributing the packages to pypi/test.pypi.
pip install twine

Directory Structure & Initial Setup

MyTool
    - app
        - __init__.py
        - __main__.py
        - application.py
    - my_tool.py
    - setup.py
    - requirements.txt
    - README.md
    - LICENSE
    - MANIFEST.in

License

Choose a LICENSE for distributing the package to pick from https://choosealicense.com/.

Required packages

pip install -r requirements.txt

setup.py

  • py_modules

    • This is the place where we tell setuptools what modules it must be including while packaging the tool.
    • In our case, we are asking it to include my_tool.py and the whole app folder.
    • That is where the main functionalities of our tool will lie.
  • packages

    • This tells the places setuptools should look for in the current directory to find packages required for the tool.
    • find_packages() without any arguments means look through the whole directory to find any packages required.
  • entry_points

    • This is where we tell how our tool can be invoked and what function must be called when it is invoked.
    • [console_scripts] tells the setuptools that this tool will be used as a CLI tool (like pip or npm).
  • cooltool=my_tool:cli

    • this tells setuptools that whenever somebody types cooltool in the terminal, call the cli function inside my_tool.py.
    • If we have another function called start, and we want that to be invoked, we would write my_tool:start.

Testing the Tool

# Create a virtual env
virtualenv <virtual-env-name>

# Activate the created virtual env
source venv/bin/activate

# Install the CLI tool
python setup.py develop

# Verify the installation
which cooltool

Tests

# Tests
% cooltool hello
Hello World

% cooltool hello -n Python
Hello Python

Packaging and Distributing

python setup.py sdist bdist_wheel

Use twine to upload the binaries to test.pypi.org.

twine upload --repository testpypi --skip-existing dist/*

Now install it using pip.

pip install --index-url https://test.pypi.org/simple/ cooltool

Reference

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

convertool-1.0.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

convertool-1.0.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file convertool-1.0.0.tar.gz.

File metadata

  • Download URL: convertool-1.0.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.2

File hashes

Hashes for convertool-1.0.0.tar.gz
Algorithm Hash digest
SHA256 228cd653fc45afaf3902bd14e5024c2b070391fa54c852990d34677e0e663c99
MD5 75a496859e1f9fa045e6aefd7ae92c68
BLAKE2b-256 c0c998bb9834a52e3ae1eb9bb784bae185547e15674b01031ba4300969b03b35

See more details on using hashes here.

File details

Details for the file convertool-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: convertool-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.2

File hashes

Hashes for convertool-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6346db2e632f546c63fc05d1d03f93b052ba0c76dfbc703522fd735acd8abaaa
MD5 c9a46a095ebfd357fca801887ea00a7a
BLAKE2b-256 6fe8e802ad18896e6e4dd1fbdfd90d7abec01aa08746dd37c3d1f0898e1e6f3e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page