Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
PyPI

typer 0.21.1

pip install typer

Latest version

Released:

Typer, build great CLIs. Easy to code. Based on Python type hints.

Verified details

These details have beenverified by PyPI
Project links
GitHub Statistics
Maintainers
Avatar for tiangolo from gravatar.comtiangolo

Project description

Typer

Typer, build great CLIs. Easy to code. Based on Python type hints.

TestPublishCoveragePackage version


Documentation:https://typer.tiangolo.com

Source Code:https://github.com/fastapi/typer


Typer is a library for buildingCLI applications that users willlove using and developers willlove creating. Based on Python type hints.

It's also a command line tool to run scripts, automatically converting them to CLI applications.

The key features are:

FastAPI of CLIs

Typer isFastAPI's little sibling, it's the FastAPI of CLIs.

Installation

Create and activate avirtual environment and then installTyper:

$pipinstalltyper---> 100%Successfully installed typer rich shellingham

Example

The absolute minimum

  • Create a filemain.py with:
defmain(name:str):print(f"Hello{name}")

This script doesn't even use Typer internally. But you can use thetyper command to run it as a CLI application.

Run it

Run your application with thetyper command:

// Run your application$typermain.pyrun// You get a nice error, you are missing NAMEUsage: typer [PATH_OR_MODULE] run [OPTIONS] NAMETry 'typer [PATH_OR_MODULE] run --help' for help.╭─ Error ───────────────────────────────────────────╮│ Missing argument 'NAME'.                          │╰───────────────────────────────────────────────────╯// You get a --help for free$typermain.pyrun--helpUsage: typer [PATH_OR_MODULE] run [OPTIONS] NAMERun the provided Typer app.╭─ Arguments ───────────────────────────────────────╮│ *    name      TEXT  [default: None] [required]   |╰───────────────────────────────────────────────────╯╭─ Options ─────────────────────────────────────────╮│ --help          Show this message and exit.       │╰───────────────────────────────────────────────────╯// Now pass the NAME argument$typermain.pyrunCamilaHello Camila// It works! 🎉

This is the simplest use case, not even using Typer internally, but it can already be quite useful for simple scripts.

Note: auto-completion works when you create a Python package and run it with--install-completion or when you use thetyper command.

Use Typer in your code

Now let's start using Typer in your own code, updatemain.py with:

importtyperdefmain(name:str):print(f"Hello{name}")if__name__=="__main__":typer.run(main)

Now you could run it with Python directly:

// Run your application$pythonmain.py// You get a nice error, you are missing NAMEUsage: main.py [OPTIONS] NAMETry 'main.py --help' for help.╭─ Error ───────────────────────────────────────────╮│ Missing argument 'NAME'.                          │╰───────────────────────────────────────────────────╯// You get a --help for free$pythonmain.py--helpUsage: main.py [OPTIONS] NAME╭─ Arguments ───────────────────────────────────────╮│ *    name      TEXT  [default: None] [required]   |╰───────────────────────────────────────────────────╯╭─ Options ─────────────────────────────────────────╮│ --help          Show this message and exit.       │╰───────────────────────────────────────────────────╯// Now pass the NAME argument$pythonmain.pyCamilaHello Camila// It works! 🎉

Note: you can also call this same script with thetyper command, but you don't need to.

Example upgrade

This was the simplest example possible.

Now let's see one a bit more complex.

An example with two subcommands

Modify the filemain.py.

Create atyper.Typer() app, and create two subcommands with their parameters.

importtyperapp=typer.Typer()@app.command()defhello(name:str):print(f"Hello{name}")@app.command()defgoodbye(name:str,formal:bool=False):ifformal:print(f"Goodbye Ms.{name}. Have a good day.")else:print(f"Bye{name}!")if__name__=="__main__":app()

And that will:

  • Explicitly create atyper.Typer app.
    • The previoustyper.run actually creates one implicitly for you.
  • Add two subcommands with@app.command().
  • Execute theapp() itself, as if it was a function (instead oftyper.run).

Run the upgraded example

Check the new help:

$pythonmain.py--help Usage: main.py [OPTIONS] COMMAND [ARGS]...╭─ Options ─────────────────────────────────────────╮│ --install-completion          Install completion  ││                               for the current     ││                               shell.              ││ --show-completion             Show completion for ││                               the current shell,  ││                               to copy it or       ││                               customize the       ││                               installation.       ││ --help                        Show this message   ││                               and exit.           │╰───────────────────────────────────────────────────╯╭─ Commands ────────────────────────────────────────╮│ goodbye                                           ││ hello                                             │╰───────────────────────────────────────────────────╯// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion// You have 2 subcommands (the 2 functions): goodbye and hello

Now check the help for thehello command:

$pythonmain.pyhello--help Usage: main.py hello [OPTIONS] NAME╭─ Arguments ───────────────────────────────────────╮│ *    name      TEXT  [default: None] [required]   │╰───────────────────────────────────────────────────╯╭─ Options ─────────────────────────────────────────╮│ --help          Show this message and exit.       │╰───────────────────────────────────────────────────╯

And now check the help for thegoodbye command:

$pythonmain.pygoodbye--help Usage: main.py goodbye [OPTIONS] NAME╭─ Arguments ───────────────────────────────────────╮│ *    name      TEXT  [default: None] [required]   │╰───────────────────────────────────────────────────╯╭─ Options ─────────────────────────────────────────╮│ --formal    --no-formal      [default: no-formal] ││ --help                       Show this message    ││                              and exit.            │╰───────────────────────────────────────────────────╯// Automatic --formal and --no-formal for the bool option 🎉

Now you can try out the new command line application:

// Use it with the hello command$pythonmain.pyhelloCamilaHello Camila// And with the goodbye command$pythonmain.pygoodbyeCamilaBye Camila!// And with --formal$pythonmain.pygoodbye--formalCamilaGoodbye Ms. Camila. Have a good day.

Note: If your app only has one command, by default the command name isomitted in usage:python main.py Camila. However, when there are multiple commands, you mustexplicitly include the command name:python main.py hello Camila. SeeOne or Multiple Commands for more details.

Recap

In summary, you declareonce the types of parameters (CLI arguments andCLI options) as function parameters.

You do that with standard modern Python types.

You don't have to learn a new syntax, the methods or classes of a specific library, etc.

Just standardPython.

For example, for anint:

total:int

or for abool flag:

force:bool

And similarly forfiles,paths,enums (choices), etc. And there are tools to creategroups of subcommands, add metadata, extravalidation, etc.

You get: great editor support, includingcompletion andtype checks everywhere.

Your users get: automatic--help,auto-completion in their terminal (Bash, Zsh, Fish, PowerShell) when they install your package or when using thetyper command.

For a more complete example including more features, see theTutorial - User Guide.

Dependencies

Typer stands on the shoulders of a giant. Its only internal required dependency isClick.

By default it also comes with extra standard dependencies:

  • rich: to show nicely formatted errors automatically.
  • shellingham: to automatically detect the current shell when installing completion.
    • Withshellingham you can just use--install-completion.
    • Withoutshellingham, you have to pass the name of the shell to install completion for, e.g.--install-completion bash.

typer-slim

If you don't want the extra standard optional dependencies, installtyper-slim instead.

When you install with:

pipinstalltyper

...it includes the same code and dependencies as:

pipinstall"typer-slim[standard]"

Thestandard extra dependencies arerich andshellingham.

Note: Thetyper command is only included in thetyper package.

License

This project is licensed under the terms of the MIT license.

Project details

Verified details

These details have beenverified by PyPI
Project links
GitHub Statistics
Maintainers
Avatar for tiangolo from gravatar.comtiangolo

Release historyRelease notifications |RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more aboutinstalling packages.

Source Distribution

typer-0.21.1.tar.gz (110.4 kBview details)

UploadedSource

Built Distribution

Filter files by name, interpreter, ABI, and platform.

If you're not sure about the file name format, learn more aboutwheel file names.

Copy a direct link to the current filters

typer-0.21.1-py3-none-any.whl (47.4 kBview details)

UploadedPython 3

File details

Details for the filetyper-0.21.1.tar.gz.

File metadata

  • Download URL:typer-0.21.1.tar.gz
  • Upload date:
  • Size: 110.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for typer-0.21.1.tar.gz
AlgorithmHash digest
SHA256ea835607cd752343b6b2b7ce676893e5a0324082268b48f27aa058bdb7d2145d
MD5571a791e2806179e4220cc4135aa3388
BLAKE2b-25636bf8825b5929afd84d0dabd606c67cd57b8388cb3ec385f7ef19c5cc2202069

See more details on using hashes here.

File details

Details for the filetyper-0.21.1-py3-none-any.whl.

File metadata

  • Download URL:typer-0.21.1-py3-none-any.whl
  • Upload date:
  • Size: 47.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for typer-0.21.1-py3-none-any.whl
AlgorithmHash digest
SHA2567985e89081c636b88d172c2ee0cfe33c253160994d47bdfdc302defd7d1f1d01
MD512b8b15f8fcc99dd606d2918f3f71cab
BLAKE2b-256a01dd9257dd49ff2ca23ea5f132edf1281a0c4f9de8a762b9ae399b670a59235

See more details on using hashes here.

Supported by

AWS Cloud computing and Security SponsorDatadog MonitoringDepot Continuous IntegrationFastly CDNGoogle Download AnalyticsPingdom MonitoringSentry Error loggingStatusPage Status page

[8]ページ先頭

©2009-2026 Movatter.jp