question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to manually specify command_path for CLI Python modules

See original GitHub issue

My Python code must be run as a module. So the code where I define cli command is in __main__.py, and I run the script with python -m foo.

The problem is that Click displays the help instructions as:

Usage: __main__.py [OPTIONS] COMMAND [ARGS]...

But I would like:

Usage: python -m foo [OPTIONS] COMMAND [ARGS]...

I tried this:

@click.group(name='python -m foo')
def main():
    pass

which as no effect,

and also this:

@click.group()
@click.pass_context
def main(ctx):
    ctx.command_path = 'python -m foo'

which fails with AttributeError: can't set attribute.

The ability to manually specify the command_path is necessary to work with CLI Python modules or other particular scenarios.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
roipoussierecommented, Sep 18, 2019

For anyone looking for the same behavior, here is a simple working example:

import click

@click.group()
def main():
    pass

if __name__ == '__main__':
    main(prog_name='python -m foo')

@davidism in the example you provided I was not able to guess what exactly was the imported cli.

0reactions
roipoussierecommented, Sep 18, 2019

Thanks for your help. Is there an example of that in the docs?

Read more comments on GitHub >

github_iconTop Results From Across the Web

1. Command line and environment
path . Defines the user base directory , which is used to compute the path of the user site-packages directory and Distutils installation...
Read more >
add PYTHONPATH during command line module run
Basically sys.path is a list with all the search paths for python modules. It is initialized by the interpreter. The content of PYTHONPATH...
Read more >
Build Command-Line Interfaces With Python's argparse
This program implements a minimal CLI by manually processing the ... This module allows you to define the arguments and options that your ......
Read more >
Configuring basic system settings Red Hat Enterprise Linux 8
Press the Automatic button, and select Manual from the displayed list. Press the Show button next to the protocol you want to configure...
Read more >
Python Package Installation on Windows
How to manually install a Python package: Download the package and extract it into a local directory. 2a. If the package came with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found