Skip to main content

打造命令行工具

创建虚拟环境(Virtual Environment)

$ uv venv
Using CPython 3.10.12 interpreter at: /usr/bin/python3
Creating virtual environment at: .venv
Activate with: source .venv/bin/activate
$ ls -a
. .. .venv

激活虚拟环境:

source .venv/bin/activate

查看虚拟环境:

which python

Install

创建安装文件:requirements.txt

typer==0.16.0
rich==13.7.1

安装:

uv pip install -r requirements.txt

main.py

创建第一个程序main.py:

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

虽然这个程序没有使用 Typer 功能,但是你仍然可以使用 Typer 来运行它:

typer main.py run Camila

文件内容为如下,实际使用 typer:

import typer


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


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

这样就可以用 python 来运行:

python main.py Camila