Skip to content

jtonic/handson-pycli

Repository files navigation

A simple python cli project

Steps

  • create pyenv/virtual env (python v.3.8.2)
  • setup.py
  • src/pycli
  • tests
  • setup in IntelliJ Idea
  • setup in vs code (I love its python development experience)
  • create a simple main (bootstrap)
  • create a simple test
  • Simple @dataclass
  • Create the source and wheel binary distributions
  • Load and parse a yaml file to a custom python type (class)
  • Include data files from packages in the source and binary distributions
  • simple cli command with click
  • simple cli to render a Jinja2 template
  • Include data files from file system in the source and binary distributions
  • run test with different pyenv/virtualenv configurations with tox
  • configure linters:
    • pylint (should I keep this lint?!)
    • flake8 (type hints)
    • mypy (type hints)
  • pytest-bdd gherkin example (test the support in idea)
  • create a python script for simple py-cli
  • publish the python script on pypi
  • create github actions to build the application run the tests

Howto

  • install the dev dependencies
    python -m pip install -e '.[dev]' # sh
    # OR
    python -m pip install -e .[dev] # shell
    # OR even better
    python -m pip install -r requirements.txt
  • Create the python script command

    • version 1
      # build the executable pycli
      $ python -m pip install -e .
    
      # restart shell
      $ pycli
    • version 2
      # Run a cli command
      $ python src/main/pycli/main.py hello --count=2
  • run tests

    pytest
    # OR
    pytest -s # to always show in the console the print calls
    # OR
    python -m pytest
    # OR
    python -m pytest --html=tests_report.html
  • run pycli command
    pycli --help
    # or
    pycli --count=3 --name=Tony
    # or
    pycli -c 3 -n Tony
    # or interactively
    pycli -c 3
    pycli
  • enable shell completion

    • Run the following in the terminal
eval "$(_PYCLI_COMPLETE=source pycli)"
  • or add it in .bashrc file

Note: Further about this at:

Python linting

pylint

  • run pylint
    pylint src/ --output-format=text
    # or run it with html report
    pylint src/ | pylint-json2html -o pylint.html

mypy (check type hints)

  • run mypy (with the option to install libraries/dependencies types)
    mypy .  --install-types

Caveats:

  • If python cannot be installed because curl doesn't support https protocol try this
    $ brew remove curl
    $ brew remove curl-openssl
    $ echo 'export PATH="/usr/local/opt/curl-openssl/bin:$PATH"' >> ~/.zshrc
    $ source ~/.zshrc
    # or
    $ echo 'export PATH="/usr/local/opt/curl-openssl/bin:$PATH"' >> ~/.bash_profile
    $ source ~/.bash_profile
  • If somehow, after the pyenv virtualenv python installation the virtual env is not shown into terminal (after shell restart) then:

    • check to see if you have the following lines in ~/.bash_profile or ~/.zshrc
export PATH="$(pyenv root)/shims:$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
# eval "$(pyenv virtualenv-init -)"

References

Further reading

About

Simple python cli application

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published