[Python] How can I print a help message to the console from a pyproject GUI script?

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have a python project that uses a pyproject.toml file to define a GUI script:

    [project.gui-scripts]
    my-script = "my_project.cli:cli_run"


    I have a --help option that should print a help message to the console, but it doesn't, since the script is started in a separate process. If I use [project.scripts] instead, the problem is that it is tied to the console it is called from, which is also not desirable, since if I close the console, it also closes the GUI. So, is there a way to make it print a help message if I specify --help but still detach from the console otherwise?

    In case it helps, the project uses setuptools and wxpython.

    I tried:

    • Using [project.scripts] and multiprocessing: The console is still blocked until the GUI is closed, even if I don't join().
    • Using os.fork(): Doesn't work on Windows.
    • Using both [project.scripts] and [project.gui-scripts]: Behaves the same as just using [project.gui-scripts], regardless of the order.
    Minimal example


    Project structure:

    my-project
    ├── pyproject.toml
    └── src
    └── my_project
    └── cli.py


    pyproject.toml

    [build-system]
    requires = ["setuptools >= 64"]
    build-backend = "setuptools.build_meta"

    [project]
    name = "my-project"
    version = "0.1.0"

    dependencies = [
    "wxPython~=4.2.2",
    ]

    [project.gui-scripts]
    my-project = "my_project.cli:cli_run"


    cli.py

    import sys
    import wx

    def cli_run() -> None:
    if "--help" in sys.argv:
    print("Help message")
    return

    app = wx.App()
    frame = wx.Frame(None, title="Hello, World!")
    frame.Show()
    app.MainLoop()


    Run pip install . to install and then my-project to run the application.

    Continue reading...

Compartilhe esta Página