Search code examples
pythonpython-poetrypython-click

Python Poetry build not packaging cli file


I created this project with the following structure:

repo_dir
 - module_name
 - cli.py
 - pyproject.toml

The cli.py file has code with functions using Click to call functions within the directory module_name.

The pyproject.toml file has the section tool.poetry.scripts to define the cli .py file:

[tool.poetry.scripts]
cli = 'cli:main'

Within the file cli.py I have this:

import click

@click.group()
def cli():
    ...


def main():
    cli(obj={})


if __name__ == '__main__':
    main()

However when I use poetry build and install the .whl file in an environment it raises an error:

Traceback (most recent call last):
  File "/usr/local/bin/cooh", line 5, in <module>
    from cli import main
ModuleNotFoundError: No module named 'cli'

I think for some reason when I use poetry build it is not packaging the cli.py file and in a new environment in a different directory this file does not exist hence it raises this error.


How to package a CLI using Poetry and Click?


Solution

  • Simplest solution I found is to move the cli.py file into the module_name directory:

    repo_dir
     - module_name
       - cli.py
     - pyproject.toml
    

    Then change the pyproject.toml to reflect the new structure:

    [tool.poetry.scripts]
    cli = 'module_name.cli:main'