Skip to content
Myr

📝 Recommend uv projects by default in docs (#1903)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sebastián Ramírez · committed by GitHub · parent aa09d51 · Browse files · 55 files+514 added−1516 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

55 files+514 added−1516 removed

  • other:+514 added−1516 removed

55 files+514 added−1516 removed

modified:docs/tutorial/arguments/default.md+8 added-8 removed
modified:docs/tutorial/arguments/envvar.md+24 added-11 removed
1# CLI Arguments with Environment Variables23You can also configure a *CLI argument* to read a value from an environment variable if it is not provided in the command line as a *CLI argument*.45An **environment variable** (also known as an **env var**) is a value that lives outside of your Python code, in the operating system, and can be read by your application and other programs.67/// tip87You can learn more about environment variables in the [Environment Variables](../../environment-variables.md) page.9Read the [Environment Variables guide](https://tiangolo.com/guides/environment-variables/) for a detailed, cross-platform explanation.1011///1213To do that, use the `envvar` parameter for `typer.Argument()`:
5 unmodified lines
19<div class="termy">2021```console22// Check the help21$ python main.py --help23$ uv run python main.py --help2425Usage: main.py [OPTIONS] [name]2627Arguments:
2 unmodified lines
30Options:31 --help Show this message and exit.3233// Call it without a CLI argument32$ python main.py34$ uv run python main.py3536Hello Mr. World3738// Now pass a value for the CLI argument37$ python main.py Czernobog39$ uv run python main.py Czernobog4041Hello Mr. Czernobog4243// And now use the environment variable42$ AWESOME_NAME=Wednesday python main.py44$ AWESOME_NAME=Wednesday uv run python main.py4546Hello Mr. Wednesday4748// CLI arguments take precedence over env vars47$ AWESOME_NAME=Wednesday python main.py Czernobog49$ AWESOME_NAME=Wednesday uv run python main.py Czernobog5051Hello Mr. Czernobog52```5354</div>5556/// tip | Windows PowerShell5758In PowerShell, set the environment variable first and then run the command:5960```console61$ $Env:AWESOME_NAME = "Wednesday"62$ uv run python main.py63```6465///6667## Multiple environment variables6869You are not restricted to a single environment variable, you can declare a list of environment variables that could be used to get a value if it was not passed in the command line:70
4 unmodified lines
75<div class="termy">7677```console78// Check the help66$ python main.py --help79$ uv run python main.py --help8081Usage: main.py [OPTIONS] [name]8283Arguments:
2 unmodified lines
86Options:87 --help Show this message and exit.8889// Try the first env var77$ AWESOME_NAME=Wednesday python main.py90$ AWESOME_NAME=Wednesday uv run python main.py9192Hello Mr. Wednesday9394// Try the second env var82$ GOD_NAME=Anubis python main.py95$ GOD_NAME=Anubis uv run python main.py9697Hello Mr. Anubis98```99
10 unmodified lines
110<div class="termy">111112```console113//Check the help101$ python main.py --help114$ uv run python main.py --help115116// It won't show the env var117Usage: main.py [OPTIONS] [name]118
3 unmodified lines
122Options:123 --help Show this message and exit.124125// But it will still be able to use it113$ AWESOME_NAME=Wednesday python main.py126$ AWESOME_NAME=Wednesday uv run python main.py127128Hello Mr. Wednesday129```130
1 unmodified line
modified:docs/tutorial/arguments/help.md+8 added-8 removed
17 unmodified lines
1819<div class="termy">2021```console22$ python main.py --help22$ uv run python main.py --help2324// Check the section with Arguments below 🚀25Usage: main.py [OPTIONS] {name}26
16 unmodified lines
4344<div class="termy">4546```console47$ python main.py --help47$ uv run python main.py --help4849// Notice that we have the help text from the docstring and also the Arguments 📝50Usage: main.py [OPTIONS] {name}51
18 unmodified lines
7071<div class="termy">7273```console74$ python main.py --help74$ uv run python main.py --help7576// Notice the [default: World] 🔍77Usage: main.py [OPTIONS] [name]78
16 unmodified lines
9596<div class="termy">9798```console99$ python main.py --help99$ uv run python main.py --help100101// Notice the there's no [default: World] now 🔥102Usage: main.py [OPTIONS] [name]103
18 unmodified lines
122123<div class="termy">124125```console126$ python main.py --help126$ uv run python main.py --help127128Usage: main.py [OPTIONS] [name]129130Arguments:
36 unmodified lines
167168<div class="termy">169170```console171$ python main.py --help171$ uv run python main.py --help172173Usage: main.py [OPTIONS] [✨username✨]174175Arguments:
19 unmodified lines
195196<div class="termy">197198```console199$ python main.py --help199$ uv run python main.py --help200201<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] {name} [lastname] [age] </b>202<b> </b>203 Say hi to 'name' very gently, like Dirk.
32 unmodified lines
236237<div class="termy">238239```console240$ python main.py --help240$ uv run python main.py --help241242// Notice there's no Arguments section at all 🔥243Usage: main.py [OPTIONS] [name]244
23 unmodified lines
modified:docs/tutorial/arguments/optional.md+4 added-4 removed
80 unmodified lines
8182<div class="termy">8384```console85$ python main.py85$ uv run python main.py8687Usage: main.py [OPTIONS] {name}88Try "main.py --help" for help.89
39 unmodified lines
129<div class="termy">130131```console132// First check the help133$ python main.py --help133$ uv run python main.py --help134135Usage: main.py [OPTIONS] [name]136137Arguments:
18 unmodified lines
156<div class="termy">157158```console159// With no CLI argument160$ python main.py160$ uv run python main.py161162Hello World!163164// With one optional CLI argument165$ python main.py Camila165$ uv run python main.py Camila166167Hello Camila168```169
69 unmodified lines
modified:docs/tutorial/commands/arguments.md+3 added-3 removed
modified:docs/tutorial/commands/callback.md+8 added-8 removed
30 unmodified lines
31<div class="termy">3233```console34// Check the help35$ python main.py --help35$ uv run python main.py --help3637// Notice the main help text, extracted from the callback function: "Manage users in the awesome CLI app."38Usage: main.py [OPTIONS] COMMAND [ARGS]...39
11 unmodified lines
5152// Check the new top level CLI option --verbose5354// Try it normally55$ python main.py create Camila55$ uv run python main.py create Camila5657Creating user: Camila5859// And now with --verbose60$ python main.py --verbose create Camila60$ uv run python main.py --verbose create Camila6162Will write verbose output63About to create a user64Creating user: Camila65Just created a user6667// Notice that --verbose belongs to the callback, it has to go before create or delete ⛔️68$ python main.py create --verbose Camila68$ uv run python main.py create --verbose Camila6970Usage: main.py create [OPTIONS] {username}71Try "main.py create --help" for help.72
14 unmodified lines
8788<div class="termy">8990```console91$ python main.py create Camila91$ uv run python main.py create Camila9293Running a command94Creating user: Camila95```
12 unmodified lines
108109<div class="termy">110111```console112$ python main.py create Camila112$ uv run python main.py create Camila113114// Notice that the message is the one from new_callback()115Override callback, running a command116Creating user: Camila
15 unmodified lines
132133<div class="termy">134135```console136$ python main.py --help136$ uv run python main.py --help137138// Notice all the help text extracted from the callback docstring139Usage: main.py [OPTIONS] COMMAND [ARGS]...140
11 unmodified lines
152Commands:153 create154155// And it just works as normally156$ python main.py create Camila156$ uv run python main.py create Camila157158Creating user: Camila159```160
1 unmodified line
modified:docs/tutorial/commands/context.md+7 added-7 removed
19 unmodified lines
2021<div class="termy">2223```console24$ python main.py create Camila24$ uv run python main.py create Camila2526// We get the message from the callback27About to execute command: create28Creating user: Camila2930$ python main.py delete Camila30$ uv run python main.py delete Camila3132// We get the message from the callback, this time with delete33About to execute command: delete34Deleting user: Camila
15 unmodified lines
5051<div class="termy">5253```console54$ python main.py54$ uv run python main.py5556// The callback is executed, we don't get the default help message57Initializing database5859// Try with a command60$ python main.py create Camila60$ uv run python main.py create Camila6162// The callback is still executed63Initializing database64Creating user: Camila
15 unmodified lines
8081<div class="termy">8283```console84$ python main.py84$ uv run python main.py8586// The callback is executed87Initializing database8889// Check it with a subcommand90$ python main.py create Camila90$ uv run python main.py create Camila9192// This time the callback is not executed93Creating user: Camila94```
12 unmodified lines
107108<div class="termy">109110```console111$ python main.py --name Camila --city Berlin111$ uv run python main.py --name Camila --city Berlin112113Got extra arg: --name114Got extra arg: Camila115Got extra arg: --city
10 unmodified lines
modified:docs/tutorial/commands/help.md+17 added-17 removed
10 unmodified lines
11<div class="termy">1213```console14// Check the new help15$ python main.py --help15$ uv run python main.py --help1617Usage: main.py [OPTIONS] COMMAND [ARGS]...1819 Awesome CLI user manager.
11 unmodified lines
3132// Now the commands have inline help 🎉3334// Check the help for create35$ python main.py create --help35$ uv run python main.py create --help3637Usage: main.py create [OPTIONS] {username}3839 Create a new user with username.
4 unmodified lines
44Options:45 --help Show this message and exit.4647// Check the help for delete48$ python main.py delete --help48$ uv run python main.py delete --help4950Usage: main.py delete [OPTIONS] {username}5152 Delete a user with username.
7 unmodified lines
60 --force / --no-force Force deletion without confirmation. [required]61 --help Show this message and exit.6263// Check the help for delete-all64$ python main.py delete-all --help64$ uv run python main.py delete-all --help6566Usage: main.py delete-all [OPTIONS]6768 Delete ALL users in the database.
4 unmodified lines
73 --force / --no-force Force deletion without confirmation. [required]74 --help Show this message and exit.7576// Check the help for init77$ python main.py init --help77$ uv run python main.py init --help7879Usage: main.py init [OPTIONS]8081 Initialize the users database.
23 unmodified lines
105<div class="termy">106107```console108// Check the help109$ python main.py --help109$ uv run python main.py --help110111// Notice it uses the help passed to @app.command()112Usage: main.py [OPTIONS] COMMAND [ARGS]...113
24 unmodified lines
138139<div class="termy">140141```console142$ python main.py --help142$ uv run python main.py --help143144<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] COMMAND [ARGS]... </b>145<b> </b>146<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────────────────────╮</font>
16 unmodified lines
163164<div class="termy">165166```console167$ python main.py delete --help167$ uv run python main.py delete --help168169<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py delete [OPTIONS] {username} </b>170<b> </b>171 <font color="#F92672">(deprecated) </font>
20 unmodified lines
192193<div class="termy">194195```console196$ python main.py crate196$ uv run python main.py crate197198<font color="#C4A000">Usage: </font>main.py [OPTIONS] COMMAND [ARGS]...199<font color="#AAAAAA">Try </font><font color="#22436D">&apos;main.py </font><font color="#4C6A8A"><b>--help</b></font><font color="#22436D">&apos;</font><font color="#AAAAAA"> for help.</font>200<font color="#CC0000">╭─ Error ───────────────────────────────────────────────────────────╮</font>
34 unmodified lines
235236<div class="termy">237238```console239$ python main.py create --help239$ uv run python main.py create --help240241<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py create [OPTIONS] {username} </b>242<b> </b>243 <font color="#A6E22E"><b>Create</b></font> a new <i>shiny</i> user. ✨
14 unmodified lines
258259<div class="termy">260261```console262$ python main.py delete --help262$ uv run python main.py delete --help263264<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py delete [OPTIONS] {username} </b>265<b> </b>266 <font color="#F92672"><b>Delete</b></font> a user with <i>username</i>.
25 unmodified lines
292293<div class="termy">294295```console296$ python main.py create --help296$ uv run python main.py create --help297298<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py create [OPTIONS] {username} </b>299<b> </b>300 <b>Create</b> a new <i>shiny</i> user. ✨
19 unmodified lines
320321<div class="termy">322323```console324$ python main.py delete --help324$ uv run python main.py delete --help325326<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py delete [OPTIONS] {username} </b>327<b> </b>328 <b>Delete</b> a user with <i>username</i>.
33 unmodified lines
362363<div class="termy">364365```console366$ python main.py --help366$ uv run python main.py --help367368<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] COMMAND [ARGS]... </b>369<b> </b>370<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────────────────────╮</font>
39 unmodified lines
410411<div class="termy">412413```console414$ python main.py create --help414$ uv run python main.py create --help415416<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py create [OPTIONS] {username} [lastname] </b>417<b> </b>418 <font color="#A6E22E">Create</font> a new user. ✨
25 unmodified lines
444445<div class="termy">446447```console448$ python main.py --help448$ uv run python main.py --help449450<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] COMMAND [ARGS]... </b>451<b> </b>452<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────────────────────╮</font>
26 unmodified lines
479480<div class="termy">481482```console483$ python main.py --help483$ uv run python main.py --help484485<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] {username} </b>486<b> </b>487 <font color="#A6E22E">Create</font> a new user. ✨
17 unmodified lines
modified:docs/tutorial/commands/index.md+5 added-5 removed
64 unmodified lines
65<div class="termy">6667```console68// Check the help69$ python main.py --help69$ uv run python main.py --help7071Usage: main.py [OPTIONS] COMMAND [ARGS]...7273Options:
5 unmodified lines
79 create80 delete8182// Test them83$ python main.py create83$ uv run python main.py create8485Creating user: Hiro Hamada8687$ python main.py delete87$ uv run python main.py delete8889Deleting user: Hiro Hamada9091// Now we have 2 commands! 🎉
22 unmodified lines
114<div class="termy">115116```console117// Check the help without having to type --help118$ python main.py118$ uv run python main.py119120Usage: main.py [OPTIONS] COMMAND [ARGS]...121122Options:
22 unmodified lines
145<div class="termy">146147```console148// Check the help149$ python main.py --help149$ uv run python main.py --help150151Usage: main.py [OPTIONS] COMMAND [ARGS]...152153Options:
18 unmodified lines
modified:docs/tutorial/commands/name.md+2 added-2 removed
22 unmodified lines
2324<div class="termy">2526```console27$ python main.py --help27$ uv run python main.py --help2829Usage: main.py [OPTIONS] COMMAND [ARGS]...3031Options:
5 unmodified lines
37 create38 delete3940// Test it41$ python main.py create Camila41$ uv run python main.py create Camila4243Creating user: Camila44```45
11 unmodified lines
modified:docs/tutorial/commands/one-or-multiple.md+10 added-10 removed
8 unmodified lines
9<div class="termy">1011```console12// Without a CLI argument13$ python main.py13$ uv run python main.py1415Usage: main.py [OPTIONS] {name}16Try "main.py --help" for help.1718Error: Missing argument 'name'.1920// With the 'name' CLI argument21$ python main.py Camila21$ uv run python main.py Camila2223Hello Camila2425// Asking for help26$ python main.py --help26$ uv run python main.py --help2728Usage: main.py [OPTIONS] {name}2930Options:
19 unmodified lines
50<div class="termy">5152```console53// Check the help54$ python main.py --help54$ uv run python main.py --help5556Usage: main.py [OPTIONS] COMMAND [ARGS]...5758Options:
5 unmodified lines
64 create65 delete6667// Test the commands68$ python main.py create68$ uv run python main.py create6970Creating user: Hiro Hamada7172$ python main.py delete72$ uv run python main.py delete7374Deleting user: Hiro Hamada75```76
12 unmodified lines
89<div class="termy">9091```console92// Check the help93$ python main.py --help93$ uv run python main.py --help9495// Notice the single command create96Usage: main.py [OPTIONS] COMMAND [ARGS]...97
5 unmodified lines
103Commands:104 create105106// Try it107$ python main.py create107$ uv run python main.py create108109Creating user: Hiro Hamada110```111
9 unmodified lines
121122<div class="termy">123124```console125$ python main.py --help125$ uv run python main.py --help126127// Notice the help text from the docstring128Usage: main.py [OPTIONS] COMMAND [ARGS]...129
9 unmodified lines
139Commands:140 create141142// And it still works the same, the callback does nothing143$ python main.py create143$ uv run python main.py create144145Creating user: Hiro Hamada146```147
1 unmodified line
modified:docs/tutorial/commands/options.md+7 added-7 removed
50 unmodified lines
51<div class="termy">5253```console54// Check the command create55$ python main.py create Camila55$ uv run python main.py create Camila5657Creating user: Camila5859// Now test the command delete60$ python main.py delete Camila60$ uv run python main.py delete Camila6162# Are you sure you want to delete the user? [y/n]: $ y6364Deleting user: Camila6566$ python main.py delete Wade66$ uv run python main.py delete Wade6768# Are you sure you want to delete the user? [y/n]: $ n6970Operation cancelled7172// And finally, the command delete-all73// Notice it doesn't have CLI arguments, only a CLI option7475$ python main.py delete-all75$ uv run python main.py delete-all7677# Are you sure you want to delete ALL users? [y/n]: $ y7879Deleting all users8081$ python main.py delete-all81$ uv run python main.py delete-all8283# Are you sure you want to delete ALL users? [y/n]: $ n8485Operation cancelled8687// And if you pass the --force CLI option, it doesn't need to confirm8889$ python main.py delete-all --force89$ uv run python main.py delete-all --force9091Deleting all users9293// And init that doesn't take any CLI parameter94$ python main.py init94$ uv run python main.py init9596Initializing user database97```98
1 unmodified line
modified:docs/tutorial/multiple-values/arguments-with-multiple-values.md+5 added-5 removed
9 unmodified lines
1011<div class="termy">1213```console14$ python main.py ./index.md ./first-steps.md woohoo!14$ uv run python main.py ./index.md ./first-steps.md woohoo!1516This file exists: index.md17woohoo!18This file exists: first-steps.md
25 unmodified lines
44<div class="termy">4546```console47// Check the help48$ python main.py --help48$ uv run python main.py --help4950Usage: main.py [OPTIONS] [names]...5152Arguments:
2 unmodified lines
55Options:56 --help Show this message and exit.5758// Use it with its defaults59$ python main.py59$ uv run python main.py6061Hello Harry62Hello Hermione63Hello Ron6465// If you pass an invalid number of arguments you will get an error66$ python main.py Draco Hagrid66$ uv run python main.py Draco Hagrid6768Error: Argument 'names' takes 3 values6970// And if you pass the exact number of values it will work correctly71$ python main.py Draco Hagrid Dobby71$ uv run python main.py Draco Hagrid Dobby7273Hello Draco74Hello Hagrid75Hello Dobby
3 unmodified lines
modified:docs/tutorial/multiple-values/multiple-options.md+6 added-6 removed
14 unmodified lines
15<div class="termy">1617```console18// The default value is 'None'19$ python main.py19$ uv run python main.py2021No provided users (raw input = None)22Aborted!2324// Now pass a user25$ python main.py --user Camila25$ uv run python main.py --user Camila2627Processing user: Camila2829// And now try with several users30$ python main.py --user Camila --user Rick --user Morty30$ uv run python main.py --user Camila --user Rick --user Morty3132Processing user: Camila33Processing user: Rick34Processing user: Morty
11 unmodified lines
4647<div class="termy">4849```console50$ python main.py50$ uv run python main.py5152The sum is 05354// Try with some numbers55$ python main.py --number 255$ uv run python main.py --number 25657The sum is 2.05859// Try with some numbers60$ python main.py --number 2 --number 3 --number 4.560$ uv run python main.py --number 2 --number 3 --number 4.56162The sum is 9.563```64
1 unmodified line
modified:docs/tutorial/multiple-values/options-with-multiple-values.md+4 added-4 removed
54 unmodified lines
55<div class="termy">5657```console58// check the help59$ python main.py --help59$ uv run python main.py --help6061// Notice the <str int boolean>62Usage: main.py [OPTIONS]63
1 unmodified line
65 --user <str int boolean>...66 --help Show this message and exit.6768// Now try it69$ python main.py --user Camila 50 yes69$ uv run python main.py --user Camila 50 yes7071The username Camila has 50 coins72And this user is a wizard!7374// With other values75$ python main.py --user Morty 3 no75$ uv run python main.py --user Morty 3 no7677The username Morty has 3 coins7879// Try with invalid values (not enough)80$ python main.py --user Camila 5080$ uv run python main.py --user Camila 508182Error: Option '--user' requires 3 arguments83```84
1 unmodified line
modified:docs/tutorial/options/callback-and-context.md+3 added-3 removed
21 unmodified lines
2223<div class="termy">2425```console26$ python main.py --name Camila26$ uv run python main.py --name Camila2728Hello Camila2930$ python main.py --name Rick30$ uv run python main.py --name Rick3132Usage: main.py [OPTIONS]3334// We get the error from the callback
134 unmodified lines
169170<div class="termy">171172```console173$ python main.py --name Camila173$ uv run python main.py --name Camila174175Validating param: name176Hello Camila177```
28 unmodified lines
modified:docs/tutorial/options/help.md+6 added-6 removed
27 unmodified lines
2829<div class="termy">3031```console32$ python main.py --help32$ uv run python main.py --help3334Usage: main.py [OPTIONS] {name}3536 Say hi to 'name', optionally with a --lastname.
27 unmodified lines
6465<div class="termy">6667```console68$ python main.py --help68$ uv run python main.py --help6970<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] {name} </b>71<b> </b>72 Say hi to 'name', optionally with a <font color="#A1EFE4"><b>--lastname</b></font>.
35 unmodified lines
108109<div class="termy">110111```console112$ python main.py112$ uv run python main.py113114Hello Wade Wilson115116// Show the help117$ python main.py --help117$ uv run python main.py --help118119Usage: main.py [OPTIONS]120121Options:
15 unmodified lines
137138<div class="termy">139140```console141$ python main.py141$ uv run python main.py142143Hello Wade Wilson144145// Show the help146$ python main.py --help146$ uv run python main.py --help147148Usage: main.py [OPTIONS]149150Options:
7 unmodified lines
modified:docs/tutorial/options/name.md+12 added-12 removed
45 unmodified lines
4647<div class="termy">4849```console50$ python main.py --help50$ uv run python main.py --help5152// Notice the --name instead of --user-name53Usage: main.py [OPTIONS]54
1 unmodified line
56 --name <str> [required]57 --help Show this message and exit.5859// Try it60$ python main.py --name Camila60$ uv run python main.py --name Camila6162Hello Camila63```64
129 unmodified lines
194<div class="termy">195196```console197// Check the help198$ python main.py --help198$ uv run python main.py --help199200// Notice the two CLI option names -n and --name201Usage: main.py [OPTIONS]202
1 unmodified line
204 -n, --name <str> [required]205 --help Show this message and exit.206207// Try the short version208$ python main.py -n Camila208$ uv run python main.py -n Camila209210Hello Camila211```212
9 unmodified lines
222223<div class="termy">224225```console226$ python main.py --help226$ uv run python main.py --help227228// Notice there's no --name nor --user-name, only -n229Usage: main.py [OPTIONS]230
1 unmodified line
232 -n <str> [required]233 --help Show this message and exit.234235// Try it236$ python main.py -n Camila236$ uv run python main.py -n Camila237238Hello Camila239```240
9 unmodified lines
250251<div class="termy">252253```console254$ python main.py --help254$ uv run python main.py --help255256// Notice that we have the long version --user-name back257// and we also have the short version -n258Usage: main.py [OPTIONS]
2 unmodified lines
261 -n, --user-name <str> [required]262 --help Show this message and exit.263264// Try it265$ python main.py --user-name Camila265$ uv run python main.py --user-name Camila266267Hello Camila268269// And try the short version270$ python main.py -n Camila270$ uv run python main.py -n Camila271```272273</div>274
15 unmodified lines
290291<div class="termy">292293```console294$ python main.py --help294$ uv run python main.py --help295296// We now have short versions -n and -f297// And also long versions --name and --formal298Usage: main.py [OPTIONS]
3 unmodified lines
302 -f, --formal303 --help Show this message and exit.304305// Try the short versions306$ python main.py -n Camila -f306$ uv run python main.py -n Camila -f307308Good day Ms. Camila.309310// And try the 2 short versions together311// See how -n has to go last, to be able to get the value312$ python main.py -fn Camila312$ uv run python main.py -fn Camila313314Good day Ms. Camila.315```316
1 unmodified line
modified:docs/tutorial/options/password.md+2 added-2 removed
7 unmodified lines
89<div class="termy">1011```console12$ python main.py Camila12$ uv run python main.py Camila1314// It prompts for the email15# Email: $ camila@example.com16# Repeat for confirmation: $ camila@example.com
19 unmodified lines
3637<div class="termy">3839```console40$ python main.py Camila40$ uv run python main.py Camila4142// It prompts for the password, but doesn't show anything when you type43# Password: $44# Repeat for confirmation: $
7 unmodified lines
modified:docs/tutorial/options/prompt.md+4 added-4 removed
8 unmodified lines
9<div class="termy">1011```console12// Call it with the 'name' CLI argument13$ python main.py Camila13$ uv run python main.py Camila1415// It asks for the missing CLI option --lastname16# Lastname: $ Gutiérrez17
13 unmodified lines
31<div class="termy">3233```console34// Call it with the 'name' CLI argument35$ python main.py Camila35$ uv run python main.py Camila3637// It uses the custom prompt38# Please tell me your last name: $ Gutiérrez39
16 unmodified lines
5657<div class="termy">5859```console60$ python main.py60$ uv run python main.py6162// Your app will first prompt for the project name, and then for the confirmation63# Project name: $ Old Project64# Repeat for confirmation: $ Old Project6566Deleting project Old Project6768// If the user doesn't type the same, receives an error and a new prompt69$ python main.py69$ uv run python main.py7071# Project name: $ Old Project72# Repeat for confirmation: $ New Spice73
11 unmodified lines
modified:docs/tutorial/options/required.md+3 added-3 removed
41 unmodified lines
42<div class="termy">4344```console45// Pass the 'name' CLI argument46$ python main.py Camila46$ uv run python main.py Camila4748// We didn't pass the now required --lastname CLI option49Usage: main.py [OPTIONS] {name}50Try "main.py --help" for help.5152Error: Missing option '--lastname'.5354// Now update it to pass the required --lastname CLI option55$ python main.py Camila --lastname Gutiérrez55$ uv run python main.py Camila --lastname Gutiérrez5657Hello Camila Gutiérrez5859// And if you check the help60$ python main.py --help60$ uv run python main.py --help6162Usage: main.py [OPTIONS] {name}6364Arguments:
10 unmodified lines
modified:docs/tutorial/options/version.md+5 added-5 removed
25 unmodified lines
2627<div class="termy">2829```console30$ python main.py --help30$ uv run python main.py --help3132// We get a --version, and don't get an awkward --no-version 🎉33Usage: main.py [OPTIONS]34
3 unmodified lines
38 --help Show this message and exit.394041// We can call it normally42$ python main.py --name Camila42$ uv run python main.py --name Camila4344Hello Camila4546// And we can get the version47$ python main.py --version47$ uv run python main.py --version4849Awesome CLI Version: 0.1.05051// Because we exit in the callback, we don't get a "Hello World" message after the version 🚀
11 unmodified lines
6364<div class="termy">6566```console67$ python main.py --name Rick --version67$ uv run python main.py --name Rick --version6869Only Camila is allowed70Aborted!71```
30 unmodified lines
102103<div class="termy">104105```console106$ python main.py --name Rick --version106$ uv run python main.py --name Rick --version107108// Now we only get the version, and the name is not used109Awesome CLI Version: 0.1.0110```
2 unmodified lines
modified:docs/tutorial/parameter-types/bool.md+15 added-15 removed
16 unmodified lines
17<div class="termy">1819```console20// Check the help21$ python main.py --help21$ uv run python main.py --help2223// Notice there's only --force, we no longer have --no-force24Usage: main.py [OPTIONS]25
1 unmodified line
27 --force28 --help Show this message and exit.2930// Try it:31$ python main.py31$ uv run python main.py3233Not forcing3435// Now add --force36$ python main.py --force36$ uv run python main.py --force3738Forcing operation3940// And --no-force no longer exists ⛔️41$ python main.py --no-force41$ uv run python main.py --no-force4243Usage: main.py [OPTIONS]44Try "main.py --help" for help.45
19 unmodified lines
65<div class="termy">6667```console68// Check the help69$ python main.py --help69$ uv run python main.py --help7071// Notice the --accept / --reject72Usage: main.py [OPTIONS]73
1 unmodified line
75 --accept / --reject76 --help Show this message and exit.7778// Try it79$ python main.py79$ uv run python main.py8081I don't know what you want yet8283// Now pass --accept84$ python main.py --accept84$ uv run python main.py --accept8586Accepting!8788// And --reject89$ python main.py --reject89$ uv run python main.py --reject9091Rejecting!92```93
12 unmodified lines
106<div class="termy">107108```console109// Check the help110$ python main.py --help110$ uv run python main.py --help111112// Notice the -f, --force / -F, --no-force113Usage: main.py [OPTIONS]114
1 unmodified line
116 -f, --force / -F, --no-force [default: no-force]117 --help Show this message and exit.118119// Try with the short name -f120$ python main.py -f120$ uv run python main.py -f121122Forcing operation123124// Try with the short name -F125$ python main.py -F125$ uv run python main.py -F126127Not forcing128```129
20 unmodified lines
150<div class="termy">151152```console153// Check the help154$ python main.py --help154$ uv run python main.py --help155156// Notice the / -d, --demo157Usage: main.py [OPTIONS]158
1 unmodified line
160 / -d, --demo [default: True]161 --help Show this message and exit.162163// Try it164$ python main.py164$ uv run python main.py165166Running in production167168// Now pass --demo169$ python main.py --demo169$ uv run python main.py --demo170171Running demo172173// And the short version174$ python main.py -d174$ uv run python main.py -d175176Running demo177```178
1 unmodified line
modified:docs/tutorial/parameter-types/datetime.md+5 added-5 removed
15 unmodified lines
1617<div class="termy">1819```console20$ python main.py --help20$ uv run python main.py --help2122Usage: main.py [OPTIONS] {birth}:<%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S>2324Arguments:
2 unmodified lines
27Options:28 --help Show this message and exit.2930// Pass a datetime31$ python main.py 1956-01-31T10:00:0031$ uv run python main.py 1956-01-31T10:00:003233Interesting day to be born: 1956-01-31 10:00:0034Birth hour: 103536// An invalid date37$ python main.py july-19-198937$ uv run python main.py july-19-19893839Usage: main.py [OPTIONS] {birth}:<%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S>40Try 'main.py --help' for help.41
30 unmodified lines
72<div class="termy">7374```console75// ISO dates work76$ python main.py 1969-10-2976$ uv run python main.py 1969-10-297778Launch will be at: 1969-10-29 00:00:007980// But the strange custom format also works81$ python main.py 10/29/196981$ uv run python main.py 10/29/19698283Launch will be at: 1969-10-29 00:00:0084```85
1 unmodified line
modified:docs/tutorial/parameter-types/enum.md+13 added-13 removed
15 unmodified lines
1617<div class="termy">1819```console20$ python main.py --help20$ uv run python main.py --help2122// Notice the predefined values <simple|conv|lstm>23Usage: main.py [OPTIONS]24
1 unmodified line
26 --network <simple|conv|lstm> [default: simple]27 --help Show this message and exit.2829// Try it30$ python main.py --network conv30$ uv run python main.py --network conv3132Training neural network of type: conv3334// Invalid value35$ python main.py --network capsule35$ uv run python main.py --network capsule3637Usage: main.py [OPTIONS]38Try "main.py --help" for help.3940Error: Invalid value for '--network': 'capsule' is not one of 'simple', 'conv', 'lstm'.4142// Note that enums are case sensitive by default43$ python main.py --network CONV43$ uv run python main.py --network CONV4445Usage: main.py [OPTIONS]46Try "main.py --help" for help.47
13 unmodified lines
61<div class="termy">6263```console64// Notice the upper case CONV65$ python main.py --network CONV65$ uv run python main.py --network CONV6667Training neural network of type: conv6869// A mix also works70$ python main.py --network LsTm70$ uv run python main.py --network LsTm7172Training neural network of type: lstm73```74
9 unmodified lines
8485<div class="termy">8687```console88$ python main.py --help88$ uv run python main.py --help8990// Notice the default values being shown91Usage: main.py [OPTIONS]92
1 unmodified line
94 --groceries <Eggs|Bacon|Cheese> [default: Eggs, Cheese]95 --help Show this message and exit.9697// Try it with the default values98$ python main.py98$ uv run python main.py99100Buying groceries: Eggs, Cheese101102// Try it with a single value103$ python main.py --groceries "Eggs"103$ uv run python main.py --groceries "Eggs"104105Buying groceries: Eggs106107// Try it with multiple values108$ python main.py --groceries "Eggs" --groceries "Bacon"108$ uv run python main.py --groceries "Eggs" --groceries "Bacon"109110Buying groceries: Eggs, Bacon111```112
7 unmodified lines
120121<div class="termy">122123```console124$ python main.py --help124$ uv run python main.py --help125126// Notice the predefined values <simple|conv|lstm>127Usage: main.py [OPTIONS]128
1 unmodified line
130 --network <simple|conv|lstm> [default: simple]131 --help Show this message and exit.132133// Try it134$ python main.py --network conv134$ uv run python main.py --network conv135136Training neural network of type: conv137138// Invalid value139$ python main.py --network capsule139$ uv run python main.py --network capsule140141Usage: main.py [OPTIONS]142Try "main.py --help" for help.143
4 unmodified lines
modified:docs/tutorial/parameter-types/file.md+7 added-7 removed
59 unmodified lines
60// Add another line to the config to test it61$ echo "some more settings" >> config.txt6263// Now run your program64$ python main.py --config config.txt64$ uv run python main.py --config config.txt6566Config line: some settings6768Config line: some more settings
20 unmodified lines
8990<div class="termy">9192```console93$ python main.py --config text.txt93$ uv run python main.py --config text.txt9495Config written9697// Check the contents of the file
26 unmodified lines
124125<div class="termy">126127```console128$ python main.py --file lena.jpg128$ uv run python main.py --file lena.jpg129130Processed bytes total: 512131Processed bytes total: 1024132Processed bytes total: 1536
18 unmodified lines
151152<div class="termy">153154```console155$ python main.py --file binary.dat155$ uv run python main.py --file binary.dat156157Binary file written158159// Check the binary file was created
49 unmodified lines
209210<div class="termy">211212```console213$ python main.py --config config.txt213$ uv run python main.py --config config.txt214215Config line written216217// Run your program a couple more times to see how it appends instead of overwriting218$ python main.py --config config.txt218$ uv run python main.py --config config.txt219220Config line written221222$ python main.py --config config.txt222$ uv run python main.py --config config.txt223224Config line written225226// Check the contents of the file, it should have each of the 3 lines appended
8 unmodified lines
modified:docs/tutorial/parameter-types/index.md+3 added-3 removed
19 unmodified lines
2021<div class="termy">2223```console24$ python main.py --help24$ uv run python main.py --help2526// Notice how --age is an 'int' (integer) and --height-meters is a 'float'27Usage: main.py [OPTIONS] {name}28
6 unmodified lines
35 --female / --no-female [default: female]36 --help Show this message and exit.3738// Call it with CLI parameters39$ python main.py Camila --age 15 --height-meters 1.70 --female39$ uv run python main.py Camila --age 15 --height-meters 1.70 --female4041// All the data has the correct Python type42name is Camila, of type: class 'str'43--age is 15, of type: class 'int'44--height-meters is 1.7, of type: class 'float'45--female is True, of type: class 'bool'4647// And if you pass an incorrect type48$ python main.py Camila --age 15.348$ uv run python main.py Camila --age 15.34950Usage: main.py [OPTIONS] {name}51Try "main.py --help" for help.52
10 unmodified lines
modified:docs/tutorial/parameter-types/number.md+14 added-14 removed
11 unmodified lines
1213<div class="termy">1415```console16$ python main.py --help16$ uv run python main.py --help1718// Notice the extra range information in the help text for --age and --score19Usage: main.py [OPTIONS] {ID}20
5 unmodified lines
26 --score <float range> [default: 0; x<=100]27 --help Show this message and exit.2829// Pass all the CLI parameters30$ python main.py 5 --age 20 --score 9030$ uv run python main.py 5 --age 20 --score 903132ID is 533--age is 2034--score is 90.03536// Pass an invalid ID37$ python main.py 100237$ uv run python main.py 10023839Usage: main.py [OPTIONS] {ID}40Try "main.py --help" for help.4142Error: Invalid value for 'ID': 1002 is not in the range 0<=x<=1000.4344// Pass an invalid age45$ python main.py 5 --age 1545$ uv run python main.py 5 --age 154647Usage: main.py [OPTIONS] {ID}48Try "main.py --help" for help.4950Error: Invalid value for '--age': 15 is not in the range x>=18.5152// Pass an invalid score53$ python main.py 5 --age 20 --score 100.553$ uv run python main.py 5 --age 20 --score 100.55455Usage: main.py [OPTIONS] {ID}56Try "main.py --help" for help.5758Error: Invalid value for '--score': 100.5 is not in the range x<=100.5960// But as we didn't specify a minimum score, this is accepted61$ python main.py 5 --age 20 --score -561$ uv run python main.py 5 --age 20 --score -56263ID is 564--age is 2065--score is -5.0
14 unmodified lines
80<div class="termy">8182```console83// ID doesn't have clamp, so it shows an error84$ python main.py 100284$ uv run python main.py 10028586Usage: main.py [OPTIONS] {ID}87Try "main.py --help" for help.8889Error: Invalid value for 'ID': 1002 is not in the range 0<=x<=1000.9091// But --rank and --score use clamp92$ python main.py 5 --rank 11 --score -592$ uv run python main.py 5 --rank 11 --score -59394ID is 595--rank is 1096--score is 0
14 unmodified lines
111<div class="termy">112113```console114// Check it115$ python main.py115$ uv run python main.py116117Verbose level is 0118119// Now use one --verbose120$ python main.py --verbose120$ uv run python main.py --verbose121122Verbose level is 1123124// Now 3 --verbose125$ python main.py --verbose --verbose --verbose125$ uv run python main.py --verbose --verbose --verbose126127Verbose level is 3128129// And with the short name130$ python main.py -v130$ uv run python main.py -v131132Verbose level is 1133134// And with the short name 3 times135$ python main.py -v -v -v135$ uv run python main.py -v -v -v136137Verbose level is 3138139// As short names can be put together, this also works140$ python main.py -vvv140$ uv run python main.py -vvv141142Verbose level is 3143```144
1 unmodified line
modified:docs/tutorial/parameter-types/path.md+7 added-7 removed
12 unmodified lines
13<div class="termy">1415```console16// No config17$ python main.py17$ uv run python main.py1819Aborted.20No config file2122// Pass a config that doesn't exist23$ python main.py --config config.txt23$ uv run python main.py --config config.txt2425The config doesn't exist2627// Now create a quick config28$ echo "some settings" > config.txt2930// And try again31$ python main.py --config config.txt31$ uv run python main.py --config config.txt3233Config file contents: some settings3435// And with a directory36$ python main.py --config ./36$ uv run python main.py --config ./3738Config is a directory, will use all its config files39```40
24 unmodified lines
6566<div class="termy">6768```console69$ python main.py --config config.txt69$ uv run python main.py --config config.txt7071Usage: main.py [OPTIONS]72Try "main.py --help" for help.73
2 unmodified lines
76// Now create a quick config77$ echo "some settings" > config.txt7879// And try again80$ python main.py --config config.txt80$ uv run python main.py --config config.txt8182Config file contents: some settings8384// And with a directory85$ python main.py --config ./85$ uv run python main.py --config ./8687Usage: main.py [OPTIONS]88Try "main.py --help" for help.89
17 unmodified lines
modified:docs/tutorial/parameter-types/uuid.md+2 added-2 removed
30 unmodified lines
31<div class="termy">3233```console34// Pass a valid UUID v435$ python main.py d48edaa6-871a-4082-a196-4daab372d4a135$ uv run python main.py d48edaa6-871a-4082-a196-4daab372d4a13637User ID is d48edaa6-871a-4082-a196-4daab372d4a138UUID version is: 43940// An invalid value41$ python main.py 7479706572-72756c657341$ uv run python main.py 7479706572-72756c65734243Usage: main.py [OPTIONS] {user_id}44Try "main.py --help" for help.45
4 unmodified lines
modified:docs/tutorial/subcommands/add-typer.md+8 added-8 removed
15 unmodified lines
1617<div class="termy">1819```console20$ python items.py create Wand20$ uv run python items.py create Wand2122Creating item: Wand23```24
11 unmodified lines
3637<div class="termy">3839```console40$ python users.py create Camila40$ uv run python users.py create Camila4142Creating user: Camila43```44
28 unmodified lines
73<div class="termy">7475```console76// Check the help77$ python main.py --help77$ uv run python main.py --help7879Usage: main.py [OPTIONS] COMMAND [ARGS]...8081Options:
15 unmodified lines
97<div class="termy">9899```console100// Check the help for items101$ python main.py items --help101$ uv run python main.py items --help102103// It shows its own commands (subcommands): create, delete, sell104Usage: main.py items [OPTIONS] COMMAND [ARGS]...105
5 unmodified lines
111 delete112 sell113114// Try it115$ python main.py items create Wand115$ uv run python main.py items create Wand116117Creating item: Wand118119$ python main.py items sell Vase119$ uv run python main.py items sell Vase120121Selling item: Vase122```123
9 unmodified lines
133134<div class="termy">135136```console137$ python main.py users --help137$ uv run python main.py users --help138139Usage: main.py users [OPTIONS] COMMAND [ARGS]...140141Options:
3 unmodified lines
145 create146 delete147148// Try it149$ python main.py users create Camila149$ uv run python main.py users create Camila150151Creating user: Camila152```153
15 unmodified lines
modified:docs/tutorial/subcommands/callback-override.md+4 added-4 removed
13 unmodified lines
1415<div class="termy">1617```console18$ python main.py users create Camila18$ uv run python main.py users create Camila1920// Notice the first message is not created by the command function but by the callback21Running a users command22Creating user: Camila
13 unmodified lines
3637<div class="termy">3839```console40$ python main.py users create Camila40$ uv run python main.py users create Camila4142Running a users command43Creating user: Camila44```
16 unmodified lines
6162<div class="termy">6364```console65$ python main.py users create Camila65$ uv run python main.py users create Camila6667// Notice the message from the new callback68Callback override, running users command69Creating user: Camila
17 unmodified lines
8788<div class="termy">8990```console91$ python users create Camila91$ uv run python users create Camila9293// Notice the message from the callback added in add_typer()94I have the high land! Running users command95Creating user: Camila
3 unmodified lines
modified:docs/tutorial/subcommands/name-and-help.md+16 added-16 removed
18 unmodified lines
19<div class="termy">2021```console22// Check the main help23$ python main.py --help23$ uv run python main.py --help2425Usage: main.py [OPTIONS] COMMAND [ARGS]...2627Options:
4 unmodified lines
32Commands:33 users Manage users in the app.3435// Check the help for the users command36$ python main.py users --help36$ uv run python main.py users --help3738Usage: main.py users [OPTIONS] COMMAND [ARGS]...3940 Manage users in the app.
55 unmodified lines
96<div class="termy">9798```console99// Check the main help100$ python main.py --help100$ uv run python main.py --help101102// Notice the help text "Manage users in the app."103Usage: main.py [OPTIONS] COMMAND [ARGS]...104
5 unmodified lines
110Commands:111 users Manage users in the app.112113// Check the help for the users command114$ python main.py users --help114$ uv run python main.py users --help115116// Notice the main description: "Manage users in the app."117Usage: main.py users [OPTIONS] COMMAND [ARGS]...118
31 unmodified lines
150<div class="termy">151152```console153// Check the main help154$ python main.py --help154$ uv run python main.py --help155156// Notice the help text "Manage users in the app."157Usage: main.py [OPTIONS] COMMAND [ARGS]...158
5 unmodified lines
164Commands:165 users Manage users in the app.166167// Check the help for the users command168$ python main.py users --help168$ uv run python main.py users --help169170// Notice the main description: "Manage users in the app."171Usage: main.py users [OPTIONS] COMMAND [ARGS]...172
21 unmodified lines
194<div class="termy">195196```console197// Check the main help198$ python main.py --help198$ uv run python main.py --help199200// Notice the help text "Manage users in the app."201Usage: main.py [OPTIONS] COMMAND [ARGS]...202
5 unmodified lines
208Commands:209 users Manage users in the app.210211// Check the help for the users command212$ python main.py users --help212$ uv run python main.py users --help213214// Notice the main description: "Manage users in the app."215Usage: main.py users [OPTIONS] COMMAND [ARGS]...216
25 unmodified lines
242<div class="termy">243244```console245// Check the main help246$ python main.py --help246$ uv run python main.py --help247248// Check the command new-users and its help text249Usage: main.py [OPTIONS] COMMAND [ARGS]...250
5 unmodified lines
256Commands:257 new-users I have the highland! Create some users.258259// Now check the help for the new-users command260$ python main.py new-users --help260$ uv run python main.py new-users --help261262// Notice the help text263Usage: main.py new-users [OPTIONS] COMMAND [ARGS]...264
55 unmodified lines
320<div class="termy">321322```console323// Check the main help324$ python main.py --help324$ uv run python main.py --help325326// Notice the command name is exp-users and the help text is "Explicit help."327Usage: main.py [OPTIONS] COMMAND [ARGS]...328
5 unmodified lines
334Commands:335 exp-users Explicit help.336337// Check the help for the exp-users command338$ python main.py exp-users --help338$ uv run python main.py exp-users --help339340// Notice the main help text341Usage: main.py exp-users [OPTIONS] COMMAND [ARGS]...342
23 unmodified lines
366<div class="termy">367368```console369// Check the help370$ python main.py --help370$ uv run python main.py --help371372// The help text is now "Help from callback for users.".373Usage: main.py [OPTIONS] COMMAND [ARGS]...374
5 unmodified lines
380Commands:381 users Help from callback for users.382383// Check the users command help384$ python main.py users --help384$ uv run python main.py users --help385386// Notice the main help text387Usage: main.py users [OPTIONS] COMMAND [ARGS]...388
21 unmodified lines
410<div class="termy">411412```console413// Check the help414$ python main.py --help414$ uv run python main.py --help415416// Notice the command name cake-sith-users and the new help text "Unlimited powder! Eh, users."417Usage: main.py [OPTIONS] COMMAND [ARGS]...418
5 unmodified lines
424Commands:425 cake-sith-users Unlimited powder! Eh, users.426427// And check the help for the command cake-sith-users428$ python main.py cake-sith-users --help428$ uv run python main.py cake-sith-users --help429430// Notice the main help text431Usage: main.py cake-sith-users [OPTIONS] COMMAND [ARGS]...432
27 unmodified lines
modified:docs/tutorial/subcommands/nested-subcommands.md+18 added-18 removed
18 unmodified lines
19<div class="termy">2021```console22// Check the help23$ python reigns.py --help23$ uv run python reigns.py --help2425Usage: reigns.py [OPTIONS] COMMAND [ARGS]...2627Options:
5 unmodified lines
33 conquer34 destroy3536// Try it37$ python reigns.py conquer Cintra37$ uv run python reigns.py conquer Cintra3839Conquering reign: Cintra4041$ python reigns.py destroy Mordor41$ uv run python reigns.py destroy Mordor4243Destroying reign: Mordor44```45
10 unmodified lines
56<div class="termy">5758```console59// Check the help60$ python towns.py --help60$ uv run python towns.py --help6162Usage: towns.py [OPTIONS] COMMAND [ARGS]...6364Options:
5 unmodified lines
70 found71 burn7273// Try it74$ python towns.py found "New Asgard"74$ uv run python towns.py found "New Asgard"7576Founding town: New Asgard7778$ python towns.py burn Vizima78$ uv run python towns.py burn Vizima7980Burning town: Vizima81```82
12 unmodified lines
95<div class="termy">9697```console98// Check the help99$ python lands.py --help99$ uv run python lands.py --help100101Usage: lands.py [OPTIONS] COMMAND [ARGS]...102103Options:
5 unmodified lines
109 reigns110 towns111112// We still have the help for reigns113$ python lands.py reigns --help113$ uv run python lands.py reigns --help114115Usage: lands.py reigns [OPTIONS] COMMAND [ARGS]...116117Options:
3 unmodified lines
121 conquer122 destroy123124// And the help for towns125$ python lands.py towns --help125$ uv run python lands.py towns --help126127Usage: lands.py towns [OPTIONS] COMMAND [ARGS]...128129Options:
11 unmodified lines
141<div class="termy">142143```console144// Try the reigns command145$ python lands.py reigns conquer Gondor145$ uv run python lands.py reigns conquer Gondor146147Conquering reign: Gondor148149$ python lands.py reigns destroy Nilfgaard149$ uv run python lands.py reigns destroy Nilfgaard150151Destroying reign: Nilfgaard152153// Try the towns command154$ python lands.py towns found Springfield154$ uv run python lands.py towns found Springfield155156Founding town: Springfield157158$ python lands.py towns burn Atlantis158$ uv run python lands.py towns burn Atlantis159160Burning town: Atlantis161```162
31 unmodified lines
194<div class="termy">195196```console197// Check the main help198$ python main.py --help198$ uv run python main.py --help199200Usage: main.py [OPTIONS] COMMAND [ARGS]...201202Options:
6 unmodified lines
209 items210 lands211212// Try some users commands213$ python main.py users create Camila213$ uv run python main.py users create Camila214215Creating user: Camila216217// Now try some items commands218$ python main.py items create Sword218$ uv run python main.py items create Sword219220Creating item: Sword221222// And now some lands commands for reigns223$ python main.py lands reigns conquer Gondor223$ uv run python main.py lands reigns conquer Gondor224225Conquering reign: Gondor226227// And for towns228$ python main.py lands towns found Cartagena228$ uv run python main.py lands towns found Cartagena229230Founding town: Cartagena231```232
51 unmodified lines
modified:docs/tutorial/subcommands/single-file.md+8 added-8 removed
36 unmodified lines
37<div class="termy">3839```console40// We want this ✔️41$ python main.py items create41$ uv run python main.py items create42```4344</div>45
2 unmodified lines
48<div class="termy">4950```console51// We don't want this ⛔️52$ python main.py items items-create52$ uv run python main.py items items-create53```5455</div>56
9 unmodified lines
66<div class="termy">6768```console69// Check the help70$ python main.py --help70$ uv run python main.py --help7172Usage: main.py [OPTIONS] COMMAND [ARGS]...7374Options:
15 unmodified lines
90<div class="termy">9192```console93// Check the help for items94$ python main.py items --help94$ uv run python main.py items --help9596// It shows its own commands (subcommands): create, delete, sell97Usage: main.py items [OPTIONS] COMMAND [ARGS]...98
5 unmodified lines
104 delete105 sell106107// Try it108$ python main.py items create Wand108$ uv run python main.py items create Wand109110Creating item: Wand111112$ python main.py items sell Vase112$ uv run python main.py items sell Vase113114Selling item: Vase115```116
4 unmodified lines
121122<div class="termy">123124```console125$ python main.py users --help125$ uv run python main.py users --help126127Usage: main.py users [OPTIONS] COMMAND [ARGS]...128129Options:
3 unmodified lines
133 create134 delete135136// Try it137$ python main.py users create Camila137$ uv run python main.py users create Camila138139Creating user: Camila140```141
1 unmodified line
modified:docs/tutorial/app-dir.md+1 added-1 removed
9 unmodified lines
1011<div class="termy">1213```console14$ python main.py14$ uv run python main.py1516Config file doesn't exist yet17```18
25 unmodified lines
modified:docs/tutorial/exceptions.md+6 added-6 removed
21 unmodified lines
2223<div class="termy">2425```console26$ python main.py26$ uv run python main.py2728<font color="#F92672">╭──────────────── </font><font color="#F92672"><b>Traceback (most recent call last)</b></font><font color="#F92672"> ────────────────╮</font>29<font color="#F92672">│</font> <font color="#A37F4E">/home/user/code/superapp/</font><font color="#F4BF75"><b>main.py</b></font>:<font color="#66D9EF">8</font> in <font color="#A6E22E">main</font> <font color="#F92672">│</font>30<font color="#F92672">│</font> <font color="#F92672">│</font>
18 unmodified lines
4950<div class="termy">5152```console53$ python main.py53$ uv run python main.py5455Traceback (most recent call last):5657 File "main.py", line 12, in <module>
25 unmodified lines
8384<div class="termy">8586```console87$ python main.py87$ uv run python main.py8889<font color="#F92672">╭──────────────── </font><font color="#F92672"><b>Traceback (most recent call last)</b></font><font color="#F92672"> ────────────────╮</font>90<font color="#F92672">│</font> <font color="#A37F4E">/home/user/code/superapp/</font><font color="#F4BF75"><b>main.py</b></font>:<font color="#66D9EF">5</font> in <font color="#A6E22E">main</font> <font color="#F92672">│</font>91<font color="#F92672">│</font> <font color="#F92672">│</font>
28 unmodified lines
120121<div class="termy">122123```console124$ python main.py124$ uv run python main.py125126<font color="#F92672">╭──────────────── </font><font color="#F92672"><b>Traceback (most recent call last)</b></font><font color="#F92672"> ────────────────╮</font>127<font color="#F92672">│</font> <font color="#A37F4E">/home/user/code/superapp/</font><font color="#F4BF75"><b>main.py</b></font>:<font color="#66D9EF">12</font> in <font color="#A6E22E">&lt;module&gt;</font> <font color="#F92672">│</font>128<font color="#F92672">│</font> <font color="#F92672">│</font>
73 unmodified lines
202203<div class="termy">204205```console206$ python main.py206$ uv run python main.py207208Traceback (most recent call last):209 File "main.py", line 12, in <module>210 app()
27 unmodified lines
238<div class="termy">239240```console241export TYPER_STANDARD_TRACEBACK=1242$ python main.py242$ uv run python main.py243244245Traceback (most recent call last):246 File "main.py", line 12, in <module>
23 unmodified lines
modified:docs/tutorial/first-steps.md+22 added-22 removed
11 unmodified lines
1213<div class="termy">1415```console16$ python main.py16$ uv run python main.py1718Hello World1920// It just prints "Hello World".2122// Now check the --help23$ python main.py --help23$ uv run python main.py --help2425<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] </b>26<b> </b>27<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>
38 unmodified lines
66<div class="termy">6768```console6970$ python main.py70$ uv run python main.py7172// If you run it without the argument, it shows a nice error73<font color="#F4BF75">Usage: </font>main.py [OPTIONS] {name}74<font color="#A5A5A1">Try </font><font color="#44919F">&apos;main.py </font><font color="#44919F"><b>--help</b></font><font color="#44919F">&apos;</font><font color="#A5A5A1"> for help.</font>
1 unmodified line
76<font color="#F92672">│</font> Missing argument &apos;name&apos;. <font color="#F92672">│</font>77<font color="#F92672">╰───────────────────────────────────────────────────╯</font>7879// Now pass that 'name' CLI argument80$ python main.py Camila80$ uv run python main.py Camila8182Hello Camila8384// Here "Camila" is the CLI argument8586// To pass a name with spaces for the same CLI argument, use quotes87$ python main.py "Camila Gutiérrez"87$ uv run python main.py "Camila Gutiérrez"8889Hello Camila Gutiérrez90```91
16 unmodified lines
108<div class="termy">109110```console111// Check the main --help112$ python main.py --help112$ uv run python main.py --help113114<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] {name} {lastname} </b>115<b> </b>116<font color="#A5A5A1">╭─ Arguments ─────────────────────────────────────────────────────╮</font>
7 unmodified lines
124125// There are now 2 CLI arguments, name and lastname126127// Now pass a single name argument128$ python main.py Camila128$ uv run python main.py Camila129130<font color="#F4BF75">Usage: </font>main.py [OPTIONS] {name} {lastname}131<font color="#A5A5A1">Try </font><font color="#44919F">&apos;main.py </font><font color="#44919F"><b>--help</b></font><font color="#44919F">&apos;</font><font color="#A5A5A1"> for help.</font>132<font color="#F92672">╭─ Error ───────────────────────────────────────────╮</font>133<font color="#F92672">│</font> Missing argument &apos;lastname&apos;. <font color="#F92672">│</font>134<font color="#F92672">╰───────────────────────────────────────────────────╯</font>135136// These 2 arguments are required, so, pass both:137$ python main.py Camila Gutiérrez137$ uv run python main.py Camila Gutiérrez138139Hello Camila Gutiérrez140```141
5 unmodified lines
147148If you called it with:149150```151$ python main.py Gutiérrez Camila151$ uv run python main.py Gutiérrez Camila152```153154your app wouldn't have a way to know which is the `name` and which the `lastname`. It expects the first *CLI argument* to be the `name` and the second *CLI argument* to be the `lastname`.155
76 unmodified lines
232<div class="termy">233234```console235// Get the help236$ python main.py --help236$ uv run python main.py --help237238<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] {name} {lastname} </b>239<b> </b>240<font color="#A5A5A1">╭─ Arguments ─────────────────────────────────────────────────────╮</font>
19 unmodified lines
260261<div class="termy">262263```console264$ python main.py Camila Gutiérrez264$ uv run python main.py Camila Gutiérrez265266Hello Camila Gutiérrez267268// But if you pass --formal269$ python main.py Camila Gutiérrez --formal269$ uv run python main.py Camila Gutiérrez --formal270271Good day Ms. Camila Gutiérrez.272273// And as --formal is a CLI option you can put it anywhere in this command274$ python main.py Camila --formal Gutiérrez274$ uv run python main.py Camila --formal Gutiérrez275276Good day Ms. Camila Gutiérrez.277278$ python main.py --formal Camila Gutiérrez278$ uv run python main.py --formal Camila Gutiérrez279280Good day Ms. Camila Gutiérrez.281```282
9 unmodified lines
292293<div class="termy">294295```console296$ python main.py --help296$ uv run python main.py --help297298<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] {name} </b>299<b> </b>300<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────────────────────╮</font>
20 unmodified lines
321<div class="termy">322323```console324// Call it without a --lastname325$ python main.py Camila325$ uv run python main.py Camila326327Hello Camila328329// Pass the --lastname330$ python main.py Camila --lastname Gutiérrez330$ uv run python main.py Camila --lastname Gutiérrez331332Hello Camila Gutiérrez333```334
9 unmodified lines
344345<div class="termy">346347```console348$ python main.py --lastname Gutiérrez Camila348$ uv run python main.py --lastname Gutiérrez Camila349350// and it will still work normally351Hello Camila Gutiérrez352```
10 unmodified lines
363364<div class="termy">365366```console367$ python main.py --help367$ uv run python main.py --help368369<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] {name} </b>370<b> </b>371 Say hi to 'name', optionally with a <font color="#A1EFE4"><b>--lastname</b></font>.
92 unmodified lines
464We will use ***CLI parameter*** to refer to both, *CLI arguments* and *CLI options*.465466## The `typer` Command467468When you install `typer`, by default it adds a `typer` command to your shell.468When you [activate the project's virtual environment](install.md#activate-the-virtual-environment), the `typer` command installed in that project becomes available directly in your shell.469470This `typer` command allows you to run your scripts with ✨ auto completion ✨ in your terminal.470After you install completion for it, this `typer` command allows you to run your scripts with ✨ auto completion ✨ in your terminal.471472As an alternative to running with Python:473474<div class="termy">475476```console477$ python main.py477$ uv run python main.py478479Hello World480```481
25 unmodified lines
modified:docs/tutorial/index.md+1 added-1 removed
46 unmodified lines
4748<div class="termy">4950```console51$ python main.py51$ uv run python main.py5253✨ The magic happens here ✨54```55
9 unmodified lines
modified:docs/tutorial/install.md+87 added-4 removed
1# Install **Typer**23The first step is to install **Typer**.3The first step is to set up your project and add **Typer**.45First, make sure you create your [virtual environment](../virtual-environments.md), activate it, and then install it, for example with:5Install [`uv`](https://docs.astral.sh/uv/getting-started/installation/), then create a project and add Typer:67<div class="termy">89```console10$ pip install typer10$ uv init awesome-project --bare11$ cd awesome-project12$ uv add typer13---> 100%12Successfully installed typer shellingham rich14```1516</div>1718`uv add` creates the project's virtual environment in `.venv`, adds Typer to `pyproject.toml`, and creates `uv.lock` so the same package versions can be installed later.1920/// details | What these commands do2122* `uv init`: create a new Python project.23* `awesome-project`: create the project in a new directory with this name.24* `--bare`: create only the minimal `pyproject.toml` file, without generating a sample `main.py`, `README.md`, or other files. You will create the application files yourself in the next steps of this tutorial.2526Then `cd awesome-project` enters the new project directory before adding Typer.2728`uv` will use a compatible Python version already installed on your system, or download one if needed.2930When you run `uv add`, it selects compatible versions of Typer and all the packages Typer depends on. It records the exact versions in `uv.lock`, making it possible to install the same package versions later on another computer or when distributing the application.3132Creating or updating this file is called [**locking** the project dependencies](https://docs.astral.sh/uv/concepts/projects/sync/). `uv` does this automatically when you add a package.3334///3536By default, `typer` comes with `rich` and `shellingham`.3738## Activate the Virtual Environment3940`uv add typer` installs both the Typer library and the `typer` command in the project's `.venv`.4142You can normally run project commands with `uv run`. But to configure and use shell completion for the first-class `typer` command, activate the project's virtual environment so that `typer` is available directly in your shell.4344//// tab | Linux, macOS4546<div class="termy">4748```console49$ source .venv/bin/activate50```5152</div>5354////5556//// tab | Windows PowerShell5758<div class="termy">5960```console61$ .venv\Scripts\Activate.ps162```6364</div>6566////6768//// tab | Windows Bash6970<div class="termy">7172```console73$ source .venv/Scripts/activate74```7576</div>7778////7980Enable completion for your current shell with:8182<div class="termy">8384```console85$ typer --install-completion86```8788</div>8990Restart your terminal for the new completion configuration to take effect.9192Activate the project's virtual environment again in each new terminal session where you want to use the `typer` command directly.9394/// details | Using `pip` instead9596If you prefer to manage a virtual environment and packages manually, create and activate a virtual environment and then install Typer with `pip install typer`.9798Read the [Virtual Environments guide](https://tiangolo.com/guides/virtual-environments/) for the detailed steps.99100///
modified:docs/tutorial/launch.md+2 added-2 removed
9 unmodified lines
1011<div class="termy">1213```console14$ python main.py14$ uv run python main.py1516Opening Typer docs1718// Opens browser with Typer's docs
19 unmodified lines
3839<div class="termy">4041```console42$ python main.py42$ uv run python main.py4344Opening config directory4546// Opens a file browser indicating where the config file is located
3 unmodified lines
modified:docs/tutorial/one-file-per-command.md+2 added-2 removed
106 unmodified lines
107108<div class="termy">109110```console111$ python -m mycli.main version111$ uv run python -m mycli.main version112113My CLI Version 1.0114115$ python -m mycli.main users add Camila115$ uv run python -m mycli.main users add Camila116117Adding user: Camila118```119
25 unmodified lines
modified:docs/tutorial/package.md+6 added-37 removed
296 unmodified lines
297The `.whl` is the wheel file. You can send that wheel file to anyone and they can use it to install your program (we'll see how to upload it to PyPI in a bit).298299## Test your wheel package300301Now you can open another terminal and install that package from the file for your own user with:301Now you can open another terminal and install that package as an isolated command-line tool with:302303<div class="termy">304305```console306$ pip install --user /home/rick/rick-portal-gun/dist/rick_portal_gun-0.1.0-py3-none-any.whl306$ uv tool install /home/rick/rick-portal-gun/dist/rick_portal_gun-0.1.0-py3-none-any.whl307308---> 100%309```310311</div>312313/// warning314315The `--user` is important, that ensures you install it in your user's directory and not in the global system.316317If you installed it in the global system (e.g. with `sudo`) you could install a version of a library (e.g. a sub-dependency) that is incompatible with your system.318319///320313/// tip314323Bonus points if you use [uvx](https://docs.astral.sh/uv/) to install it while keeping an isolated environment for your Python CLI programs 🚀315`uv` installs the tool in its own isolated environment, avoiding conflicts with your projects and system Python.316317///318319Now you have your CLI program installed. And you can use it freely:
212 unmodified lines
532533<div class="termy">534535```console544$ pip uninstall rick-portal-gun545546Found existing installation: rick-portal-gun 0.1.0547Uninstalling rick-portal-gun-0.1.0:548 Would remove:549 /home/rick/.local/bin/rick-portal-gun550 /home/rick/.local/lib/python3.10/site-packages/rick_portal_gun-0.1.0.dist-info/*551 /home/rick/.local/lib/python3.10/site-packages/rick_portal_gun/*552# Proceed (Y/n)? $ Y553 Successfully uninstalled rick-portal-gun-0.1.0536$ uv tool uninstall rick-portal-gun537```538539</div>540558And now install it again, but this time using just the name, so that `pip` pulls it from PyPI:541And now install it again, but this time using just the name, so that `uv` pulls it from PyPI:542543<div class="termy">544545```console563$ pip install --user rick-portal-gun564565// Notice that it says "Downloading" 🚀566Collecting rick-portal-gun567 Downloading rick_portal_gun-0.1.0-py3-none-any.whl.metadata (435 bytes)568Requirement already satisfied: typer<0.13.0,>=0.12.3 in ./.local/lib/python3.10/site-packages (from rick-portal-gun==0.1.0) (0.12.3)569Requirement already satisfied: typing-extensions>=3.7.4.3 in ./.local/lib/python3.10/site-packages (from typer<0.13.0,>=0.12.3->rick-portal-gun==0.1.0) (4.11.0)570Requirement already satisfied: shellingham>=1.3.0 in ./.local/lib/python3.10/site-packages (from typer<0.13.0,>=0.12.3->rick-portal-gun==0.1.0) (1.5.4)571Requirement already satisfied: rich>=10.11.0 in ./.local/lib/python3.10/site-packages (from typer<0.13.0,>=0.12.3->rick-portal-gun==0.1.0) (13.7.1)572Requirement already satisfied: pygments<3.0.0,>=2.13.0 in ./.local/lib/python3.10/site-packages (from rich>=10.11.0->typer<0.13.0,>=0.12.3->rick-portal-gun==0.1.0) (2.17.2)573Requirement already satisfied: markdown-it-py>=2.2.0 in ./.local/lib/python3.10/site-packages (from rich>=10.11.0->typer<0.13.0,>=0.12.3->rick-portal-gun==0.1.0) (3.0.0)574Requirement already satisfied: mdurl~=0.1 in ./.local/lib/python3.10/site-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<0.13.0,>=0.12.3->rick-portal-gun==0.1.0) (0.1.2)575Downloading rick_portal_gun-0.1.0-py3-none-any.whl (1.8 kB)576Installing collected packages: rick-portal-gun577Successfully installed rick-portal-gun-0.1.0546$ uv tool install rick-portal-gun547```548549</div>550
89 unmodified lines
modified:docs/tutorial/printing.md+5 added-5 removed
7 unmodified lines
89<div class="termy">1011```console12$ python main.py World12$ uv run python main.py World1314Hello World15```16
13 unmodified lines
3031<div class="termy">3233```console34$ python main.py34$ uv run python main.py3536Here's the data37<b>{</b>38 <font color="#A6E22E">&apos;name&apos;</font>: <font color="#A6E22E">&apos;Rick&apos;</font>,
17 unmodified lines
5657<div class="termy">5859```console60$ python main.py60$ uv run python main.py6162<font color="#F92672"><b>Alert!</b></font> <font color="#A6E22E">Portal gun</font> shooting! 💥63```64
19 unmodified lines
8485<div class="termy">8687```console88$ python main.py88$ uv run python main.py8990┏━━━━━━━┳━━━━━━━━━━━━┓91┃<b> Name </b>┃<b> Item </b>┃92┡━━━━━━━╇━━━━━━━━━━━━┩
65 unmodified lines
158159<div class="termy">160161```console162$ python main.py162$ uv run python main.py163164Here is something written to standard error165```166
106 unmodified lines
modified:docs/tutorial/progressbar.md+5 added-5 removed
13 unmodified lines
1415<div class="termy">1617```console18$ python main.py18$ uv run python main.py1920---> 100%2122Processed 100 things.
7 unmodified lines
3031<div class="termy">3233```console34$ python main.py34$ uv run python main.py3536Processing... <font color="#F92672">━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸</font><font color="#3A3A3A">━━━━━━━━━━</font> <font color="#AE81FF"> 74%</font> <font color="#A1EFE4">0:00:01</font>37```38
15 unmodified lines
5455<div class="termy">5657```console58$ python main.py58$ uv run python main.py5960<font color="#A6E22E">⠹</font> Processing...61<font color="#A6E22E">⠹</font> Preparing...62```
72 unmodified lines
135136<div class="termy">137138```console139$ python main.py139$ uv run python main.py140141---> 100%142143Processed 100 things.
19 unmodified lines
163164<div class="termy">165166```console167$ python main.py167$ uv run python main.py168169---> 100%170171Processed 100 user IDs.
61 unmodified lines
modified:docs/tutorial/prompt.md+6 added-6 removed
9 unmodified lines
1011<div class="termy">1213```console14$ python main.py14$ uv run python main.py1516# What's your name?:$ Camila1718Hello Camila
11 unmodified lines
3031<div class="termy">3233```console34$ python main.py34$ uv run python main.py3536# Are you sure you want to delete it? [y/N]:$ y3738Deleting it!3940// This time cancel it41$ python main.py41$ uv run python main.py4243# Are you sure you want to delete it? [y/N]:$ n4445Not deleting
10 unmodified lines
5657<div class="termy">5859```console60$ python main.py60$ uv run python main.py6162# Are you sure you want to delete it? [y/N]:$ y6364Deleting it!6566// This time cancel it67$ python main.py67$ uv run python main.py6869# Are you sure you want to delete it? [y/N]:$ n7071Aborted!
11 unmodified lines
8384<div class="termy">8586```console87$ python main.py87$ uv run python main.py8889# Enter your name 😎:$ Morty9091Hello Morty
3 unmodified lines
modified:docs/tutorial/terminating.md+6 added-6 removed
23 unmodified lines
2425<div class="termy">2627```console28$ python main.py Camila28$ uv run python main.py Camila2930User created: Camila31Notification sent for new user: Camila3233// Try with an existing user34$ python main.py rick34$ uv run python main.py rick3536The user already exists3738// Notice that the notification code was never run, the second message is not printed
23 unmodified lines
6263<div class="termy">6465```console66$ python main.py Camila66$ uv run python main.py Camila6768New user created: Camila6970// Print the result code of the last program executed
1 unmodified line
727307475// Now make it exit with an error76$ python main.py root76$ uv run python main.py root7778The root user is reserved7980// Print the result code of the last program executed
24 unmodified lines
105106<div class="termy">107108```console109$ python main.py Camila109$ uv run python main.py Camila110111New user created: Camila112113// Now make it exit with an error114$ python main.py root114$ uv run python main.py root115116The root user is reserved117Aborted!118```
2 unmodified lines
modified:docs/tutorial/testing.md+3 added-3 removed
9 unmodified lines
1011<div class="termy">1213```console14$ python main.py Camila --city Berlin14$ uv run python main.py Camila --city Berlin1516Hello Camila17Let's have a coffee in Berlin18```
68 unmodified lines
8788<div class="termy">8990```console91$ pytest91$ uv run pytest9293================ test session starts ================94platform linux -- Python 3.10, pytest-5.3.5, py-1.8.1, pluggy-0.13.195rootdir: /home/user/code/superawesome-cli/app
19 unmodified lines
115116<div class="termy">117118```console119$ python main.py Camila119$ uv run python main.py Camila120121# Email: $ camila@example.com122123Hello Camila, your email is: camila@example.com
91 unmodified lines
modified:docs/tutorial/typer-app.md+4 added-4 removed
47 unmodified lines
48<div class="termy">4950```console51// Without a CLI argument52$ python main.py52$ uv run python main.py5354Usage: main.py [OPTIONS] {name}55Try "main.py --help" for help.5657Error: Missing argument 'name'.5859// With the 'name' CLI argument60$ python main.py Camila60$ uv run python main.py Camila6162Hello Camila6364// Asking for help65$ python main.py --help65$ uv run python main.py --help6667Usage: main.py [OPTIONS] {name}6869Arguments:
22 unmodified lines
9293<div class="termy">9495```console96$ python main.py96$ uv run python main.py9798✨ Some magic here ✨99```100
21 unmodified lines
modified:docs/tutorial/typer-command.md+40 added-13 removed
1# `typer` command23The `typer` command provides ✨ completion ✨ in the Terminal for your own small scripts. Even if they don't use Typer internally. Of course, it works better if you use **Typer** in your script.45It's probably most useful if you have a small custom Python script using **Typer** (maybe as part of some project), for some small tasks, and it's not complex/important enough to create a whole installable Python package for it (something to be installed with `pip`).5It's probably most useful if you have a small custom Python script using **Typer** (maybe as part of some project), for some small tasks, and it's not complex/important enough to create a whole installable Python package.67In that case, you can run your program with the `typer` command in your Terminal, and it will provide completion for your script.89The `typer` command also has functionality to generate Markdown documentation for your own **Typer** programs 📝.1011## Install1213When you install **Typer** with:13Add Typer to your project:1415```bash16pip install typer15```console16$ uv add typer17```1819...it includes the `typer` command.19This installs both the Typer library and the `typer` command in the project's `.venv`.2021Activate that environment to make `typer` available as a first-class command in your terminal:2223//// tab | Linux, macOS2425```console26$ source .venv/bin/activate27```2829////3031//// tab | Windows PowerShell3233```console34$ .venv\Scripts\Activate.ps135```3637////3839//// tab | Windows Bash4041```console42$ source .venv/Scripts/activate43```4445////4647Using the `typer` command from the project environment ensures it uses the same Typer version and dependencies as the project.4849If you don't want to use the `typer` command, you can call the Typer library as a module with:5023```bash24python -m typer51```console52$ uv run python -m typer53```5455## Install completion56
46 unmodified lines
103104<div class="termy">105106```console79$ python -m pip install typer107$ uv add typer108---> 100%81Successfully installed typer109```110111</div>112
3 unmodified lines
116117<div class="termy">118119```console93$ python my_custom_script.py hello120$ uv run python my_custom_script.py hello121122Hello World!12397$ python my_custom_script.py hello --name Camila124$ uv run python my_custom_script.py hello --name Camila125126Hello Camila!127101$ python my_custom_script.py bye --name Camila128$ uv run python my_custom_script.py bye --name Camila129130Bye Camila131```132
151 unmodified lines
284285If you don't want to use the `typer` command, you can still generate docs with:286287```console261$ python -m typer some_script.py utils docs288$ uv run python -m typer some_script.py utils docs289```290291///292
108 unmodified lines
modified:docs/environment-variables.md+4 added-293 removed
1# Environment Variables23Before we jump into **Typer** code, let's cover a bit some of the **basics** that we'll need to understand how to work with Python (and programming) in general. Let's check a bit about **environment variables**.3An **environment variable** (also known as an **env var**) is a value that lives outside of your Python code, in the operating system, and can be read by your application and other programs.45/// tip5Typer can use environment variables as alternative values for command-line parameters. You will learn how in [CLI Arguments with Environment Variables](tutorial/arguments/envvar.md).67If you already know what "environment variables" are and how to use them, feel free to skip this.7## Learn More { #learn-more }89///1011An environment variable (also known as "**env var**") is a variable that lives **outside** of the Python code, in the **operating system**, and could be read by your Python code (or by other programs as well).1213Environment variables could be useful for handling application **settings**, as part of the **installation** of Python, etc.1415## Create and Use Env Vars1617You can **create** and use environment variables in the **shell (terminal)**, without needing Python:1819//// tab | Linux, macOS, Windows Bash2021<div class="termy">2223```console24// You could create an env var MY_NAME with25$ export MY_NAME="Wade Wilson"2627// Then you could use it with other programs, like28$ echo "Hello $MY_NAME"2930Hello Wade Wilson31```3233</div>3435////3637//// tab | Windows PowerShell3839<div class="termy">4041```console42// Create an env var MY_NAME43$ $Env:MY_NAME = "Wade Wilson"4445// Use it with other programs, like46$ echo "Hello $Env:MY_NAME"4748Hello Wade Wilson49```5051</div>5253////5455## Read env vars in Python5657You could also create environment variables **outside** of Python, in the terminal (or with any other method), and then **read them in Python**.5859For example you could have a file `main.py` with:6061```Python hl_lines="3"62import os6364name = os.getenv("MY_NAME", "World")65print(f"Hello {name} from Python")66```6768/// tip6970The second argument to [`os.getenv()`](https://docs.python.org/3.8/library/os.html#os.getenv) is the default value to return.7172If not provided, it's `None` by default, here we provide `"World"` as the default value to use.7374///7576Then you could call that Python program:7778//// tab | Linux, macOS, Windows Bash7980<div class="termy">8182```console83// Here we don't set the env var yet84$ python main.py8586// As we didn't set the env var, we get the default value8788Hello World from Python8990// But if we create an environment variable first91$ export MY_NAME="Wade Wilson"9293// And then call the program again94$ python main.py9596// Now it can read the environment variable9798Hello Wade Wilson from Python99```100101</div>102103////104105//// tab | Windows PowerShell106107<div class="termy">108109```console110// Here we don't set the env var yet111$ python main.py112113// As we didn't set the env var, we get the default value114115Hello World from Python116117// But if we create an environment variable first118$ $Env:MY_NAME = "Wade Wilson"119120// And then call the program again121$ python main.py122123// Now it can read the environment variable124125Hello Wade Wilson from Python126```127128</div>129130////131132As environment variables can be set outside of the code, but can be read by the code, and don't have to be stored (committed to `git`) with the rest of the files, it's common to use them for configurations or **settings**.133134You can also create an environment variable only for a **specific program invocation**, that is only available to that program, and only for its duration.135136To do that, create it right before the program itself, on the same line:137138<div class="termy">139140```console141// Create an env var MY_NAME in line for this program call142$ MY_NAME="Wade Wilson" python main.py143144// Now it can read the environment variable145146Hello Wade Wilson from Python147148// The env var no longer exists afterwards149$ python main.py150151Hello World from Python152```153154</div>155156/// tip157158You can read more about it at [The Twelve-Factor App: Config](https://12factor.net/config).159160///161162## Types and Validation163164These environment variables can only handle **text strings**, as they are external to Python and have to be compatible with other programs and the rest of the system (and even with different operating systems, as Linux, Windows, macOS).165166That means that **any value** read in Python from an environment variable **will be a `str`**, and any conversion to a different type or any validation has to be done in code.167168You will learn more about using environment variables for your <abbr title="command line interface">CLI</abbr> applications later in the section about [CLI Arguments with Environment Variables](./tutorial/arguments/envvar.md).169170## `PATH` Environment Variable171172There is a **special** environment variable called **`PATH`** that is used by the operating systems (Linux, macOS, Windows) to find programs to run.173174The value of the variable `PATH` is a long string that is made of directories separated by a colon `:` on Linux and macOS, and by a semicolon `;` on Windows.175176For example, the `PATH` environment variable could look like this:177178//// tab | Linux, macOS179180```plaintext181/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin182```183184This means that the system should look for programs in the directories:185186* `/usr/local/bin`187* `/usr/bin`188* `/bin`189* `/usr/sbin`190* `/sbin`191192////193194//// tab | Windows195196```plaintext197C:\Program Files\Python312\Scripts;C:\Program Files\Python312;C:\Windows\System32198```199200This means that the system should look for programs in the directories:201202* `C:\Program Files\Python312\Scripts`203* `C:\Program Files\Python312`204* `C:\Windows\System32`205206////207208When you type a **command** in the terminal, the operating system **looks for** the program in **each of those directories** listed in the `PATH` environment variable.209210For example, when you type `python` in the terminal, the operating system looks for a program called `python` in the **first directory** in that list.211212If it finds it, then it will **use it**. Otherwise it keeps looking in the **other directories**.213214### Installing Python and Updating the `PATH`215216When you install Python, you might be asked if you want to update the `PATH` environment variable.217218//// tab | Linux, macOS219220Let's say you install Python and it ends up in a directory `/opt/custompython/bin`.221222If you say yes to update the `PATH` environment variable, then the installer will add `/opt/custompython/bin` to the `PATH` environment variable.223224It could look like this:225226```plaintext227/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/custompython/bin228```229230////231232//// tab | Windows233234Let's say you install Python and it ends up in a directory `C:\opt\custompython\bin`.235236If you say yes to update the `PATH` environment variable, then the installer will add `C:\opt\custompython\bin` to the `PATH` environment variable.237238```plaintext239C:\Program Files\Python312\Scripts;C:\Program Files\Python312;C:\Windows\System32;C:\opt\custompython\bin240```241242////243244So, if you type:245246<div class="termy">247248```console249$ python250```251252</div>253254//// tab | Linux, macOS255256The system will **find** the `python` program in `/opt/custompython/bin` (the last directory) and run it.257258It would be roughly equivalent to typing:259260<div class="termy">261262```console263$ /opt/custompython/bin/python264```265266</div>267268////269270//// tab | Windows271272The system will **find** the `python` program in `C:\opt\custompython\bin\python` (the last directory) and run it.273274It would be roughly equivalent to typing:275276<div class="termy">277278```console279$ C:\opt\custompython\bin\python280```281282</div>283284////285286This information will be useful when learning about [Virtual Environments](virtual-environments.md).287288It will also be useful when you **create your own CLI programs** as, for them to be available for your users, they will need to be somewhere in the `PATH` environment variable.289290## Conclusion291292With this you should have a basic understanding of what **environment variables** are and how to use them in Python.293294You can also read more about them in the [Wikipedia for Environment Variable](https://en.wikipedia.org/wiki/Environment_variable).295296In many cases it's not very obvious how environment variables would be useful and applicable right away. But they keep showing up in many different scenarios when you are developing, so it's good to know about them.297298For example, you will need this information in the next section, about [Virtual Environments](virtual-environments.md).9Read the [Environment Variables guide](https://tiangolo.com/guides/environment-variables/) for a detailed, cross-platform explanation, including how to create and read environment variables and how the `PATH` environment variable works.
modified:docs/index.md+15 added-12 removed
49 unmodified lines
50**Typer** is [FastAPI](https://fastapi.tiangolo.com)'s little sibling, it's the FastAPI of CLIs.5152## Installation5354Create and activate a [virtual environment](https://typer.tiangolo.com/virtual-environments/) and then install **Typer**:54First, [install `uv`](https://docs.astral.sh/uv/getting-started/installation/), and then add **Typer** to your project:5556<div class="termy">5758```console59$ pip install typer59$ uv add typer60---> 100%61Successfully installed typer rich shellingham61```6263</div>6465This installs both the Typer library and the `typer` command in the project's virtual environment. To use `typer` directly with shell completion, [activate the project environment and install completion](tutorial/install.md#activate-the-virtual-environment).6667If you prefer to use `pip`, install `typer` inside a virtual environment. See the [installation guide](tutorial/install.md) for the alternative steps.6869## Example7071### The absolute minimum72
73 unmodified lines
146<div class="termy">147148```console149// Run your application147$ python main.py150$ uv run python main.py151152// You get a nice error, you are missing 'name'153Usage: main.py [OPTIONS] {name}154Try 'main.py --help' for help.
2 unmodified lines
157╰───────────────────────────────────────────────────╯158159160// You get a --help for free158$ python main.py --help161$ uv run python main.py --help162163Usage: main.py [OPTIONS] {name}164165╭─ Arguments ───────────────────────────────────────╮
3 unmodified lines
169│ --help Show this message and exit. │170╰───────────────────────────────────────────────────╯171172// Now pass the 'name' argument170$ python main.py Camila173$ uv run python main.py Camila174175Hello Camila176177// It works! 🎉
51 unmodified lines
229230<div class="termy">231232```console230$ python main.py --help233$ uv run python main.py --help234235 Usage: main.py [OPTIONS] COMMAND [ARGS]...236237╭─ Options ─────────────────────────────────────────╮
24 unmodified lines
262263<div class="termy">264265```console263$ python main.py hello --help266$ uv run python main.py hello --help267268 Usage: main.py hello [OPTIONS] {name}269270╭─ Arguments ───────────────────────────────────────╮
10 unmodified lines
281282<div class="termy">283284```console282$ python main.py goodbye --help285$ uv run python main.py goodbye --help286287 Usage: main.py goodbye [OPTIONS] {name}288289╭─ Arguments ───────────────────────────────────────╮
16 unmodified lines
306307```console308// Use it with the hello command309307$ python main.py hello Camila310$ uv run python main.py hello Camila311312Hello Camila313314// And with the goodbye command315313$ python main.py goodbye Camila316$ uv run python main.py goodbye Camila317318Bye Camila!319320// And with --formal321319$ python main.py goodbye --formal Camila322$ uv run python main.py goodbye --formal Camila323324Goodbye Ms. Camila. Have a good day.325```326
62 unmodified lines
modified:docs/virtual-environments.md+11 added-820 removed
1# Virtual Environments23When you work in Python projects you probably should use a **virtual environment** (or a similar mechanism) to isolate the packages you install for each project.3When you work with Python projects, you should use a **virtual environment** to isolate the packages installed for each project.45/// note5For Typer projects, I recommend using [uv](https://docs.astral.sh/uv/) to manage the project, its dependencies, and its virtual environment.67If you already know about virtual environments, how to create them and use them, you might want to skip this section. 🤓7## Create a Project { #create-a-project }89///9Install `uv` using the [official installation guide](https://docs.astral.sh/uv/getting-started/installation/), and then create a project:1011/// tip1213A **virtual environment** is different than an **environment variable**.1415An **environment variable** is a variable in the system that can be used by programs.1617A **virtual environment** is a directory with some files in it.1819///2021/// note2223This page will teach you how to use **virtual environments** and how they work.2425If you are ready to adopt a **tool that manages everything** for you (including installing Python), try [uv](https://github.com/astral-sh/uv).2627///2829## Create a Project3031First, create a directory for your project.3233What I normally do is that I create a directory named `code` inside my home/user directory.3435And inside of that I create one directory per project.3611<div class="termy">1213```console40// Go to the home directory41$ cd42// Create a directory for all your code projects43$ mkdir code44// Enter into that code directory45$ cd code46// Create a directory for this project47$ mkdir awesome-project48// Enter into that project directory14$ uv init awesome-project --bare15$ cd awesome-project16$ uv add typer17```1819</div>2054## Create a Virtual Environment21`uv` creates a virtual environment for the project automatically. You don't need to create or activate one yourself.2256When you start working on a Python project **for the first time**, create a virtual environment **<abbr title="there are other options, this is a simple guideline">inside your project</abbr>**.23Run commands inside the project environment with `uv run`, for example:2458/// tip5960You only need to do this **once per project**, not every time you work.6162///6364//// tab | `venv`6566To create a virtual environment, you can use the `venv` module that comes with Python.6725<div class="termy">2627```console71$ python -m venv .venv28$ uv run python main.py29```3031</div>3276/// details | What that command means33## Learn More { #learn-more }3478* `python`: use the program called `python`79* `-m`: call a module as a script, we'll tell it which module next80* `venv`: use the module called `venv` that normally comes installed with Python81* `.venv`: create the virtual environment in the new directory `.venv`8283///8485////8687//// tab | `uv`8889If you have [`uv`](https://github.com/astral-sh/uv) installed, you can use it to create a virtual environment.9091<div class="termy">9293```console94$ uv venv95```9697</div>9899/// tip100101By default, `uv` will create a virtual environment in a directory called `.venv`.102103But you could customize it passing an additional argument with the directory name.104105///106107////108109That command creates a new virtual environment in a directory called `.venv`.110111/// details | `.venv` or other name112113You could create the virtual environment in a different directory, but there's a convention of calling it `.venv`.114115///116117## Activate the Virtual Environment118119Activate the new virtual environment so that any Python command you run or package you install uses it.120121/// tip122123Do this **every time** you start a **new terminal session** to work on the project.124125///126127//// tab | Linux, macOS128129<div class="termy">130131```console132$ source .venv/bin/activate133```134135</div>136137////138139//// tab | Windows PowerShell140141<div class="termy">142143```console144$ .venv\Scripts\Activate.ps1145```146147</div>148149////150151//// tab | Windows Bash152153Or if you use Bash for Windows (e.g. [Git Bash](https://gitforwindows.org/)):154155<div class="termy">156157```console158$ source .venv/Scripts/activate159```160161</div>162163////164165/// tip166167Every time you install a **new package** in that environment, **activate** the environment again.168169This makes sure that if you use a **terminal (<abbr title="command line interface">CLI</abbr>) program** installed by that package, you use the one from your virtual environment and not any other that could be installed globally, probably with a different version than what you need.170171///172173## Check the Virtual Environment is Active174175Check that the virtual environment is active (the previous command worked).176177/// tip178179This is **optional**, but it's a good way to **check** that everything is working as expected and you are using the virtual environment you intended.180181///182183//// tab | Linux, macOS, Windows Bash184185<div class="termy">186187```console188$ which python189190/home/user/code/awesome-project/.venv/bin/python191```192193</div>194195If it shows the `python` binary at `.venv/bin/python`, inside of your project (in this case `awesome-project`), then it worked. 🎉196197////198199//// tab | Windows PowerShell200201<div class="termy">202203```console204$ Get-Command python205206C:\Users\user\code\awesome-project\.venv\Scripts\python207```208209</div>210211If it shows the `python` binary at `.venv\Scripts\python`, inside of your project (in this case `awesome-project`), then it worked. 🎉212213////214215## Upgrade `pip`216217/// tip218219If you use [`uv`](https://github.com/astral-sh/uv) you would use it to install things instead of `pip`, so you don't need to upgrade `pip`. 😎220221///222223If you are using `pip` to install packages (it comes by default with Python), you should **upgrade** it to the latest version.224225Many exotic errors while installing a package are solved by just upgrading `pip` first.226227/// tip228229You would normally do this **once**, right after you create the virtual environment.230231///232233Make sure the virtual environment is active (with the command above) and then run:234235<div class="termy">236237```console238$ python -m pip install --upgrade pip239240---> 100%241```242243</div>244245## Add `.gitignore`246247If you are using **Git** (you should), add a `.gitignore` file to exclude everything in your `.venv` from Git.248249/// tip250251If you used [`uv`](https://github.com/astral-sh/uv) to create the virtual environment, it already did this for you, you can skip this step. 😎252253///254255/// tip256257Do this **once**, right after you create the virtual environment.258259///260261<div class="termy">262263```console264$ echo "*" > .venv/.gitignore265```266267</div>268269/// details | What that command means270271* `echo "*"`: will "print" the text `*` in the terminal (the next part changes that a bit)272* `>`: anything printed to the terminal by the command to the left of `>` should not be printed but instead written to the file that goes to the right of `>`273* `.gitignore`: the name of the file where the text should be written274275And `*` for Git means "everything". So, it will ignore everything in the `.venv` directory.276277That command will create a file `.gitignore` with the content:278279```gitignore280*281```282283///284285## Install Packages286287After activating the environment, you can install packages in it.288289/// tip290291Do this **once** when installing or upgrading the packages your project needs.292293If you need to upgrade a version or add a new package you would **do this again**.294295///296297### Install Packages Directly298299If you're in a hurry and don't want to use a file to declare your project's package requirements, you can install them directly.300301/// tip302303It's a (very) good idea to put the packages and versions your program needs in a file (for example `requirements.txt` or `pyproject.toml`).304305///306307//// tab | `pip`308309<div class="termy">310311```console312$ pip install typer313314---> 100%315```316317</div>318319////320321//// tab | `uv`322323If you have [`uv`](https://github.com/astral-sh/uv):324325<div class="termy">326327```console328$ uv pip install typer329---> 100%330```331332</div>333334////335336### Install from `requirements.txt`337338If you have a `requirements.txt`, you can now use it to install its packages.339340//// tab | `pip`341342<div class="termy">343344```console345$ pip install -r requirements.txt346---> 100%347```348349</div>350351////352353//// tab | `uv`354355If you have [`uv`](https://github.com/astral-sh/uv):356357<div class="termy">358359```console360$ uv pip install -r requirements.txt361---> 100%362```363364</div>365366////367368/// details | `requirements.txt`369370A `requirements.txt` with some packages could look like:371372```requirements.txt373typer==0.13.0374rich==13.7.1375```376377///378379## Run Your Program380381After you activated the virtual environment, you can run your program, and it will use the Python inside of your virtual environment with the packages you installed there.382383<div class="termy">384385```console386$ python main.py387388Hello World389```390391</div>392393## Configure Your Editor394395You would probably use an editor, make sure you configure it to use the same virtual environment you created (it will probably autodetect it) so that you can get autocompletion and inline errors.396397For example:398399* [VS Code](https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment)400* [PyCharm](https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html)401402/// tip403404You normally have to do this only **once**, when you create the virtual environment.405406///407408## Deactivate the Virtual Environment409410Once you are done working on your project you can **deactivate** the virtual environment.411412<div class="termy">413414```console415$ deactivate416```417418</div>419420This way, when you run `python` it won't try to run it from that virtual environment with the packages installed there.421422## Ready to Work423424Now you're ready to start working on your project.425426427428/// tip429430Do you want to understand what's all that above?431432Continue reading. 👇🤓433434///435436## Why Virtual Environments437438To work with Typer you need to install [Python](https://www.python.org/).439440After that, you would need to **install** Typer and any other **packages** you want to use.441442To install packages you would normally use the `pip` command that comes with Python (or similar alternatives).443444Nevertheless, if you just use `pip` directly, the packages would be installed in your **global Python environment** (the global installation of Python).445446### The Problem447448So, what's the problem with installing packages in the global Python environment?449450At some point, you will probably end up writing many different programs that depend on **different packages**. And some of these projects you work on will depend on **different versions** of the same package. 😱451452For example, you could create a project called `philosophers-stone`, this program depends on another package called **`harry`, using the version `1`**. So, you need to install `harry`.453454```mermaid455flowchart LR456 stone(philosophers-stone) -->|requires| harry-1[harry v1]457```458459Then, at some point later, you create another project called `prisoner-of-azkaban`, and this project also depends on `harry`, but this project needs **`harry` version `3`**.460461```mermaid462flowchart LR463 azkaban(prisoner-of-azkaban) --> |requires| harry-3[harry v3]464```465466But now the problem is, if you install the packages globally (in the global environment) instead of in a local **virtual environment**, you will have to choose which version of `harry` to install.467468If you want to run `philosophers-stone` you will need to first install `harry` version `1`, for example with:469470<div class="termy">471472```console473$ pip install "harry==1"474```475476</div>477478And then you would end up with `harry` version `1` installed in your global Python environment.479480```mermaid481flowchart LR482 subgraph global[global env]483 harry-1[harry v1]484 end485 subgraph stone-project[philosophers-stone project]486 stone(philosophers-stone) -->|requires| harry-1487 end488```489490But then if you want to run `prisoner-of-azkaban`, you will need to uninstall `harry` version `1` and install `harry` version `3` (or just installing version `3` would automatically uninstall version `1`).491492<div class="termy">493494```console495$ pip install "harry==3"496```497498</div>499500And then you would end up with `harry` version `3` installed in your global Python environment.501502And if you try to run `philosophers-stone` again, there's a chance it would **not work** because it needs `harry` version `1`.503504```mermaid505flowchart LR506 subgraph global[global env]507 harry-1[<strike>harry v1</strike>]508 style harry-1 fill:#ccc,stroke-dasharray: 5 5509 harry-3[harry v3]510 end511 subgraph stone-project[philosophers-stone project]512 stone(philosophers-stone) -.-x|⛔️| harry-1513 end514 subgraph azkaban-project[prisoner-of-azkaban project]515 azkaban(prisoner-of-azkaban) --> |requires| harry-3516 end517```518519/// tip520521It's very common in Python packages to try the best to **avoid breaking changes** in **new versions**, but it's better to be safe, and install newer versions intentionally and when you can run the tests to check everything is working correctly.522523///524525Now, imagine that with **many** other **packages** that all your **projects depend on**. That's very difficult to manage. And you would probably end up running some projects with some **incompatible versions** of the packages, and not knowing why something isn't working.526527Also, depending on your operating system (e.g. Linux, Windows, macOS), it could have come with Python already installed. And in that case it probably had some packages pre-installed with some specific versions **needed by your system**. If you install packages in the global Python environment, you could end up **breaking** some of the programs that came with your operating system.528529## Where are Packages Installed530531When you install Python, it creates some directories with some files in your computer.532533Some of these directories are the ones in charge of having all the packages you install.534535When you run:536537<div class="termy">538539```console540// Don't run this now, it's just an example 🤓541$ pip install typer542---> 100%543```544545</div>546547That will download a compressed file with the Typer code, normally from [PyPI](https://pypi.org/project/typer/).548549It will also **download** files for other packages that Typer depends on.550551Then it will **extract** all those files and put them in a directory in your computer.552553By default, it will put those files downloaded and extracted in the directory that comes with your Python installation, that's the **global environment**.554555## What are Virtual Environments556557The solution to the problems of having all the packages in the global environment is to use a **virtual environment for each project** you work on.558559A virtual environment is a **directory**, very similar to the global one, where you can install the packages for a project.560561This way, each project will have its own virtual environment (`.venv` directory) with its own packages.562563```mermaid564flowchart TB565 subgraph stone-project[philosophers-stone project]566 stone(philosophers-stone) --->|requires| harry-1567 subgraph venv1[.venv]568 harry-1[harry v1]569 end570 end571 subgraph azkaban-project[prisoner-of-azkaban project]572 azkaban(prisoner-of-azkaban) --->|requires| harry-3573 subgraph venv2[.venv]574 harry-3[harry v3]575 end576 end577 stone-project ~~~ azkaban-project578```579580## What Does Activating a Virtual Environment Mean581582When you activate a virtual environment, for example with:583584//// tab | Linux, macOS585586<div class="termy">587588```console589$ source .venv/bin/activate590```591592</div>593594////595596//// tab | Windows PowerShell597598<div class="termy">599600```console601$ .venv\Scripts\Activate.ps1602```603604</div>605606////607608//// tab | Windows Bash609610Or if you use Bash for Windows (e.g. [Git Bash](https://gitforwindows.org/)):611612<div class="termy">613614```console615$ source .venv/Scripts/activate616```617618</div>619620////621622That command will create or modify some [environment variables](environment-variables.md) that will be available for the next commands.623624One of those variables is the `PATH` variable.625626/// tip627628You can learn more about the `PATH` environment variable in the [Environment Variables](environment-variables.md#path-environment-variable) section.629630///631632Activating a virtual environment adds its path `.venv/bin` (on Linux and macOS) or `.venv\Scripts` (on Windows) to the `PATH` environment variable.633634Let's say that before activating the environment, the `PATH` variable looked like this:635636//// tab | Linux, macOS637638```plaintext639/usr/bin:/bin:/usr/sbin:/sbin640```641642That means that the system would look for programs in:643644* `/usr/bin`645* `/bin`646* `/usr/sbin`647* `/sbin`648649////650651//// tab | Windows652653```plaintext654C:\Windows\System32655```656657That means that the system would look for programs in:658659* `C:\Windows\System32`660661////662663After activating the virtual environment, the `PATH` variable would look something like this:664665//// tab | Linux, macOS666667```plaintext668/home/user/code/awesome-project/.venv/bin:/usr/bin:/bin:/usr/sbin:/sbin669```670671That means that the system will now start looking first look for programs in:672673```plaintext674/home/user/code/awesome-project/.venv/bin675```676677before looking in the other directories.678679So, when you type `python` in the terminal, the system will find the Python program in680681```plaintext682/home/user/code/awesome-project/.venv/bin/python683```684685and use that one.686687////688689//// tab | Windows690691```plaintext692C:\Users\user\code\awesome-project\.venv\Scripts;C:\Windows\System32693```694695That means that the system will now start looking first look for programs in:696697```plaintext698C:\Users\user\code\awesome-project\.venv\Scripts699```700701before looking in the other directories.702703So, when you type `python` in the terminal, the system will find the Python program in704705```plaintext706C:\Users\user\code\awesome-project\.venv\Scripts\python707```708709and use that one.710711////712713An important detail is that it will put the virtual environment path at the **beginning** of the `PATH` variable. The system will find it **before** finding any other Python available. This way, when you run `python`, it will use the Python **from the virtual environment** instead of any other `python` (for example, a `python` from a global environment).714715Activating a virtual environment also changes a couple of other things, but this is one of the most important things it does.716717## Checking a Virtual Environment718719When you check if a virtual environment is active, for example with:720721//// tab | Linux, macOS, Windows Bash722723<div class="termy">724725```console726$ which python727728/home/user/code/awesome-project/.venv/bin/python729```730731</div>732733////734735//// tab | Windows PowerShell736737<div class="termy">738739```console740$ Get-Command python741742C:\Users\user\code\awesome-project\.venv\Scripts\python743```744745</div>746747////748749That means that the `python` program that will be used is the one **in the virtual environment**.750751you use `which` in Linux and macOS and `Get-Command` in Windows PowerShell.752753The way that command works is that it will go and check in the `PATH` environment variable, going through **each path in order**, looking for the program called `python`. Once it finds it, it will **show you the path** to that program.754755The most important part is that when you call `python`, that is the exact "`python`" that will be executed.756757So, you can confirm if you are in the correct virtual environment.758759/// tip760761It's easy to activate one virtual environment, get one Python, and then **go to another project**.762763And the second project **wouldn't work** because you are using the **incorrect Python**, from a virtual environment for another project.764765It's useful being able to check what `python` is being used. 🤓766767///768769## Why Deactivate a Virtual Environment770771For example, you could be working on a project `philosophers-stone`, **activate that virtual environment**, install packages and work with that environment.772773And then you want to work on **another project** `prisoner-of-azkaban`.774775You go to that project:776777<div class="termy">778779```console780$ cd ~/code/prisoner-of-azkaban781```782783</div>784785If you don't deactivate the virtual environment for `philosophers-stone`, when you run `python` in the terminal, it will try to use the Python from `philosophers-stone`.786787<div class="termy">788789```console790$ cd ~/code/prisoner-of-azkaban791792$ python main.py793794// Error importing sirius, it's not installed 😱795Traceback (most recent call last):796 File "main.py", line 1, in <module>797 import sirius798```799800</div>801802But if you deactivate the virtual environment and activate the new one for `prisoner-of-askaban` then when you run `python` it will use the Python from the virtual environment in `prisoner-of-azkaban`.803804<div class="termy">805806```console807$ cd ~/code/prisoner-of-azkaban808809// You don't need to be in the old directory to deactivate, you can do it wherever you are, even after going to the other project 😎810$ deactivate811812// Activate the virtual environment in prisoner-of-azkaban/.venv 🚀813$ source .venv/bin/activate814815// Now when you run python, it will find the package sirius installed in this virtual environment ✨816$ python main.py817818I solemnly swear 🐺819```820821</div>822823## Alternatives824825This is a simple guide to get you started and teach you how everything works **underneath**.826827There are many **alternatives** to managing virtual environments, package dependencies (requirements), projects.828829Once you are ready and want to use a tool to **manage the entire project**, packages dependencies, virtual environments, etc. I would suggest you try [uv](https://github.com/astral-sh/uv).830831`uv` can do a lot of things, it can:832833* **Install Python** for you, including different versions834* Manage the **virtual environment** for your projects835* Install **packages**836* Manage package **dependencies and versions** for your project837* Make sure you have an **exact** set of packages and versions to install, including their dependencies, so that you can be sure that you can run your project in production exactly the same as in your computer while developing, this is called **locking**838* And many other things839840## Conclusion841842If you read and understood all this, now **you know much more** about virtual environments than many developers out there. 🤓843844Knowing these details will most probably be useful in a future time when you are debugging something that seems complex, but you will know **how it all works underneath**. 😎35Read the [Virtual Environments guide](https://tiangolo.com/guides/virtual-environments/) to learn how virtual environments work underneath, including activation and the alternative `python -m venv` and `pip` workflow.
modified:mkdocs.yml-2 removed
80 unmodified lines
81 - Typer: index.md82 - features.md83 - "":84 - tutorial/index.md85 - environment-variables.md86 - virtual-environments.md85 - tutorial/install.md86 - tutorial/first-steps.md87 - tutorial/typer-app.md88 - tutorial/printing.md
142 unmodified lines
modified:README.md+15 added-12 removed
41 unmodified lines
42**Typer** is [FastAPI](https://fastapi.tiangolo.com)'s little sibling, it's the FastAPI of CLIs.4344## Installation4546Create and activate a [virtual environment](https://typer.tiangolo.com/virtual-environments/) and then install **Typer**:46First, [install `uv`](https://docs.astral.sh/uv/getting-started/installation/), and then add **Typer** to your project:4748<div class="termy">4950```console51$ pip install typer51$ uv add typer52---> 100%53Successfully installed typer rich shellingham53```5455</div>5657This installs both the Typer library and the `typer` command in the project's virtual environment. To use `typer` directly with shell completion, [activate the project environment and install completion](tutorial/install.md#activate-the-virtual-environment).5859If you prefer to use `pip`, install `typer` inside a virtual environment. See the [installation guide](tutorial/install.md) for the alternative steps.6061## Example6263### The absolute minimum64
73 unmodified lines
138<div class="termy">139140```console141// Run your application139$ python main.py142$ uv run python main.py143144// You get a nice error, you are missing 'name'145Usage: main.py [OPTIONS] {name}146Try 'main.py --help' for help.
2 unmodified lines
149╰───────────────────────────────────────────────────╯150151152// You get a --help for free150$ python main.py --help153$ uv run python main.py --help154155Usage: main.py [OPTIONS] {name}156157╭─ Arguments ───────────────────────────────────────╮
3 unmodified lines
161│ --help Show this message and exit. │162╰───────────────────────────────────────────────────╯163164// Now pass the 'name' argument162$ python main.py Camila165$ uv run python main.py Camila166167Hello Camila168169// It works! 🎉
51 unmodified lines
221222<div class="termy">223224```console222$ python main.py --help225$ uv run python main.py --help226227 Usage: main.py [OPTIONS] COMMAND [ARGS]...228229╭─ Options ─────────────────────────────────────────╮
24 unmodified lines
254255<div class="termy">256257```console255$ python main.py hello --help258$ uv run python main.py hello --help259260 Usage: main.py hello [OPTIONS] {name}261262╭─ Arguments ───────────────────────────────────────╮
10 unmodified lines
273274<div class="termy">275276```console274$ python main.py goodbye --help277$ uv run python main.py goodbye --help278279 Usage: main.py goodbye [OPTIONS] {name}280281╭─ Arguments ───────────────────────────────────────╮
16 unmodified lines
298299```console300// Use it with the hello command301299$ python main.py hello Camila302$ uv run python main.py hello Camila303304Hello Camila305306// And with the goodbye command307305$ python main.py goodbye Camila308$ uv run python main.py goodbye Camila309310Bye Camila!311312// And with --formal313311$ python main.py goodbye --formal Camila314$ uv run python main.py goodbye --formal Camila315316Goodbye Ms. Camila. Have a good day.317```318
62 unmodified lines