简体   繁体   English

typer 命令的输出在终端中不可见

[英]Output of typer command is not visible in terminal

I'm trying to use Typer package in python.我正在尝试在 python 中使用 Typer 包。

I wrote this code:我写了这段代码:

import typer


app = typer.Typer()


@app.command()
def hello(name: str):
    print(f"Hello {name}")

And I'm trying to run this with this command:我正在尝试使用以下命令运行它:

python main.py hello Patryk

And my terminal is not displaying anything.我的终端没有显示任何东西。 No errors, no text.没有错误,没有文本。

Python version: Python 3.9.13 (pipenv) System: Macos Ventura 13.1 Terminal: iTerm2 Build 3.4.18 and Macos default terminal Python 版本:Python 3.9.13 (pipenv) 系统:Macos Ventura 13.1 终端:iTerm2 Build 3.4.18 和 Macos 默认终端

I tried to not use pipenv and install all typer on my machine.我试图不使用 pipenv 并在我的机器上安装所有打字机。

I also tried this piece of code:我也试过这段代码:

import typer


def main(name: str):
    print(f"Hello {name}")


if __name__ == "__main__":
    typer.run(main)

And it works when I run it with this command:当我用这个命令运行它时它起作用了:

python main.py

I found solution.我找到了解决方案。 To make it work you need to add:要使其工作,您需要添加:

if __name__ == "__main__":
    app()

And have at least 2 commands, so your code has to look like this:并且至少有 2 个命令,因此您的代码必须如下所示:

import typer


app = typer.Typer()


@app.command()
def hello(name: str):
    print(f"Hello {name}")


@app.command()
def goodbye(name: str):
    print(f"Goodbye {name}")


if __name__ == "__main__":
    app()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM