📝 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
Provenance
- Commit
e60a33e- Tree
6d0d261- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
aa09d51
55 files changed+514 added−1516 removed
modified:docs/tutorial/arguments/default.md+8 added-8 removed
22 unmodified lines232425262727282930313 unmodified lines3536373839394041424344444546474820 unmodified lines697071727373747576772 unmodified lines80818283848485868788888990919292939495969797989910010110222 unmodified lines<div class="termy">
```console// Check the help$ python main.py --help$ uv run python main.py --help
// Notice the [default: Wade Wilson] ✨Usage: main.py [OPTIONS] [name]
3 unmodified linesOptions: --help Show this message and exit.
// With no optional CLI argument$ python main.py$ uv run python main.py
Hello Wade Wilson
// With one CLI argument$ python main.py Camila$ uv run python main.py Camila
Hello Camila```
20 unmodified lines<div class="termy">
```console// Check the help$ python main.py --help$ uv run python main.py --help
Usage: main.py [OPTIONS] [name]
Arguments:2 unmodified linesOptions: --help Show this message and exit.
// Try it several times, it will use a random default each time$ python main.py$ uv run python main.py
Hello Deadpool
$ python main.py$ uv run python main.py
Hello Hiro
$ python main.py$ uv run python main.py
Hello Rick
// Now pass a value for the CLI argument$ python main.py Camila$ uv run python main.py Camila
Hello Camila```
</div>modified:docs/tutorial/arguments/envvar.md+24 added-11 removed
1 # CLI Arguments with Environment Variables2 3 You 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*.4 5+An **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.6+7 /// tip8 7-You can learn more about environment variables in the [Environment Variables](../../environment-variables.md) page.9+Read the [Environment Variables guide](https://tiangolo.com/guides/environment-variables/) for a detailed, cross-platform explanation.10 11 ///12 13 To do that, use the `envvar` parameter for `typer.Argument()`:5 unmodified lines19 <div class="termy">20 21 ```console22 // Check the help21-$ python main.py --help23+$ uv run python main.py --help24 25 Usage: main.py [OPTIONS] [name]26 27 Arguments:2 unmodified lines30 Options:31 --help Show this message and exit.32 33 // Call it without a CLI argument32-$ python main.py34+$ uv run python main.py35 36 Hello Mr. World37 38 // Now pass a value for the CLI argument37-$ python main.py Czernobog39+$ uv run python main.py Czernobog40 41 Hello Mr. Czernobog42 43 // And now use the environment variable42-$ AWESOME_NAME=Wednesday python main.py44+$ AWESOME_NAME=Wednesday uv run python main.py45 46 Hello Mr. Wednesday47 48 // CLI arguments take precedence over env vars47-$ AWESOME_NAME=Wednesday python main.py Czernobog49+$ AWESOME_NAME=Wednesday uv run python main.py Czernobog50 51 Hello Mr. Czernobog52 ```53 54 </div>55 56+/// tip | Windows PowerShell57+58+In PowerShell, set the environment variable first and then run the command:59+60+```console61+$ $Env:AWESOME_NAME = "Wednesday"62+$ uv run python main.py63+```64+65+///66+67 ## Multiple environment variables68 69 You 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 lines75 <div class="termy">76 77 ```console78 // Check the help66-$ python main.py --help79+$ uv run python main.py --help80 81 Usage: main.py [OPTIONS] [name]82 83 Arguments:2 unmodified lines86 Options:87 --help Show this message and exit.88 89 // Try the first env var77-$ AWESOME_NAME=Wednesday python main.py90+$ AWESOME_NAME=Wednesday uv run python main.py91 92 Hello Mr. Wednesday93 94 // Try the second env var82-$ GOD_NAME=Anubis python main.py95+$ GOD_NAME=Anubis uv run python main.py96 97 Hello Mr. Anubis98 ```99 10 unmodified lines110 <div class="termy">111 112 ```console113 //Check the help101-$ python main.py --help114+$ uv run python main.py --help115 116 // It won't show the env var117 Usage: main.py [OPTIONS] [name]118 3 unmodified lines122 Options:123 --help Show this message and exit.124 125 // But it will still be able to use it113-$ AWESOME_NAME=Wednesday python main.py126+$ AWESOME_NAME=Wednesday uv run python main.py127 128 Hello Mr. Wednesday129 ```130 1 unmodified linemodified:docs/tutorial/arguments/help.md+8 added-8 removed
17 unmodified lines18 19 <div class="termy">20 21 ```console22-$ python main.py --help22+$ uv run python main.py --help23 24 // Check the section with Arguments below 🚀25 Usage: main.py [OPTIONS] {name}26 16 unmodified lines43 44 <div class="termy">45 46 ```console47-$ python main.py --help47+$ uv run python main.py --help48 49 // Notice that we have the help text from the docstring and also the Arguments 📝50 Usage: main.py [OPTIONS] {name}51 18 unmodified lines70 71 <div class="termy">72 73 ```console74-$ python main.py --help74+$ uv run python main.py --help75 76 // Notice the [default: World] 🔍77 Usage: main.py [OPTIONS] [name]78 16 unmodified lines95 96 <div class="termy">97 98 ```console99-$ python main.py --help99+$ uv run python main.py --help100 101 // Notice the there's no [default: World] now 🔥102 Usage: main.py [OPTIONS] [name]103 18 unmodified lines122 123 <div class="termy">124 125 ```console126-$ python main.py --help126+$ uv run python main.py --help127 128 Usage: main.py [OPTIONS] [name]129 130 Arguments:36 unmodified lines167 168 <div class="termy">169 170 ```console171-$ python main.py --help171+$ uv run python main.py --help172 173 Usage: main.py [OPTIONS] [✨username✨]174 175 Arguments:19 unmodified lines195 196 <div class="termy">197 198 ```console199-$ python main.py --help199+$ uv run python main.py --help200 201 <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 lines236 237 <div class="termy">238 239 ```console240-$ python main.py --help240+$ uv run python main.py --help241 242 // Notice there's no Arguments section at all 🔥243 Usage: main.py [OPTIONS] [name]244 23 unmodified linesmodified:docs/tutorial/arguments/optional.md+4 added-4 removed
80 unmodified lines81 82 <div class="termy">83 84 ```console85-$ python main.py85+$ uv run python main.py86 87 Usage: main.py [OPTIONS] {name}88 Try "main.py --help" for help.89 39 unmodified lines129 <div class="termy">130 131 ```console132 // First check the help133-$ python main.py --help133+$ uv run python main.py --help134 135 Usage: main.py [OPTIONS] [name]136 137 Arguments:18 unmodified lines156 <div class="termy">157 158 ```console159 // With no CLI argument160-$ python main.py160+$ uv run python main.py161 162 Hello World!163 164 // With one optional CLI argument165-$ python main.py Camila165+$ uv run python main.py Camila166 167 Hello Camila168 ```169 69 unmodified linesmodified:docs/tutorial/commands/arguments.md+3 added-3 removed
6 unmodified lines789101111121314152 unmodified lines1819202122222324252627272829303117 unmodified lines6 unmodified lines<div class="termy">
```console// Check the help for create$ python main.py create --help$ uv run python main.py create --help
Usage: main.py create [OPTIONS] {username}
Arguments:2 unmodified linesOptions: --help Show this message and exit.
// Call it with a CLI argument$ python main.py create Camila$ uv run python main.py create Camila
Creating user: Camila
// The same for delete$ python main.py delete Camila$ uv run python main.py delete Camila
Deleting user: Camila```
17 unmodified linesmodified:docs/tutorial/commands/callback.md+8 added-8 removed
30 unmodified lines31 <div class="termy">32 33 ```console34 // Check the help35-$ python main.py --help35+$ uv run python main.py --help36 37 // Notice the main help text, extracted from the callback function: "Manage users in the awesome CLI app."38 Usage: main.py [OPTIONS] COMMAND [ARGS]...39 11 unmodified lines51 52 // Check the new top level CLI option --verbose53 54 // Try it normally55-$ python main.py create Camila55+$ uv run python main.py create Camila56 57 Creating user: Camila58 59 // And now with --verbose60-$ python main.py --verbose create Camila60+$ uv run python main.py --verbose create Camila61 62 Will write verbose output63 About to create a user64 Creating user: Camila65 Just created a user66 67 // 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 Camila69 70 Usage: main.py create [OPTIONS] {username}71 Try "main.py create --help" for help.72 14 unmodified lines87 88 <div class="termy">89 90 ```console91-$ python main.py create Camila91+$ uv run python main.py create Camila92 93 Running a command94 Creating user: Camila95 ```12 unmodified lines108 109 <div class="termy">110 111 ```console112-$ python main.py create Camila112+$ uv run python main.py create Camila113 114 // Notice that the message is the one from new_callback()115 Override callback, running a command116 Creating user: Camila15 unmodified lines132 133 <div class="termy">134 135 ```console136-$ python main.py --help136+$ uv run python main.py --help137 138 // Notice all the help text extracted from the callback docstring139 Usage: main.py [OPTIONS] COMMAND [ARGS]...140 11 unmodified lines152 Commands:153 create154 155 // And it just works as normally156-$ python main.py create Camila156+$ uv run python main.py create Camila157 158 Creating user: Camila159 ```160 1 unmodified linemodified:docs/tutorial/commands/context.md+7 added-7 removed
19 unmodified lines20 21 <div class="termy">22 23 ```console24-$ python main.py create Camila24+$ uv run python main.py create Camila25 26 // We get the message from the callback27 About to execute command: create28 Creating user: Camila29 30-$ python main.py delete Camila30+$ uv run python main.py delete Camila31 32 // We get the message from the callback, this time with delete33 About to execute command: delete34 Deleting user: Camila15 unmodified lines50 51 <div class="termy">52 53 ```console54-$ python main.py54+$ uv run python main.py55 56 // The callback is executed, we don't get the default help message57 Initializing database58 59 // Try with a command60-$ python main.py create Camila60+$ uv run python main.py create Camila61 62 // The callback is still executed63 Initializing database64 Creating user: Camila15 unmodified lines80 81 <div class="termy">82 83 ```console84-$ python main.py84+$ uv run python main.py85 86 // The callback is executed87 Initializing database88 89 // Check it with a subcommand90-$ python main.py create Camila90+$ uv run python main.py create Camila91 92 // This time the callback is not executed93 Creating user: Camila94 ```12 unmodified lines107 108 <div class="termy">109 110 ```console111-$ python main.py --name Camila --city Berlin111+$ uv run python main.py --name Camila --city Berlin112 113 Got extra arg: --name114 Got extra arg: Camila115 Got extra arg: --city10 unmodified linesmodified:docs/tutorial/commands/help.md+17 added-17 removed
10 unmodified lines11 <div class="termy">12 13 ```console14 // Check the new help15-$ python main.py --help15+$ uv run python main.py --help16 17 Usage: main.py [OPTIONS] COMMAND [ARGS]...18 19 Awesome CLI user manager.11 unmodified lines31 32 // Now the commands have inline help 🎉33 34 // Check the help for create35-$ python main.py create --help35+$ uv run python main.py create --help36 37 Usage: main.py create [OPTIONS] {username}38 39 Create a new user with username.4 unmodified lines44 Options:45 --help Show this message and exit.46 47 // Check the help for delete48-$ python main.py delete --help48+$ uv run python main.py delete --help49 50 Usage: main.py delete [OPTIONS] {username}51 52 Delete a user with username.7 unmodified lines60 --force / --no-force Force deletion without confirmation. [required]61 --help Show this message and exit.62 63 // Check the help for delete-all64-$ python main.py delete-all --help64+$ uv run python main.py delete-all --help65 66 Usage: main.py delete-all [OPTIONS]67 68 Delete ALL users in the database.4 unmodified lines73 --force / --no-force Force deletion without confirmation. [required]74 --help Show this message and exit.75 76 // Check the help for init77-$ python main.py init --help77+$ uv run python main.py init --help78 79 Usage: main.py init [OPTIONS]80 81 Initialize the users database.23 unmodified lines105 <div class="termy">106 107 ```console108 // Check the help109-$ python main.py --help109+$ uv run python main.py --help110 111 // Notice it uses the help passed to @app.command()112 Usage: main.py [OPTIONS] COMMAND [ARGS]...113 24 unmodified lines138 139 <div class="termy">140 141 ```console142-$ python main.py --help142+$ uv run python main.py --help143 144 <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 lines163 164 <div class="termy">165 166 ```console167-$ python main.py delete --help167+$ uv run python main.py delete --help168 169 <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 lines192 193 <div class="termy">194 195 ```console196-$ python main.py crate196+$ uv run python main.py crate197 198 <font color="#C4A000">Usage: </font>main.py [OPTIONS] COMMAND [ARGS]...199 <font color="#AAAAAA">Try </font><font color="#22436D">'main.py </font><font color="#4C6A8A"><b>--help</b></font><font color="#22436D">'</font><font color="#AAAAAA"> for help.</font>200 <font color="#CC0000">╭─ Error ───────────────────────────────────────────────────────────╮</font>34 unmodified lines235 236 <div class="termy">237 238 ```console239-$ python main.py create --help239+$ uv run python main.py create --help240 241 <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 lines258 259 <div class="termy">260 261 ```console262-$ python main.py delete --help262+$ uv run python main.py delete --help263 264 <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 lines292 293 <div class="termy">294 295 ```console296-$ python main.py create --help296+$ uv run python main.py create --help297 298 <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 lines320 321 <div class="termy">322 323 ```console324-$ python main.py delete --help324+$ uv run python main.py delete --help325 326 <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 lines362 363 <div class="termy">364 365 ```console366-$ python main.py --help366+$ uv run python main.py --help367 368 <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 lines410 411 <div class="termy">412 413 ```console414-$ python main.py create --help414+$ uv run python main.py create --help415 416 <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 lines444 445 <div class="termy">446 447 ```console448-$ python main.py --help448+$ uv run python main.py --help449 450 <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 lines479 480 <div class="termy">481 482 ```console483-$ python main.py --help483+$ uv run python main.py --help484 485 <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 linesmodified:docs/tutorial/commands/index.md+5 added-5 removed
64 unmodified lines65 <div class="termy">66 67 ```console68 // Check the help69-$ python main.py --help69+$ uv run python main.py --help70 71 Usage: main.py [OPTIONS] COMMAND [ARGS]...72 73 Options:5 unmodified lines79 create80 delete81 82 // Test them83-$ python main.py create83+$ uv run python main.py create84 85 Creating user: Hiro Hamada86 87-$ python main.py delete87+$ uv run python main.py delete88 89 Deleting user: Hiro Hamada90 91 // Now we have 2 commands! 🎉22 unmodified lines114 <div class="termy">115 116 ```console117 // Check the help without having to type --help118-$ python main.py118+$ uv run python main.py119 120 Usage: main.py [OPTIONS] COMMAND [ARGS]...121 122 Options:22 unmodified lines145 <div class="termy">146 147 ```console148 // Check the help149-$ python main.py --help149+$ uv run python main.py --help150 151 Usage: main.py [OPTIONS] COMMAND [ARGS]...152 153 Options:18 unmodified linesmodified:docs/tutorial/commands/name.md+2 added-2 removed
22 unmodified lines23 24 <div class="termy">25 26 ```console27-$ python main.py --help27+$ uv run python main.py --help28 29 Usage: main.py [OPTIONS] COMMAND [ARGS]...30 31 Options:5 unmodified lines37 create38 delete39 40 // Test it41-$ python main.py create Camila41+$ uv run python main.py create Camila42 43 Creating user: Camila44 ```45 11 unmodified linesmodified:docs/tutorial/commands/one-or-multiple.md+10 added-10 removed
8 unmodified lines9 <div class="termy">10 11 ```console12 // Without a CLI argument13-$ python main.py13+$ uv run python main.py14 15 Usage: main.py [OPTIONS] {name}16 Try "main.py --help" for help.17 18 Error: Missing argument 'name'.19 20 // With the 'name' CLI argument21-$ python main.py Camila21+$ uv run python main.py Camila22 23 Hello Camila24 25 // Asking for help26-$ python main.py --help26+$ uv run python main.py --help27 28 Usage: main.py [OPTIONS] {name}29 30 Options:19 unmodified lines50 <div class="termy">51 52 ```console53 // Check the help54-$ python main.py --help54+$ uv run python main.py --help55 56 Usage: main.py [OPTIONS] COMMAND [ARGS]...57 58 Options:5 unmodified lines64 create65 delete66 67 // Test the commands68-$ python main.py create68+$ uv run python main.py create69 70 Creating user: Hiro Hamada71 72-$ python main.py delete72+$ uv run python main.py delete73 74 Deleting user: Hiro Hamada75 ```76 12 unmodified lines89 <div class="termy">90 91 ```console92 // Check the help93-$ python main.py --help93+$ uv run python main.py --help94 95 // Notice the single command create96 Usage: main.py [OPTIONS] COMMAND [ARGS]...97 5 unmodified lines103 Commands:104 create105 106 // Try it107-$ python main.py create107+$ uv run python main.py create108 109 Creating user: Hiro Hamada110 ```111 9 unmodified lines121 122 <div class="termy">123 124 ```console125-$ python main.py --help125+$ uv run python main.py --help126 127 // Notice the help text from the docstring128 Usage: main.py [OPTIONS] COMMAND [ARGS]...129 9 unmodified lines139 Commands:140 create141 142 // And it still works the same, the callback does nothing143-$ python main.py create143+$ uv run python main.py create144 145 Creating user: Hiro Hamada146 ```147 1 unmodified linemodified:docs/tutorial/commands/options.md+7 added-7 removed
50 unmodified lines51 <div class="termy">52 53 ```console54 // Check the command create55-$ python main.py create Camila55+$ uv run python main.py create Camila56 57 Creating user: Camila58 59 // Now test the command delete60-$ python main.py delete Camila60+$ uv run python main.py delete Camila61 62 # Are you sure you want to delete the user? [y/n]: $ y63 64 Deleting user: Camila65 66-$ python main.py delete Wade66+$ uv run python main.py delete Wade67 68 # Are you sure you want to delete the user? [y/n]: $ n69 70 Operation cancelled71 72 // And finally, the command delete-all73 // Notice it doesn't have CLI arguments, only a CLI option74 75-$ python main.py delete-all75+$ uv run python main.py delete-all76 77 # Are you sure you want to delete ALL users? [y/n]: $ y78 79 Deleting all users80 81-$ python main.py delete-all81+$ uv run python main.py delete-all82 83 # Are you sure you want to delete ALL users? [y/n]: $ n84 85 Operation cancelled86 87 // And if you pass the --force CLI option, it doesn't need to confirm88 89-$ python main.py delete-all --force89+$ uv run python main.py delete-all --force90 91 Deleting all users92 93 // And init that doesn't take any CLI parameter94-$ python main.py init94+$ uv run python main.py init95 96 Initializing user database97 ```98 1 unmodified linemodified:docs/tutorial/multiple-values/arguments-with-multiple-values.md+5 added-5 removed
9 unmodified lines10 11 <div class="termy">12 13 ```console14-$ python main.py ./index.md ./first-steps.md woohoo!14+$ uv run python main.py ./index.md ./first-steps.md woohoo!15 16 This file exists: index.md17 woohoo!18 This file exists: first-steps.md25 unmodified lines44 <div class="termy">45 46 ```console47 // Check the help48-$ python main.py --help48+$ uv run python main.py --help49 50 Usage: main.py [OPTIONS] [names]...51 52 Arguments:2 unmodified lines55 Options:56 --help Show this message and exit.57 58 // Use it with its defaults59-$ python main.py59+$ uv run python main.py60 61 Hello Harry62 Hello Hermione63 Hello Ron64 65 // If you pass an invalid number of arguments you will get an error66-$ python main.py Draco Hagrid66+$ uv run python main.py Draco Hagrid67 68 Error: Argument 'names' takes 3 values69 70 // 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 Dobby72 73 Hello Draco74 Hello Hagrid75 Hello Dobby3 unmodified linesmodified:docs/tutorial/multiple-values/multiple-options.md+6 added-6 removed
14 unmodified lines15 <div class="termy">16 17 ```console18 // The default value is 'None'19-$ python main.py19+$ uv run python main.py20 21 No provided users (raw input = None)22 Aborted!23 24 // Now pass a user25-$ python main.py --user Camila25+$ uv run python main.py --user Camila26 27 Processing user: Camila28 29 // 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 Morty31 32 Processing user: Camila33 Processing user: Rick34 Processing user: Morty11 unmodified lines46 47 <div class="termy">48 49 ```console50-$ python main.py50+$ uv run python main.py51 52 The sum is 053 54 // Try with some numbers55-$ python main.py --number 255+$ uv run python main.py --number 256 57 The sum is 2.058 59 // 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.561 62 The sum is 9.563 ```64 1 unmodified linemodified:docs/tutorial/multiple-values/options-with-multiple-values.md+4 added-4 removed
54 unmodified lines55 <div class="termy">56 57 ```console58 // check the help59-$ python main.py --help59+$ uv run python main.py --help60 61 // Notice the <str int boolean>62 Usage: main.py [OPTIONS]63 1 unmodified line65 --user <str int boolean>...66 --help Show this message and exit.67 68 // Now try it69-$ python main.py --user Camila 50 yes69+$ uv run python main.py --user Camila 50 yes70 71 The username Camila has 50 coins72 And this user is a wizard!73 74 // With other values75-$ python main.py --user Morty 3 no75+$ uv run python main.py --user Morty 3 no76 77 The username Morty has 3 coins78 79 // Try with invalid values (not enough)80-$ python main.py --user Camila 5080+$ uv run python main.py --user Camila 5081 82 Error: Option '--user' requires 3 arguments83 ```84 1 unmodified linemodified:docs/tutorial/options/callback-and-context.md+3 added-3 removed
21 unmodified lines22 23 <div class="termy">24 25 ```console26-$ python main.py --name Camila26+$ uv run python main.py --name Camila27 28 Hello Camila29 30-$ python main.py --name Rick30+$ uv run python main.py --name Rick31 32 Usage: main.py [OPTIONS]33 34 // We get the error from the callback134 unmodified lines169 170 <div class="termy">171 172 ```console173-$ python main.py --name Camila173+$ uv run python main.py --name Camila174 175 Validating param: name176 Hello Camila177 ```28 unmodified linesmodified:docs/tutorial/options/help.md+6 added-6 removed
27 unmodified lines28 29 <div class="termy">30 31 ```console32-$ python main.py --help32+$ uv run python main.py --help33 34 Usage: main.py [OPTIONS] {name}35 36 Say hi to 'name', optionally with a --lastname.27 unmodified lines64 65 <div class="termy">66 67 ```console68-$ python main.py --help68+$ uv run python main.py --help69 70 <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 lines108 109 <div class="termy">110 111 ```console112-$ python main.py112+$ uv run python main.py113 114 Hello Wade Wilson115 116 // Show the help117-$ python main.py --help117+$ uv run python main.py --help118 119 Usage: main.py [OPTIONS]120 121 Options:15 unmodified lines137 138 <div class="termy">139 140 ```console141-$ python main.py141+$ uv run python main.py142 143 Hello Wade Wilson144 145 // Show the help146-$ python main.py --help146+$ uv run python main.py --help147 148 Usage: main.py [OPTIONS]149 150 Options:7 unmodified linesmodified:docs/tutorial/options/name.md+12 added-12 removed
45 unmodified lines46 47 <div class="termy">48 49 ```console50-$ python main.py --help50+$ uv run python main.py --help51 52 // Notice the --name instead of --user-name53 Usage: main.py [OPTIONS]54 1 unmodified line56 --name <str> [required]57 --help Show this message and exit.58 59 // Try it60-$ python main.py --name Camila60+$ uv run python main.py --name Camila61 62 Hello Camila63 ```64 129 unmodified lines194 <div class="termy">195 196 ```console197 // Check the help198-$ python main.py --help198+$ uv run python main.py --help199 200 // Notice the two CLI option names -n and --name201 Usage: main.py [OPTIONS]202 1 unmodified line204 -n, --name <str> [required]205 --help Show this message and exit.206 207 // Try the short version208-$ python main.py -n Camila208+$ uv run python main.py -n Camila209 210 Hello Camila211 ```212 9 unmodified lines222 223 <div class="termy">224 225 ```console226-$ python main.py --help226+$ uv run python main.py --help227 228 // Notice there's no --name nor --user-name, only -n229 Usage: main.py [OPTIONS]230 1 unmodified line232 -n <str> [required]233 --help Show this message and exit.234 235 // Try it236-$ python main.py -n Camila236+$ uv run python main.py -n Camila237 238 Hello Camila239 ```240 9 unmodified lines250 251 <div class="termy">252 253 ```console254-$ python main.py --help254+$ uv run python main.py --help255 256 // Notice that we have the long version --user-name back257 // and we also have the short version -n258 Usage: main.py [OPTIONS]2 unmodified lines261 -n, --user-name <str> [required]262 --help Show this message and exit.263 264 // Try it265-$ python main.py --user-name Camila265+$ uv run python main.py --user-name Camila266 267 Hello Camila268 269 // And try the short version270-$ python main.py -n Camila270+$ uv run python main.py -n Camila271 ```272 273 </div>274 15 unmodified lines290 291 <div class="termy">292 293 ```console294-$ python main.py --help294+$ uv run python main.py --help295 296 // We now have short versions -n and -f297 // And also long versions --name and --formal298 Usage: main.py [OPTIONS]3 unmodified lines302 -f, --formal303 --help Show this message and exit.304 305 // Try the short versions306-$ python main.py -n Camila -f306+$ uv run python main.py -n Camila -f307 308 Good day Ms. Camila.309 310 // 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 Camila313 314 Good day Ms. Camila.315 ```316 1 unmodified linemodified:docs/tutorial/options/password.md+2 added-2 removed
7 unmodified lines8 9 <div class="termy">10 11 ```console12-$ python main.py Camila12+$ uv run python main.py Camila13 14 // It prompts for the email15 # Email: $ camila@example.com16 # Repeat for confirmation: $ camila@example.com19 unmodified lines36 37 <div class="termy">38 39 ```console40-$ python main.py Camila40+$ uv run python main.py Camila41 42 // It prompts for the password, but doesn't show anything when you type43 # Password: $44 # Repeat for confirmation: $7 unmodified linesmodified:docs/tutorial/options/prompt.md+4 added-4 removed
8 unmodified lines9 <div class="termy">10 11 ```console12 // Call it with the 'name' CLI argument13-$ python main.py Camila13+$ uv run python main.py Camila14 15 // It asks for the missing CLI option --lastname16 # Lastname: $ Gutiérrez17 13 unmodified lines31 <div class="termy">32 33 ```console34 // Call it with the 'name' CLI argument35-$ python main.py Camila35+$ uv run python main.py Camila36 37 // It uses the custom prompt38 # Please tell me your last name: $ Gutiérrez39 16 unmodified lines56 57 <div class="termy">58 59 ```console60-$ python main.py60+$ uv run python main.py61 62 // Your app will first prompt for the project name, and then for the confirmation63 # Project name: $ Old Project64 # Repeat for confirmation: $ Old Project65 66 Deleting project Old Project67 68 // If the user doesn't type the same, receives an error and a new prompt69-$ python main.py69+$ uv run python main.py70 71 # Project name: $ Old Project72 # Repeat for confirmation: $ New Spice73 11 unmodified linesmodified:docs/tutorial/options/required.md+3 added-3 removed
41 unmodified lines42 <div class="termy">43 44 ```console45 // Pass the 'name' CLI argument46-$ python main.py Camila46+$ uv run python main.py Camila47 48 // We didn't pass the now required --lastname CLI option49 Usage: main.py [OPTIONS] {name}50 Try "main.py --help" for help.51 52 Error: Missing option '--lastname'.53 54 // 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érrez56 57 Hello Camila Gutiérrez58 59 // And if you check the help60-$ python main.py --help60+$ uv run python main.py --help61 62 Usage: main.py [OPTIONS] {name}63 64 Arguments:10 unmodified linesmodified:docs/tutorial/options/version.md+5 added-5 removed
25 unmodified lines26 27 <div class="termy">28 29 ```console30-$ python main.py --help30+$ uv run python main.py --help31 32 // We get a --version, and don't get an awkward --no-version 🎉33 Usage: main.py [OPTIONS]34 3 unmodified lines38 --help Show this message and exit.39 40 41 // We can call it normally42-$ python main.py --name Camila42+$ uv run python main.py --name Camila43 44 Hello Camila45 46 // And we can get the version47-$ python main.py --version47+$ uv run python main.py --version48 49 Awesome CLI Version: 0.1.050 51 // Because we exit in the callback, we don't get a "Hello World" message after the version 🚀11 unmodified lines63 64 <div class="termy">65 66 ```console67-$ python main.py --name Rick --version67+$ uv run python main.py --name Rick --version68 69 Only Camila is allowed70 Aborted!71 ```30 unmodified lines102 103 <div class="termy">104 105 ```console106-$ python main.py --name Rick --version106+$ uv run python main.py --name Rick --version107 108 // Now we only get the version, and the name is not used109 Awesome CLI Version: 0.1.0110 ```2 unmodified linesmodified:docs/tutorial/parameter-types/bool.md+15 added-15 removed
16 unmodified lines17 <div class="termy">18 19 ```console20 // Check the help21-$ python main.py --help21+$ uv run python main.py --help22 23 // Notice there's only --force, we no longer have --no-force24 Usage: main.py [OPTIONS]25 1 unmodified line27 --force28 --help Show this message and exit.29 30 // Try it:31-$ python main.py31+$ uv run python main.py32 33 Not forcing34 35 // Now add --force36-$ python main.py --force36+$ uv run python main.py --force37 38 Forcing operation39 40 // And --no-force no longer exists ⛔️41-$ python main.py --no-force41+$ uv run python main.py --no-force42 43 Usage: main.py [OPTIONS]44 Try "main.py --help" for help.45 19 unmodified lines65 <div class="termy">66 67 ```console68 // Check the help69-$ python main.py --help69+$ uv run python main.py --help70 71 // Notice the --accept / --reject72 Usage: main.py [OPTIONS]73 1 unmodified line75 --accept / --reject76 --help Show this message and exit.77 78 // Try it79-$ python main.py79+$ uv run python main.py80 81 I don't know what you want yet82 83 // Now pass --accept84-$ python main.py --accept84+$ uv run python main.py --accept85 86 Accepting!87 88 // And --reject89-$ python main.py --reject89+$ uv run python main.py --reject90 91 Rejecting!92 ```93 12 unmodified lines106 <div class="termy">107 108 ```console109 // Check the help110-$ python main.py --help110+$ uv run python main.py --help111 112 // Notice the -f, --force / -F, --no-force113 Usage: main.py [OPTIONS]114 1 unmodified line116 -f, --force / -F, --no-force [default: no-force]117 --help Show this message and exit.118 119 // Try with the short name -f120-$ python main.py -f120+$ uv run python main.py -f121 122 Forcing operation123 124 // Try with the short name -F125-$ python main.py -F125+$ uv run python main.py -F126 127 Not forcing128 ```129 20 unmodified lines150 <div class="termy">151 152 ```console153 // Check the help154-$ python main.py --help154+$ uv run python main.py --help155 156 // Notice the / -d, --demo157 Usage: main.py [OPTIONS]158 1 unmodified line160 / -d, --demo [default: True]161 --help Show this message and exit.162 163 // Try it164-$ python main.py164+$ uv run python main.py165 166 Running in production167 168 // Now pass --demo169-$ python main.py --demo169+$ uv run python main.py --demo170 171 Running demo172 173 // And the short version174-$ python main.py -d174+$ uv run python main.py -d175 176 Running demo177 ```178 1 unmodified linemodified:docs/tutorial/parameter-types/datetime.md+5 added-5 removed
15 unmodified lines16 17 <div class="termy">18 19 ```console20-$ python main.py --help20+$ uv run python main.py --help21 22 Usage: main.py [OPTIONS] {birth}:<%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S>23 24 Arguments:2 unmodified lines27 Options:28 --help Show this message and exit.29 30 // Pass a datetime31-$ python main.py 1956-01-31T10:00:0031+$ uv run python main.py 1956-01-31T10:00:0032 33 Interesting day to be born: 1956-01-31 10:00:0034 Birth hour: 1035 36 // An invalid date37-$ python main.py july-19-198937+$ uv run python main.py july-19-198938 39 Usage: main.py [OPTIONS] {birth}:<%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S>40 Try 'main.py --help' for help.41 30 unmodified lines72 <div class="termy">73 74 ```console75 // ISO dates work76-$ python main.py 1969-10-2976+$ uv run python main.py 1969-10-2977 78 Launch will be at: 1969-10-29 00:00:0079 80 // But the strange custom format also works81-$ python main.py 10/29/196981+$ uv run python main.py 10/29/196982 83 Launch will be at: 1969-10-29 00:00:0084 ```85 1 unmodified linemodified:docs/tutorial/parameter-types/enum.md+13 added-13 removed
15 unmodified lines16 17 <div class="termy">18 19 ```console20-$ python main.py --help20+$ uv run python main.py --help21 22 // Notice the predefined values <simple|conv|lstm>23 Usage: main.py [OPTIONS]24 1 unmodified line26 --network <simple|conv|lstm> [default: simple]27 --help Show this message and exit.28 29 // Try it30-$ python main.py --network conv30+$ uv run python main.py --network conv31 32 Training neural network of type: conv33 34 // Invalid value35-$ python main.py --network capsule35+$ uv run python main.py --network capsule36 37 Usage: main.py [OPTIONS]38 Try "main.py --help" for help.39 40 Error: Invalid value for '--network': 'capsule' is not one of 'simple', 'conv', 'lstm'.41 42 // Note that enums are case sensitive by default43-$ python main.py --network CONV43+$ uv run python main.py --network CONV44 45 Usage: main.py [OPTIONS]46 Try "main.py --help" for help.47 13 unmodified lines61 <div class="termy">62 63 ```console64 // Notice the upper case CONV65-$ python main.py --network CONV65+$ uv run python main.py --network CONV66 67 Training neural network of type: conv68 69 // A mix also works70-$ python main.py --network LsTm70+$ uv run python main.py --network LsTm71 72 Training neural network of type: lstm73 ```74 9 unmodified lines84 85 <div class="termy">86 87 ```console88-$ python main.py --help88+$ uv run python main.py --help89 90 // Notice the default values being shown91 Usage: main.py [OPTIONS]92 1 unmodified line94 --groceries <Eggs|Bacon|Cheese> [default: Eggs, Cheese]95 --help Show this message and exit.96 97 // Try it with the default values98-$ python main.py98+$ uv run python main.py99 100 Buying groceries: Eggs, Cheese101 102 // Try it with a single value103-$ python main.py --groceries "Eggs"103+$ uv run python main.py --groceries "Eggs"104 105 Buying groceries: Eggs106 107 // Try it with multiple values108-$ python main.py --groceries "Eggs" --groceries "Bacon"108+$ uv run python main.py --groceries "Eggs" --groceries "Bacon"109 110 Buying groceries: Eggs, Bacon111 ```112 7 unmodified lines120 121 <div class="termy">122 123 ```console124-$ python main.py --help124+$ uv run python main.py --help125 126 // Notice the predefined values <simple|conv|lstm>127 Usage: main.py [OPTIONS]128 1 unmodified line130 --network <simple|conv|lstm> [default: simple]131 --help Show this message and exit.132 133 // Try it134-$ python main.py --network conv134+$ uv run python main.py --network conv135 136 Training neural network of type: conv137 138 // Invalid value139-$ python main.py --network capsule139+$ uv run python main.py --network capsule140 141 Usage: main.py [OPTIONS]142 Try "main.py --help" for help.143 4 unmodified linesmodified:docs/tutorial/parameter-types/file.md+7 added-7 removed
59 unmodified lines60 // Add another line to the config to test it61 $ echo "some more settings" >> config.txt62 63 // Now run your program64-$ python main.py --config config.txt64+$ uv run python main.py --config config.txt65 66 Config line: some settings67 68 Config line: some more settings20 unmodified lines89 90 <div class="termy">91 92 ```console93-$ python main.py --config text.txt93+$ uv run python main.py --config text.txt94 95 Config written96 97 // Check the contents of the file26 unmodified lines124 125 <div class="termy">126 127 ```console128-$ python main.py --file lena.jpg128+$ uv run python main.py --file lena.jpg129 130 Processed bytes total: 512131 Processed bytes total: 1024132 Processed bytes total: 153618 unmodified lines151 152 <div class="termy">153 154 ```console155-$ python main.py --file binary.dat155+$ uv run python main.py --file binary.dat156 157 Binary file written158 159 // Check the binary file was created49 unmodified lines209 210 <div class="termy">211 212 ```console213-$ python main.py --config config.txt213+$ uv run python main.py --config config.txt214 215 Config line written216 217 // 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.txt219 220 Config line written221 222-$ python main.py --config config.txt222+$ uv run python main.py --config config.txt223 224 Config line written225 226 // Check the contents of the file, it should have each of the 3 lines appended8 unmodified linesmodified:docs/tutorial/parameter-types/index.md+3 added-3 removed
19 unmodified lines20 21 <div class="termy">22 23 ```console24-$ python main.py --help24+$ uv run python main.py --help25 26 // Notice how --age is an 'int' (integer) and --height-meters is a 'float'27 Usage: main.py [OPTIONS] {name}28 6 unmodified lines35 --female / --no-female [default: female]36 --help Show this message and exit.37 38 // 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 --female40 41 // All the data has the correct Python type42 name 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'46 47 // And if you pass an incorrect type48-$ python main.py Camila --age 15.348+$ uv run python main.py Camila --age 15.349 50 Usage: main.py [OPTIONS] {name}51 Try "main.py --help" for help.52 10 unmodified linesmodified:docs/tutorial/parameter-types/number.md+14 added-14 removed
11 unmodified lines12 13 <div class="termy">14 15 ```console16-$ python main.py --help16+$ uv run python main.py --help17 18 // Notice the extra range information in the help text for --age and --score19 Usage: main.py [OPTIONS] {ID}20 5 unmodified lines26 --score <float range> [default: 0; x<=100]27 --help Show this message and exit.28 29 // Pass all the CLI parameters30-$ python main.py 5 --age 20 --score 9030+$ uv run python main.py 5 --age 20 --score 9031 32 ID is 533 --age is 2034 --score is 90.035 36 // Pass an invalid ID37-$ python main.py 100237+$ uv run python main.py 100238 39 Usage: main.py [OPTIONS] {ID}40 Try "main.py --help" for help.41 42 Error: Invalid value for 'ID': 1002 is not in the range 0<=x<=1000.43 44 // Pass an invalid age45-$ python main.py 5 --age 1545+$ uv run python main.py 5 --age 1546 47 Usage: main.py [OPTIONS] {ID}48 Try "main.py --help" for help.49 50 Error: Invalid value for '--age': 15 is not in the range x>=18.51 52 // Pass an invalid score53-$ python main.py 5 --age 20 --score 100.553+$ uv run python main.py 5 --age 20 --score 100.554 55 Usage: main.py [OPTIONS] {ID}56 Try "main.py --help" for help.57 58 Error: Invalid value for '--score': 100.5 is not in the range x<=100.59 60 // 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 -562 63 ID is 564 --age is 2065 --score is -5.014 unmodified lines80 <div class="termy">81 82 ```console83 // ID doesn't have clamp, so it shows an error84-$ python main.py 100284+$ uv run python main.py 100285 86 Usage: main.py [OPTIONS] {ID}87 Try "main.py --help" for help.88 89 Error: Invalid value for 'ID': 1002 is not in the range 0<=x<=1000.90 91 // But --rank and --score use clamp92-$ python main.py 5 --rank 11 --score -592+$ uv run python main.py 5 --rank 11 --score -593 94 ID is 595 --rank is 1096 --score is 014 unmodified lines111 <div class="termy">112 113 ```console114 // Check it115-$ python main.py115+$ uv run python main.py116 117 Verbose level is 0118 119 // Now use one --verbose120-$ python main.py --verbose120+$ uv run python main.py --verbose121 122 Verbose level is 1123 124 // Now 3 --verbose125-$ python main.py --verbose --verbose --verbose125+$ uv run python main.py --verbose --verbose --verbose126 127 Verbose level is 3128 129 // And with the short name130-$ python main.py -v130+$ uv run python main.py -v131 132 Verbose level is 1133 134 // And with the short name 3 times135-$ python main.py -v -v -v135+$ uv run python main.py -v -v -v136 137 Verbose level is 3138 139 // As short names can be put together, this also works140-$ python main.py -vvv140+$ uv run python main.py -vvv141 142 Verbose level is 3143 ```144 1 unmodified linemodified:docs/tutorial/parameter-types/path.md+7 added-7 removed
12 unmodified lines13 <div class="termy">14 15 ```console16 // No config17-$ python main.py17+$ uv run python main.py18 19 Aborted.20 No config file21 22 // Pass a config that doesn't exist23-$ python main.py --config config.txt23+$ uv run python main.py --config config.txt24 25 The config doesn't exist26 27 // Now create a quick config28 $ echo "some settings" > config.txt29 30 // And try again31-$ python main.py --config config.txt31+$ uv run python main.py --config config.txt32 33 Config file contents: some settings34 35 // And with a directory36-$ python main.py --config ./36+$ uv run python main.py --config ./37 38 Config is a directory, will use all its config files39 ```40 24 unmodified lines65 66 <div class="termy">67 68 ```console69-$ python main.py --config config.txt69+$ uv run python main.py --config config.txt70 71 Usage: main.py [OPTIONS]72 Try "main.py --help" for help.73 2 unmodified lines76 // Now create a quick config77 $ echo "some settings" > config.txt78 79 // And try again80-$ python main.py --config config.txt80+$ uv run python main.py --config config.txt81 82 Config file contents: some settings83 84 // And with a directory85-$ python main.py --config ./85+$ uv run python main.py --config ./86 87 Usage: main.py [OPTIONS]88 Try "main.py --help" for help.89 17 unmodified linesmodified:docs/tutorial/parameter-types/uuid.md+2 added-2 removed
30 unmodified lines31 <div class="termy">32 33 ```console34 // Pass a valid UUID v435-$ python main.py d48edaa6-871a-4082-a196-4daab372d4a135+$ uv run python main.py d48edaa6-871a-4082-a196-4daab372d4a136 37 User ID is d48edaa6-871a-4082-a196-4daab372d4a138 UUID version is: 439 40 // An invalid value41-$ python main.py 7479706572-72756c657341+$ uv run python main.py 7479706572-72756c657342 43 Usage: main.py [OPTIONS] {user_id}44 Try "main.py --help" for help.45 4 unmodified linesmodified:docs/tutorial/subcommands/add-typer.md+8 added-8 removed
15 unmodified lines16 17 <div class="termy">18 19 ```console20-$ python items.py create Wand20+$ uv run python items.py create Wand21 22 Creating item: Wand23 ```24 11 unmodified lines36 37 <div class="termy">38 39 ```console40-$ python users.py create Camila40+$ uv run python users.py create Camila41 42 Creating user: Camila43 ```44 28 unmodified lines73 <div class="termy">74 75 ```console76 // Check the help77-$ python main.py --help77+$ uv run python main.py --help78 79 Usage: main.py [OPTIONS] COMMAND [ARGS]...80 81 Options:15 unmodified lines97 <div class="termy">98 99 ```console100 // Check the help for items101-$ python main.py items --help101+$ uv run python main.py items --help102 103 // It shows its own commands (subcommands): create, delete, sell104 Usage: main.py items [OPTIONS] COMMAND [ARGS]...105 5 unmodified lines111 delete112 sell113 114 // Try it115-$ python main.py items create Wand115+$ uv run python main.py items create Wand116 117 Creating item: Wand118 119-$ python main.py items sell Vase119+$ uv run python main.py items sell Vase120 121 Selling item: Vase122 ```123 9 unmodified lines133 134 <div class="termy">135 136 ```console137-$ python main.py users --help137+$ uv run python main.py users --help138 139 Usage: main.py users [OPTIONS] COMMAND [ARGS]...140 141 Options:3 unmodified lines145 create146 delete147 148 // Try it149-$ python main.py users create Camila149+$ uv run python main.py users create Camila150 151 Creating user: Camila152 ```153 15 unmodified linesmodified:docs/tutorial/subcommands/callback-override.md+4 added-4 removed
13 unmodified lines14 15 <div class="termy">16 17 ```console18-$ python main.py users create Camila18+$ uv run python main.py users create Camila19 20 // Notice the first message is not created by the command function but by the callback21 Running a users command22 Creating user: Camila13 unmodified lines36 37 <div class="termy">38 39 ```console40-$ python main.py users create Camila40+$ uv run python main.py users create Camila41 42 Running a users command43 Creating user: Camila44 ```16 unmodified lines61 62 <div class="termy">63 64 ```console65-$ python main.py users create Camila65+$ uv run python main.py users create Camila66 67 // Notice the message from the new callback68 Callback override, running users command69 Creating user: Camila17 unmodified lines87 88 <div class="termy">89 90 ```console91-$ python users create Camila91+$ uv run python users create Camila92 93 // Notice the message from the callback added in add_typer()94 I have the high land! Running users command95 Creating user: Camila3 unmodified linesmodified:docs/tutorial/subcommands/name-and-help.md+16 added-16 removed
18 unmodified lines19 <div class="termy">20 21 ```console22 // Check the main help23-$ python main.py --help23+$ uv run python main.py --help24 25 Usage: main.py [OPTIONS] COMMAND [ARGS]...26 27 Options:4 unmodified lines32 Commands:33 users Manage users in the app.34 35 // Check the help for the users command36-$ python main.py users --help36+$ uv run python main.py users --help37 38 Usage: main.py users [OPTIONS] COMMAND [ARGS]...39 40 Manage users in the app.55 unmodified lines96 <div class="termy">97 98 ```console99 // Check the main help100-$ python main.py --help100+$ uv run python main.py --help101 102 // Notice the help text "Manage users in the app."103 Usage: main.py [OPTIONS] COMMAND [ARGS]...104 5 unmodified lines110 Commands:111 users Manage users in the app.112 113 // Check the help for the users command114-$ python main.py users --help114+$ uv run python main.py users --help115 116 // Notice the main description: "Manage users in the app."117 Usage: main.py users [OPTIONS] COMMAND [ARGS]...118 31 unmodified lines150 <div class="termy">151 152 ```console153 // Check the main help154-$ python main.py --help154+$ uv run python main.py --help155 156 // Notice the help text "Manage users in the app."157 Usage: main.py [OPTIONS] COMMAND [ARGS]...158 5 unmodified lines164 Commands:165 users Manage users in the app.166 167 // Check the help for the users command168-$ python main.py users --help168+$ uv run python main.py users --help169 170 // Notice the main description: "Manage users in the app."171 Usage: main.py users [OPTIONS] COMMAND [ARGS]...172 21 unmodified lines194 <div class="termy">195 196 ```console197 // Check the main help198-$ python main.py --help198+$ uv run python main.py --help199 200 // Notice the help text "Manage users in the app."201 Usage: main.py [OPTIONS] COMMAND [ARGS]...202 5 unmodified lines208 Commands:209 users Manage users in the app.210 211 // Check the help for the users command212-$ python main.py users --help212+$ uv run python main.py users --help213 214 // Notice the main description: "Manage users in the app."215 Usage: main.py users [OPTIONS] COMMAND [ARGS]...216 25 unmodified lines242 <div class="termy">243 244 ```console245 // Check the main help246-$ python main.py --help246+$ uv run python main.py --help247 248 // Check the command new-users and its help text249 Usage: main.py [OPTIONS] COMMAND [ARGS]...250 5 unmodified lines256 Commands:257 new-users I have the highland! Create some users.258 259 // Now check the help for the new-users command260-$ python main.py new-users --help260+$ uv run python main.py new-users --help261 262 // Notice the help text263 Usage: main.py new-users [OPTIONS] COMMAND [ARGS]...264 55 unmodified lines320 <div class="termy">321 322 ```console323 // Check the main help324-$ python main.py --help324+$ uv run python main.py --help325 326 // Notice the command name is exp-users and the help text is "Explicit help."327 Usage: main.py [OPTIONS] COMMAND [ARGS]...328 5 unmodified lines334 Commands:335 exp-users Explicit help.336 337 // Check the help for the exp-users command338-$ python main.py exp-users --help338+$ uv run python main.py exp-users --help339 340 // Notice the main help text341 Usage: main.py exp-users [OPTIONS] COMMAND [ARGS]...342 23 unmodified lines366 <div class="termy">367 368 ```console369 // Check the help370-$ python main.py --help370+$ uv run python main.py --help371 372 // The help text is now "Help from callback for users.".373 Usage: main.py [OPTIONS] COMMAND [ARGS]...374 5 unmodified lines380 Commands:381 users Help from callback for users.382 383 // Check the users command help384-$ python main.py users --help384+$ uv run python main.py users --help385 386 // Notice the main help text387 Usage: main.py users [OPTIONS] COMMAND [ARGS]...388 21 unmodified lines410 <div class="termy">411 412 ```console413 // Check the help414-$ python main.py --help414+$ uv run python main.py --help415 416 // Notice the command name cake-sith-users and the new help text "Unlimited powder! Eh, users."417 Usage: main.py [OPTIONS] COMMAND [ARGS]...418 5 unmodified lines424 Commands:425 cake-sith-users Unlimited powder! Eh, users.426 427 // 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 --help429 430 // Notice the main help text431 Usage: main.py cake-sith-users [OPTIONS] COMMAND [ARGS]...432 27 unmodified linesmodified:docs/tutorial/subcommands/nested-subcommands.md+18 added-18 removed
18 unmodified lines19 <div class="termy">20 21 ```console22 // Check the help23-$ python reigns.py --help23+$ uv run python reigns.py --help24 25 Usage: reigns.py [OPTIONS] COMMAND [ARGS]...26 27 Options:5 unmodified lines33 conquer34 destroy35 36 // Try it37-$ python reigns.py conquer Cintra37+$ uv run python reigns.py conquer Cintra38 39 Conquering reign: Cintra40 41-$ python reigns.py destroy Mordor41+$ uv run python reigns.py destroy Mordor42 43 Destroying reign: Mordor44 ```45 10 unmodified lines56 <div class="termy">57 58 ```console59 // Check the help60-$ python towns.py --help60+$ uv run python towns.py --help61 62 Usage: towns.py [OPTIONS] COMMAND [ARGS]...63 64 Options:5 unmodified lines70 found71 burn72 73 // Try it74-$ python towns.py found "New Asgard"74+$ uv run python towns.py found "New Asgard"75 76 Founding town: New Asgard77 78-$ python towns.py burn Vizima78+$ uv run python towns.py burn Vizima79 80 Burning town: Vizima81 ```82 12 unmodified lines95 <div class="termy">96 97 ```console98 // Check the help99-$ python lands.py --help99+$ uv run python lands.py --help100 101 Usage: lands.py [OPTIONS] COMMAND [ARGS]...102 103 Options:5 unmodified lines109 reigns110 towns111 112 // We still have the help for reigns113-$ python lands.py reigns --help113+$ uv run python lands.py reigns --help114 115 Usage: lands.py reigns [OPTIONS] COMMAND [ARGS]...116 117 Options:3 unmodified lines121 conquer122 destroy123 124 // And the help for towns125-$ python lands.py towns --help125+$ uv run python lands.py towns --help126 127 Usage: lands.py towns [OPTIONS] COMMAND [ARGS]...128 129 Options:11 unmodified lines141 <div class="termy">142 143 ```console144 // Try the reigns command145-$ python lands.py reigns conquer Gondor145+$ uv run python lands.py reigns conquer Gondor146 147 Conquering reign: Gondor148 149-$ python lands.py reigns destroy Nilfgaard149+$ uv run python lands.py reigns destroy Nilfgaard150 151 Destroying reign: Nilfgaard152 153 // Try the towns command154-$ python lands.py towns found Springfield154+$ uv run python lands.py towns found Springfield155 156 Founding town: Springfield157 158-$ python lands.py towns burn Atlantis158+$ uv run python lands.py towns burn Atlantis159 160 Burning town: Atlantis161 ```162 31 unmodified lines194 <div class="termy">195 196 ```console197 // Check the main help198-$ python main.py --help198+$ uv run python main.py --help199 200 Usage: main.py [OPTIONS] COMMAND [ARGS]...201 202 Options:6 unmodified lines209 items210 lands211 212 // Try some users commands213-$ python main.py users create Camila213+$ uv run python main.py users create Camila214 215 Creating user: Camila216 217 // Now try some items commands218-$ python main.py items create Sword218+$ uv run python main.py items create Sword219 220 Creating item: Sword221 222 // And now some lands commands for reigns223-$ python main.py lands reigns conquer Gondor223+$ uv run python main.py lands reigns conquer Gondor224 225 Conquering reign: Gondor226 227 // And for towns228-$ python main.py lands towns found Cartagena228+$ uv run python main.py lands towns found Cartagena229 230 Founding town: Cartagena231 ```232 51 unmodified linesmodified:docs/tutorial/subcommands/single-file.md+8 added-8 removed
36 unmodified lines37 <div class="termy">38 39 ```console40 // We want this ✔️41-$ python main.py items create41+$ uv run python main.py items create42 ```43 44 </div>45 2 unmodified lines48 <div class="termy">49 50 ```console51 // We don't want this ⛔️52-$ python main.py items items-create52+$ uv run python main.py items items-create53 ```54 55 </div>56 9 unmodified lines66 <div class="termy">67 68 ```console69 // Check the help70-$ python main.py --help70+$ uv run python main.py --help71 72 Usage: main.py [OPTIONS] COMMAND [ARGS]...73 74 Options:15 unmodified lines90 <div class="termy">91 92 ```console93 // Check the help for items94-$ python main.py items --help94+$ uv run python main.py items --help95 96 // It shows its own commands (subcommands): create, delete, sell97 Usage: main.py items [OPTIONS] COMMAND [ARGS]...98 5 unmodified lines104 delete105 sell106 107 // Try it108-$ python main.py items create Wand108+$ uv run python main.py items create Wand109 110 Creating item: Wand111 112-$ python main.py items sell Vase112+$ uv run python main.py items sell Vase113 114 Selling item: Vase115 ```116 4 unmodified lines121 122 <div class="termy">123 124 ```console125-$ python main.py users --help125+$ uv run python main.py users --help126 127 Usage: main.py users [OPTIONS] COMMAND [ARGS]...128 129 Options:3 unmodified lines133 create134 delete135 136 // Try it137-$ python main.py users create Camila137+$ uv run python main.py users create Camila138 139 Creating user: Camila140 ```141 1 unmodified linemodified:docs/tutorial/app-dir.md+1 added-1 removed
9 unmodified lines10 11 <div class="termy">12 13 ```console14-$ python main.py14+$ uv run python main.py15 16 Config file doesn't exist yet17 ```18 25 unmodified linesmodified:docs/tutorial/exceptions.md+6 added-6 removed
21 unmodified lines22 23 <div class="termy">24 25 ```console26-$ python main.py26+$ uv run python main.py27 28 <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 lines49 50 <div class="termy">51 52 ```console53-$ python main.py53+$ uv run python main.py54 55 Traceback (most recent call last):56 57 File "main.py", line 12, in <module>25 unmodified lines83 84 <div class="termy">85 86 ```console87-$ python main.py87+$ uv run python main.py88 89 <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 lines120 121 <div class="termy">122 123 ```console124-$ python main.py124+$ uv run python main.py125 126 <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"><module></font> <font color="#F92672">│</font>128 <font color="#F92672">│</font> <font color="#F92672">│</font>73 unmodified lines202 203 <div class="termy">204 205 ```console206-$ python main.py206+$ uv run python main.py207 208 Traceback (most recent call last):209 File "main.py", line 12, in <module>210 app()27 unmodified lines238 <div class="termy">239 240 ```console241 export TYPER_STANDARD_TRACEBACK=1242-$ python main.py242+$ uv run python main.py243 244 245 Traceback (most recent call last):246 File "main.py", line 12, in <module>23 unmodified linesmodified:docs/tutorial/first-steps.md+22 added-22 removed
11 unmodified lines12 13 <div class="termy">14 15 ```console16-$ python main.py16+$ uv run python main.py17 18 Hello World19 20 // It just prints "Hello World".21 22 // Now check the --help23-$ python main.py --help23+$ uv run python main.py --help24 25 <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 lines66 <div class="termy">67 68 ```console69 70-$ python main.py70+$ uv run python main.py71 72 // 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">'main.py </font><font color="#44919F"><b>--help</b></font><font color="#44919F">'</font><font color="#A5A5A1"> for help.</font>1 unmodified line76 <font color="#F92672">│</font> Missing argument 'name'. <font color="#F92672">│</font>77 <font color="#F92672">╰───────────────────────────────────────────────────╯</font>78 79 // Now pass that 'name' CLI argument80-$ python main.py Camila80+$ uv run python main.py Camila81 82 Hello Camila83 84 // Here "Camila" is the CLI argument85 86 // 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"88 89 Hello Camila Gutiérrez90 ```91 16 unmodified lines108 <div class="termy">109 110 ```console111 // Check the main --help112-$ python main.py --help112+$ uv run python main.py --help113 114 <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 lines124 125 // There are now 2 CLI arguments, name and lastname126 127 // Now pass a single name argument128-$ python main.py Camila128+$ uv run python main.py Camila129 130 <font color="#F4BF75">Usage: </font>main.py [OPTIONS] {name} {lastname}131 <font color="#A5A5A1">Try </font><font color="#44919F">'main.py </font><font color="#44919F"><b>--help</b></font><font color="#44919F">'</font><font color="#A5A5A1"> for help.</font>132 <font color="#F92672">╭─ Error ───────────────────────────────────────────╮</font>133 <font color="#F92672">│</font> Missing argument 'lastname'. <font color="#F92672">│</font>134 <font color="#F92672">╰───────────────────────────────────────────────────╯</font>135 136 // These 2 arguments are required, so, pass both:137-$ python main.py Camila Gutiérrez137+$ uv run python main.py Camila Gutiérrez138 139 Hello Camila Gutiérrez140 ```141 5 unmodified lines147 148 If you called it with:149 150 ```151-$ python main.py Gutiérrez Camila151+$ uv run python main.py Gutiérrez Camila152 ```153 154 your 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 lines232 <div class="termy">233 234 ```console235 // Get the help236-$ python main.py --help236+$ uv run python main.py --help237 238 <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 lines260 261 <div class="termy">262 263 ```console264-$ python main.py Camila Gutiérrez264+$ uv run python main.py Camila Gutiérrez265 266 Hello Camila Gutiérrez267 268 // But if you pass --formal269-$ python main.py Camila Gutiérrez --formal269+$ uv run python main.py Camila Gutiérrez --formal270 271 Good day Ms. Camila Gutiérrez.272 273 // 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érrez275 276 Good day Ms. Camila Gutiérrez.277 278-$ python main.py --formal Camila Gutiérrez278+$ uv run python main.py --formal Camila Gutiérrez279 280 Good day Ms. Camila Gutiérrez.281 ```282 9 unmodified lines292 293 <div class="termy">294 295 ```console296-$ python main.py --help296+$ uv run python main.py --help297 298 <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 lines321 <div class="termy">322 323 ```console324 // Call it without a --lastname325-$ python main.py Camila325+$ uv run python main.py Camila326 327 Hello Camila328 329 // Pass the --lastname330-$ python main.py Camila --lastname Gutiérrez330+$ uv run python main.py Camila --lastname Gutiérrez331 332 Hello Camila Gutiérrez333 ```334 9 unmodified lines344 345 <div class="termy">346 347 ```console348-$ python main.py --lastname Gutiérrez Camila348+$ uv run python main.py --lastname Gutiérrez Camila349 350 // and it will still work normally351 Hello Camila Gutiérrez352 ```10 unmodified lines363 364 <div class="termy">365 366 ```console367-$ python main.py --help367+$ uv run python main.py --help368 369 <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 lines464 We will use ***CLI parameter*** to refer to both, *CLI arguments* and *CLI options*.465 466 ## The `typer` Command467 468-When you install `typer`, by default it adds a `typer` command to your shell.468+When 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.469 470-This `typer` command allows you to run your scripts with ✨ auto completion ✨ in your terminal.470+After you install completion for it, this `typer` command allows you to run your scripts with ✨ auto completion ✨ in your terminal.471 472 As an alternative to running with Python:473 474 <div class="termy">475 476 ```console477-$ python main.py477+$ uv run python main.py478 479 Hello World480 ```481 25 unmodified linesmodified:docs/tutorial/index.md+1 added-1 removed
46 unmodified lines47 48 <div class="termy">49 50 ```console51-$ python main.py51+$ uv run python main.py52 53 ✨ The magic happens here ✨54 ```55 9 unmodified linesmodified:docs/tutorial/install.md+87 added-4 removed
1 # Install **Typer**2 3-The first step is to install **Typer**.3+The first step is to set up your project and add **Typer**.4 5-First, make sure you create your [virtual environment](../virtual-environments.md), activate it, and then install it, for example with:5+Install [`uv`](https://docs.astral.sh/uv/getting-started/installation/), then create a project and add Typer:6 7 <div class="termy">8 9 ```console10-$ pip install typer10+$ uv init awesome-project --bare11+$ cd awesome-project12+$ uv add typer13 ---> 100%12-Successfully installed typer shellingham rich14 ```15 16 </div>17 18+`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.19+20+/// details | What these commands do21+22+* `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.25+26+Then `cd awesome-project` enters the new project directory before adding Typer.27+28+`uv` will use a compatible Python version already installed on your system, or download one if needed.29+30+When 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.31+32+Creating 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.33+34+///35+36 By default, `typer` comes with `rich` and `shellingham`.37+38+## Activate the Virtual Environment39+40+`uv add typer` installs both the Typer library and the `typer` command in the project's `.venv`.41+42+You 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.43+44+//// tab | Linux, macOS45+46+<div class="termy">47+48+```console49+$ source .venv/bin/activate50+```51+52+</div>53+54+////55+56+//// tab | Windows PowerShell57+58+<div class="termy">59+60+```console61+$ .venv\Scripts\Activate.ps162+```63+64+</div>65+66+////67+68+//// tab | Windows Bash69+70+<div class="termy">71+72+```console73+$ source .venv/Scripts/activate74+```75+76+</div>77+78+////79+80+Enable completion for your current shell with:81+82+<div class="termy">83+84+```console85+$ typer --install-completion86+```87+88+</div>89+90+Restart your terminal for the new completion configuration to take effect.91+92+Activate the project's virtual environment again in each new terminal session where you want to use the `typer` command directly.93+94+/// details | Using `pip` instead95+96+If you prefer to manage a virtual environment and packages manually, create and activate a virtual environment and then install Typer with `pip install typer`.97+98+Read the [Virtual Environments guide](https://tiangolo.com/guides/virtual-environments/) for the detailed steps.99+100+///modified:docs/tutorial/launch.md+2 added-2 removed
9 unmodified lines10 11 <div class="termy">12 13 ```console14-$ python main.py14+$ uv run python main.py15 16 Opening Typer docs17 18 // Opens browser with Typer's docs19 unmodified lines38 39 <div class="termy">40 41 ```console42-$ python main.py42+$ uv run python main.py43 44 Opening config directory45 46 // Opens a file browser indicating where the config file is located3 unmodified linesmodified:docs/tutorial/one-file-per-command.md+2 added-2 removed
106 unmodified lines107 108 <div class="termy">109 110 ```console111-$ python -m mycli.main version111+$ uv run python -m mycli.main version112 113 My CLI Version 1.0114 115-$ python -m mycli.main users add Camila115+$ uv run python -m mycli.main users add Camila116 117 Adding user: Camila118 ```119 25 unmodified linesmodified:docs/tutorial/package.md+6 added-37 removed
296 unmodified lines297 The `.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).298 299 ## Test your wheel package300 301-Now you can open another terminal and install that package from the file for your own user with:301+Now you can open another terminal and install that package as an isolated command-line tool with:302 303 <div class="termy">304 305 ```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.whl307 308 ---> 100%309 ```310 311 </div>312 313-/// warning314-315-The `--user` is important, that ensures you install it in your user's directory and not in the global system.316-317-If 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.318-319-///320-313 /// tip314 323-Bonus 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.316 317 ///318 319 Now you have your CLI program installed. And you can use it freely:212 unmodified lines532 533 <div class="termy">534 535 ```console544-$ pip uninstall rick-portal-gun545-546-Found existing installation: rick-portal-gun 0.1.0547-Uninstalling 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 ```538 539 </div>540 558-And now install it again, but this time using just the name, so that `pip` pulls it from PyPI:541+And now install it again, but this time using just the name, so that `uv` pulls it from PyPI:542 543 <div class="termy">544 545 ```console563-$ pip install --user rick-portal-gun564-565-// Notice that it says "Downloading" 🚀566-Collecting rick-portal-gun567- Downloading rick_portal_gun-0.1.0-py3-none-any.whl.metadata (435 bytes)568-Requirement 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)569-Requirement 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)570-Requirement 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)571-Requirement 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)572-Requirement 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)573-Requirement 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)574-Requirement 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)575-Downloading rick_portal_gun-0.1.0-py3-none-any.whl (1.8 kB)576-Installing collected packages: rick-portal-gun577-Successfully installed rick-portal-gun-0.1.0546+$ uv tool install rick-portal-gun547 ```548 549 </div>550 89 unmodified linesmodified:docs/tutorial/printing.md+5 added-5 removed
7 unmodified lines8 9 <div class="termy">10 11 ```console12-$ python main.py World12+$ uv run python main.py World13 14 Hello World15 ```16 13 unmodified lines30 31 <div class="termy">32 33 ```console34-$ python main.py34+$ uv run python main.py35 36 Here's the data37 <b>{</b>38 <font color="#A6E22E">'name'</font>: <font color="#A6E22E">'Rick'</font>,17 unmodified lines56 57 <div class="termy">58 59 ```console60-$ python main.py60+$ uv run python main.py61 62 <font color="#F92672"><b>Alert!</b></font> <font color="#A6E22E">Portal gun</font> shooting! 💥63 ```64 19 unmodified lines84 85 <div class="termy">86 87 ```console88-$ python main.py88+$ uv run python main.py89 90 ┏━━━━━━━┳━━━━━━━━━━━━┓91 ┃<b> Name </b>┃<b> Item </b>┃92 ┡━━━━━━━╇━━━━━━━━━━━━┩65 unmodified lines158 159 <div class="termy">160 161 ```console162-$ python main.py162+$ uv run python main.py163 164 Here is something written to standard error165 ```166 106 unmodified linesmodified:docs/tutorial/progressbar.md+5 added-5 removed
13 unmodified lines14 15 <div class="termy">16 17 ```console18-$ python main.py18+$ uv run python main.py19 20 ---> 100%21 22 Processed 100 things.7 unmodified lines30 31 <div class="termy">32 33 ```console34-$ python main.py34+$ uv run python main.py35 36 Processing... <font color="#F92672">━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸</font><font color="#3A3A3A">━━━━━━━━━━</font> <font color="#AE81FF"> 74%</font> <font color="#A1EFE4">0:00:01</font>37 ```38 15 unmodified lines54 55 <div class="termy">56 57 ```console58-$ python main.py58+$ uv run python main.py59 60 <font color="#A6E22E">⠹</font> Processing...61 <font color="#A6E22E">⠹</font> Preparing...62 ```72 unmodified lines135 136 <div class="termy">137 138 ```console139-$ python main.py139+$ uv run python main.py140 141 ---> 100%142 143 Processed 100 things.19 unmodified lines163 164 <div class="termy">165 166 ```console167-$ python main.py167+$ uv run python main.py168 169 ---> 100%170 171 Processed 100 user IDs.61 unmodified linesmodified:docs/tutorial/prompt.md+6 added-6 removed
9 unmodified lines10 11 <div class="termy">12 13 ```console14-$ python main.py14+$ uv run python main.py15 16 # What's your name?:$ Camila17 18 Hello Camila11 unmodified lines30 31 <div class="termy">32 33 ```console34-$ python main.py34+$ uv run python main.py35 36 # Are you sure you want to delete it? [y/N]:$ y37 38 Deleting it!39 40 // This time cancel it41-$ python main.py41+$ uv run python main.py42 43 # Are you sure you want to delete it? [y/N]:$ n44 45 Not deleting10 unmodified lines56 57 <div class="termy">58 59 ```console60-$ python main.py60+$ uv run python main.py61 62 # Are you sure you want to delete it? [y/N]:$ y63 64 Deleting it!65 66 // This time cancel it67-$ python main.py67+$ uv run python main.py68 69 # Are you sure you want to delete it? [y/N]:$ n70 71 Aborted!11 unmodified lines83 84 <div class="termy">85 86 ```console87-$ python main.py87+$ uv run python main.py88 89 # Enter your name 😎:$ Morty90 91 Hello Morty3 unmodified linesmodified:docs/tutorial/terminating.md+6 added-6 removed
23 unmodified lines24 25 <div class="termy">26 27 ```console28-$ python main.py Camila28+$ uv run python main.py Camila29 30 User created: Camila31 Notification sent for new user: Camila32 33 // Try with an existing user34-$ python main.py rick34+$ uv run python main.py rick35 36 The user already exists37 38 // Notice that the notification code was never run, the second message is not printed23 unmodified lines62 63 <div class="termy">64 65 ```console66-$ python main.py Camila66+$ uv run python main.py Camila67 68 New user created: Camila69 70 // Print the result code of the last program executed1 unmodified line72 73 074 75 // Now make it exit with an error76-$ python main.py root76+$ uv run python main.py root77 78 The root user is reserved79 80 // Print the result code of the last program executed24 unmodified lines105 106 <div class="termy">107 108 ```console109-$ python main.py Camila109+$ uv run python main.py Camila110 111 New user created: Camila112 113 // Now make it exit with an error114-$ python main.py root114+$ uv run python main.py root115 116 The root user is reserved117 Aborted!118 ```2 unmodified linesmodified:docs/tutorial/testing.md+3 added-3 removed
9 unmodified lines10 11 <div class="termy">12 13 ```console14-$ python main.py Camila --city Berlin14+$ uv run python main.py Camila --city Berlin15 16 Hello Camila17 Let's have a coffee in Berlin18 ```68 unmodified lines87 88 <div class="termy">89 90 ```console91-$ pytest91+$ uv run pytest92 93 ================ test session starts ================94 platform linux -- Python 3.10, pytest-5.3.5, py-1.8.1, pluggy-0.13.195 rootdir: /home/user/code/superawesome-cli/app19 unmodified lines115 116 <div class="termy">117 118 ```console119-$ python main.py Camila119+$ uv run python main.py Camila120 121 # Email: $ camila@example.com122 123 Hello Camila, your email is: camila@example.com91 unmodified linesmodified:docs/tutorial/typer-app.md+4 added-4 removed
47 unmodified lines48 <div class="termy">49 50 ```console51 // Without a CLI argument52-$ python main.py52+$ uv run python main.py53 54 Usage: main.py [OPTIONS] {name}55 Try "main.py --help" for help.56 57 Error: Missing argument 'name'.58 59 // With the 'name' CLI argument60-$ python main.py Camila60+$ uv run python main.py Camila61 62 Hello Camila63 64 // Asking for help65-$ python main.py --help65+$ uv run python main.py --help66 67 Usage: main.py [OPTIONS] {name}68 69 Arguments:22 unmodified lines92 93 <div class="termy">94 95 ```console96-$ python main.py96+$ uv run python main.py97 98 ✨ Some magic here ✨99 ```100 21 unmodified linesmodified:docs/tutorial/typer-command.md+40 added-13 removed
1 # `typer` command2 3 The `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.4 5-It'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`).5+It'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.6 7 In that case, you can run your program with the `typer` command in your Terminal, and it will provide completion for your script.8 9 The `typer` command also has functionality to generate Markdown documentation for your own **Typer** programs 📝.10 11 ## Install12 13-When you install **Typer** with:13+Add Typer to your project:14 15-```bash16-pip install typer15+```console16+$ uv add typer17 ```18 19-...it includes the `typer` command.19+This installs both the Typer library and the `typer` command in the project's `.venv`.20 21+Activate that environment to make `typer` available as a first-class command in your terminal:22+23+//// tab | Linux, macOS24+25+```console26+$ source .venv/bin/activate27+```28+29+////30+31+//// tab | Windows PowerShell32+33+```console34+$ .venv\Scripts\Activate.ps135+```36+37+////38+39+//// tab | Windows Bash40+41+```console42+$ source .venv/Scripts/activate43+```44+45+////46+47+Using the `typer` command from the project environment ensures it uses the same Typer version and dependencies as the project.48+49 If you don't want to use the `typer` command, you can call the Typer library as a module with:50 23-```bash24-python -m typer51+```console52+$ uv run python -m typer53 ```54 55 ## Install completion56 46 unmodified lines103 104 <div class="termy">105 106 ```console79-$ python -m pip install typer107+$ uv add typer108 ---> 100%81-Successfully installed typer109 ```110 111 </div>112 3 unmodified lines116 117 <div class="termy">118 119 ```console93-$ python my_custom_script.py hello120+$ uv run python my_custom_script.py hello121 122 Hello World!123 97-$ python my_custom_script.py hello --name Camila124+$ uv run python my_custom_script.py hello --name Camila125 126 Hello Camila!127 101-$ python my_custom_script.py bye --name Camila128+$ uv run python my_custom_script.py bye --name Camila129 130 Bye Camila131 ```132 151 unmodified lines284 285 If you don't want to use the `typer` command, you can still generate docs with:286 287 ```console261-$ python -m typer some_script.py utils docs288+$ uv run python -m typer some_script.py utils docs289 ```290 291 ///292 108 unmodified linesmodified:docs/environment-variables.md+4 added-293 removed
1 # Environment Variables2 3-Before 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**.3+An **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.4 5-/// tip5+Typer 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).6 7-If you already know what "environment variables" are and how to use them, feel free to skip this.7+## Learn More { #learn-more }8 9-///10-11-An 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).12-13-Environment variables could be useful for handling application **settings**, as part of the **installation** of Python, etc.14-15-## Create and Use Env Vars16-17-You can **create** and use environment variables in the **shell (terminal)**, without needing Python:18-19-//// tab | Linux, macOS, Windows Bash20-21-<div class="termy">22-23-```console24-// You could create an env var MY_NAME with25-$ export MY_NAME="Wade Wilson"26-27-// Then you could use it with other programs, like28-$ echo "Hello $MY_NAME"29-30-Hello Wade Wilson31-```32-33-</div>34-35-////36-37-//// tab | Windows PowerShell38-39-<div class="termy">40-41-```console42-// Create an env var MY_NAME43-$ $Env:MY_NAME = "Wade Wilson"44-45-// Use it with other programs, like46-$ echo "Hello $Env:MY_NAME"47-48-Hello Wade Wilson49-```50-51-</div>52-53-////54-55-## Read env vars in Python56-57-You could also create environment variables **outside** of Python, in the terminal (or with any other method), and then **read them in Python**.58-59-For example you could have a file `main.py` with:60-61-```Python hl_lines="3"62-import os63-64-name = os.getenv("MY_NAME", "World")65-print(f"Hello {name} from Python")66-```67-68-/// tip69-70-The second argument to [`os.getenv()`](https://docs.python.org/3.8/library/os.html#os.getenv) is the default value to return.71-72-If not provided, it's `None` by default, here we provide `"World"` as the default value to use.73-74-///75-76-Then you could call that Python program:77-78-//// tab | Linux, macOS, Windows Bash79-80-<div class="termy">81-82-```console83-// Here we don't set the env var yet84-$ python main.py85-86-// As we didn't set the env var, we get the default value87-88-Hello World from Python89-90-// But if we create an environment variable first91-$ export MY_NAME="Wade Wilson"92-93-// And then call the program again94-$ python main.py95-96-// Now it can read the environment variable97-98-Hello Wade Wilson from Python99-```100-101-</div>102-103-////104-105-//// tab | Windows PowerShell106-107-<div class="termy">108-109-```console110-// Here we don't set the env var yet111-$ python main.py112-113-// As we didn't set the env var, we get the default value114-115-Hello World from Python116-117-// But if we create an environment variable first118-$ $Env:MY_NAME = "Wade Wilson"119-120-// And then call the program again121-$ python main.py122-123-// Now it can read the environment variable124-125-Hello Wade Wilson from Python126-```127-128-</div>129-130-////131-132-As 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**.133-134-You can also create an environment variable only for a **specific program invocation**, that is only available to that program, and only for its duration.135-136-To do that, create it right before the program itself, on the same line:137-138-<div class="termy">139-140-```console141-// Create an env var MY_NAME in line for this program call142-$ MY_NAME="Wade Wilson" python main.py143-144-// Now it can read the environment variable145-146-Hello Wade Wilson from Python147-148-// The env var no longer exists afterwards149-$ python main.py150-151-Hello World from Python152-```153-154-</div>155-156-/// tip157-158-You can read more about it at [The Twelve-Factor App: Config](https://12factor.net/config).159-160-///161-162-## Types and Validation163-164-These 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).165-166-That 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.167-168-You 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).169-170-## `PATH` Environment Variable171-172-There is a **special** environment variable called **`PATH`** that is used by the operating systems (Linux, macOS, Windows) to find programs to run.173-174-The 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.175-176-For example, the `PATH` environment variable could look like this:177-178-//// tab | Linux, macOS179-180-```plaintext181-/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin182-```183-184-This means that the system should look for programs in the directories:185-186-* `/usr/local/bin`187-* `/usr/bin`188-* `/bin`189-* `/usr/sbin`190-* `/sbin`191-192-////193-194-//// tab | Windows195-196-```plaintext197-C:\Program Files\Python312\Scripts;C:\Program Files\Python312;C:\Windows\System32198-```199-200-This means that the system should look for programs in the directories:201-202-* `C:\Program Files\Python312\Scripts`203-* `C:\Program Files\Python312`204-* `C:\Windows\System32`205-206-////207-208-When 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.209-210-For example, when you type `python` in the terminal, the operating system looks for a program called `python` in the **first directory** in that list.211-212-If it finds it, then it will **use it**. Otherwise it keeps looking in the **other directories**.213-214-### Installing Python and Updating the `PATH`215-216-When you install Python, you might be asked if you want to update the `PATH` environment variable.217-218-//// tab | Linux, macOS219-220-Let's say you install Python and it ends up in a directory `/opt/custompython/bin`.221-222-If you say yes to update the `PATH` environment variable, then the installer will add `/opt/custompython/bin` to the `PATH` environment variable.223-224-It could look like this:225-226-```plaintext227-/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/custompython/bin228-```229-230-////231-232-//// tab | Windows233-234-Let's say you install Python and it ends up in a directory `C:\opt\custompython\bin`.235-236-If you say yes to update the `PATH` environment variable, then the installer will add `C:\opt\custompython\bin` to the `PATH` environment variable.237-238-```plaintext239-C:\Program Files\Python312\Scripts;C:\Program Files\Python312;C:\Windows\System32;C:\opt\custompython\bin240-```241-242-////243-244-So, if you type:245-246-<div class="termy">247-248-```console249-$ python250-```251-252-</div>253-254-//// tab | Linux, macOS255-256-The system will **find** the `python` program in `/opt/custompython/bin` (the last directory) and run it.257-258-It would be roughly equivalent to typing:259-260-<div class="termy">261-262-```console263-$ /opt/custompython/bin/python264-```265-266-</div>267-268-////269-270-//// tab | Windows271-272-The system will **find** the `python` program in `C:\opt\custompython\bin\python` (the last directory) and run it.273-274-It would be roughly equivalent to typing:275-276-<div class="termy">277-278-```console279-$ C:\opt\custompython\bin\python280-```281-282-</div>283-284-////285-286-This information will be useful when learning about [Virtual Environments](virtual-environments.md).287-288-It 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.289-290-## Conclusion291-292-With this you should have a basic understanding of what **environment variables** are and how to use them in Python.293-294-You can also read more about them in the [Wikipedia for Environment Variable](https://en.wikipedia.org/wiki/Environment_variable).295-296-In 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.297-298-For example, you will need this information in the next section, about [Virtual Environments](virtual-environments.md).9+Read 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 lines50 **Typer** is [FastAPI](https://fastapi.tiangolo.com)'s little sibling, it's the FastAPI of CLIs.51 52 ## Installation53 54-Create and activate a [virtual environment](https://typer.tiangolo.com/virtual-environments/) and then install **Typer**:54+First, [install `uv`](https://docs.astral.sh/uv/getting-started/installation/), and then add **Typer** to your project:55 56 <div class="termy">57 58 ```console59-$ pip install typer59+$ uv add typer60 ---> 100%61-Successfully installed typer rich shellingham61 ```62 63 </div>64 65+This 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).66+67+If you prefer to use `pip`, install `typer` inside a virtual environment. See the [installation guide](tutorial/install.md) for the alternative steps.68+69 ## Example70 71 ### The absolute minimum72 73 unmodified lines146 <div class="termy">147 148 ```console149 // Run your application147-$ python main.py150+$ uv run python main.py151 152 // You get a nice error, you are missing 'name'153 Usage: main.py [OPTIONS] {name}154 Try 'main.py --help' for help.2 unmodified lines157 ╰───────────────────────────────────────────────────╯158 159 160 // You get a --help for free158-$ python main.py --help161+$ uv run python main.py --help162 163 Usage: main.py [OPTIONS] {name}164 165 ╭─ Arguments ───────────────────────────────────────╮3 unmodified lines169 │ --help Show this message and exit. │170 ╰───────────────────────────────────────────────────╯171 172 // Now pass the 'name' argument170-$ python main.py Camila173+$ uv run python main.py Camila174 175 Hello Camila176 177 // It works! 🎉51 unmodified lines229 230 <div class="termy">231 232 ```console230-$ python main.py --help233+$ uv run python main.py --help234 235 Usage: main.py [OPTIONS] COMMAND [ARGS]...236 237 ╭─ Options ─────────────────────────────────────────╮24 unmodified lines262 263 <div class="termy">264 265 ```console263-$ python main.py hello --help266+$ uv run python main.py hello --help267 268 Usage: main.py hello [OPTIONS] {name}269 270 ╭─ Arguments ───────────────────────────────────────╮10 unmodified lines281 282 <div class="termy">283 284 ```console282-$ python main.py goodbye --help285+$ uv run python main.py goodbye --help286 287 Usage: main.py goodbye [OPTIONS] {name}288 289 ╭─ Arguments ───────────────────────────────────────╮16 unmodified lines306 307 ```console308 // Use it with the hello command309 307-$ python main.py hello Camila310+$ uv run python main.py hello Camila311 312 Hello Camila313 314 // And with the goodbye command315 313-$ python main.py goodbye Camila316+$ uv run python main.py goodbye Camila317 318 Bye Camila!319 320 // And with --formal321 319-$ python main.py goodbye --formal Camila322+$ uv run python main.py goodbye --formal Camila323 324 Goodbye Ms. Camila. Have a good day.325 ```326 62 unmodified linesmodified:docs/virtual-environments.md+11 added-820 removed
1 # Virtual Environments2 3-When 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.3+When you work with Python projects, you should use a **virtual environment** to isolate the packages installed for each project.4 5-/// note5+For Typer projects, I recommend using [uv](https://docs.astral.sh/uv/) to manage the project, its dependencies, and its virtual environment.6 7-If 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 }8 9-///9+Install `uv` using the [official installation guide](https://docs.astral.sh/uv/getting-started/installation/), and then create a project:10 11-/// tip12-13-A **virtual environment** is different than an **environment variable**.14-15-An **environment variable** is a variable in the system that can be used by programs.16-17-A **virtual environment** is a directory with some files in it.18-19-///20-21-/// note22-23-This page will teach you how to use **virtual environments** and how they work.24-25-If you are ready to adopt a **tool that manages everything** for you (including installing Python), try [uv](https://github.com/astral-sh/uv).26-27-///28-29-## Create a Project30-31-First, create a directory for your project.32-33-What I normally do is that I create a directory named `code` inside my home/user directory.34-35-And inside of that I create one directory per project.36-11 <div class="termy">12 13 ```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 ```18 19 </div>20 54-## Create a Virtual Environment21+`uv` creates a virtual environment for the project automatically. You don't need to create or activate one yourself.22 56-When 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>**.23+Run commands inside the project environment with `uv run`, for example:24 58-/// tip59-60-You only need to do this **once per project**, not every time you work.61-62-///63-64-//// tab | `venv`65-66-To create a virtual environment, you can use the `venv` module that comes with Python.67-25 <div class="termy">26 27 ```console71-$ python -m venv .venv28+$ uv run python main.py29 ```30 31 </div>32 76-/// details | What that command means33+## Learn More { #learn-more }34 78-* `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`82-83-///84-85-////86-87-//// tab | `uv`88-89-If you have [`uv`](https://github.com/astral-sh/uv) installed, you can use it to create a virtual environment.90-91-<div class="termy">92-93-```console94-$ uv venv95-```96-97-</div>98-99-/// tip100-101-By default, `uv` will create a virtual environment in a directory called `.venv`.102-103-But you could customize it passing an additional argument with the directory name.104-105-///106-107-////108-109-That command creates a new virtual environment in a directory called `.venv`.110-111-/// details | `.venv` or other name112-113-You could create the virtual environment in a different directory, but there's a convention of calling it `.venv`.114-115-///116-117-## Activate the Virtual Environment118-119-Activate the new virtual environment so that any Python command you run or package you install uses it.120-121-/// tip122-123-Do this **every time** you start a **new terminal session** to work on the project.124-125-///126-127-//// tab | Linux, macOS128-129-<div class="termy">130-131-```console132-$ source .venv/bin/activate133-```134-135-</div>136-137-////138-139-//// tab | Windows PowerShell140-141-<div class="termy">142-143-```console144-$ .venv\Scripts\Activate.ps1145-```146-147-</div>148-149-////150-151-//// tab | Windows Bash152-153-Or if you use Bash for Windows (e.g. [Git Bash](https://gitforwindows.org/)):154-155-<div class="termy">156-157-```console158-$ source .venv/Scripts/activate159-```160-161-</div>162-163-////164-165-/// tip166-167-Every time you install a **new package** in that environment, **activate** the environment again.168-169-This 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.170-171-///172-173-## Check the Virtual Environment is Active174-175-Check that the virtual environment is active (the previous command worked).176-177-/// tip178-179-This 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.180-181-///182-183-//// tab | Linux, macOS, Windows Bash184-185-<div class="termy">186-187-```console188-$ which python189-190-/home/user/code/awesome-project/.venv/bin/python191-```192-193-</div>194-195-If it shows the `python` binary at `.venv/bin/python`, inside of your project (in this case `awesome-project`), then it worked. 🎉196-197-////198-199-//// tab | Windows PowerShell200-201-<div class="termy">202-203-```console204-$ Get-Command python205-206-C:\Users\user\code\awesome-project\.venv\Scripts\python207-```208-209-</div>210-211-If it shows the `python` binary at `.venv\Scripts\python`, inside of your project (in this case `awesome-project`), then it worked. 🎉212-213-////214-215-## Upgrade `pip`216-217-/// tip218-219-If 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`. 😎220-221-///222-223-If you are using `pip` to install packages (it comes by default with Python), you should **upgrade** it to the latest version.224-225-Many exotic errors while installing a package are solved by just upgrading `pip` first.226-227-/// tip228-229-You would normally do this **once**, right after you create the virtual environment.230-231-///232-233-Make sure the virtual environment is active (with the command above) and then run:234-235-<div class="termy">236-237-```console238-$ python -m pip install --upgrade pip239-240----> 100%241-```242-243-</div>244-245-## Add `.gitignore`246-247-If you are using **Git** (you should), add a `.gitignore` file to exclude everything in your `.venv` from Git.248-249-/// tip250-251-If 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. 😎252-253-///254-255-/// tip256-257-Do this **once**, right after you create the virtual environment.258-259-///260-261-<div class="termy">262-263-```console264-$ echo "*" > .venv/.gitignore265-```266-267-</div>268-269-/// details | What that command means270-271-* `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 written274-275-And `*` for Git means "everything". So, it will ignore everything in the `.venv` directory.276-277-That command will create a file `.gitignore` with the content:278-279-```gitignore280-*281-```282-283-///284-285-## Install Packages286-287-After activating the environment, you can install packages in it.288-289-/// tip290-291-Do this **once** when installing or upgrading the packages your project needs.292-293-If you need to upgrade a version or add a new package you would **do this again**.294-295-///296-297-### Install Packages Directly298-299-If 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.300-301-/// tip302-303-It's a (very) good idea to put the packages and versions your program needs in a file (for example `requirements.txt` or `pyproject.toml`).304-305-///306-307-//// tab | `pip`308-309-<div class="termy">310-311-```console312-$ pip install typer313-314----> 100%315-```316-317-</div>318-319-////320-321-//// tab | `uv`322-323-If you have [`uv`](https://github.com/astral-sh/uv):324-325-<div class="termy">326-327-```console328-$ uv pip install typer329----> 100%330-```331-332-</div>333-334-////335-336-### Install from `requirements.txt`337-338-If you have a `requirements.txt`, you can now use it to install its packages.339-340-//// tab | `pip`341-342-<div class="termy">343-344-```console345-$ pip install -r requirements.txt346----> 100%347-```348-349-</div>350-351-////352-353-//// tab | `uv`354-355-If you have [`uv`](https://github.com/astral-sh/uv):356-357-<div class="termy">358-359-```console360-$ uv pip install -r requirements.txt361----> 100%362-```363-364-</div>365-366-////367-368-/// details | `requirements.txt`369-370-A `requirements.txt` with some packages could look like:371-372-```requirements.txt373-typer==0.13.0374-rich==13.7.1375-```376-377-///378-379-## Run Your Program380-381-After 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.382-383-<div class="termy">384-385-```console386-$ python main.py387-388-Hello World389-```390-391-</div>392-393-## Configure Your Editor394-395-You 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.396-397-For example:398-399-* [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)401-402-/// tip403-404-You normally have to do this only **once**, when you create the virtual environment.405-406-///407-408-## Deactivate the Virtual Environment409-410-Once you are done working on your project you can **deactivate** the virtual environment.411-412-<div class="termy">413-414-```console415-$ deactivate416-```417-418-</div>419-420-This way, when you run `python` it won't try to run it from that virtual environment with the packages installed there.421-422-## Ready to Work423-424-Now you're ready to start working on your project.425-426-427-428-/// tip429-430-Do you want to understand what's all that above?431-432-Continue reading. 👇🤓433-434-///435-436-## Why Virtual Environments437-438-To work with Typer you need to install [Python](https://www.python.org/).439-440-After that, you would need to **install** Typer and any other **packages** you want to use.441-442-To install packages you would normally use the `pip` command that comes with Python (or similar alternatives).443-444-Nevertheless, if you just use `pip` directly, the packages would be installed in your **global Python environment** (the global installation of Python).445-446-### The Problem447-448-So, what's the problem with installing packages in the global Python environment?449-450-At 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. 😱451-452-For 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`.453-454-```mermaid455-flowchart LR456- stone(philosophers-stone) -->|requires| harry-1[harry v1]457-```458-459-Then, 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`**.460-461-```mermaid462-flowchart LR463- azkaban(prisoner-of-azkaban) --> |requires| harry-3[harry v3]464-```465-466-But 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.467-468-If you want to run `philosophers-stone` you will need to first install `harry` version `1`, for example with:469-470-<div class="termy">471-472-```console473-$ pip install "harry==1"474-```475-476-</div>477-478-And then you would end up with `harry` version `1` installed in your global Python environment.479-480-```mermaid481-flowchart 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-```489-490-But 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`).491-492-<div class="termy">493-494-```console495-$ pip install "harry==3"496-```497-498-</div>499-500-And then you would end up with `harry` version `3` installed in your global Python environment.501-502-And if you try to run `philosophers-stone` again, there's a chance it would **not work** because it needs `harry` version `1`.503-504-```mermaid505-flowchart 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-```518-519-/// tip520-521-It'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.522-523-///524-525-Now, 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.526-527-Also, 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.528-529-## Where are Packages Installed530-531-When you install Python, it creates some directories with some files in your computer.532-533-Some of these directories are the ones in charge of having all the packages you install.534-535-When you run:536-537-<div class="termy">538-539-```console540-// Don't run this now, it's just an example 🤓541-$ pip install typer542----> 100%543-```544-545-</div>546-547-That will download a compressed file with the Typer code, normally from [PyPI](https://pypi.org/project/typer/).548-549-It will also **download** files for other packages that Typer depends on.550-551-Then it will **extract** all those files and put them in a directory in your computer.552-553-By default, it will put those files downloaded and extracted in the directory that comes with your Python installation, that's the **global environment**.554-555-## What are Virtual Environments556-557-The 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.558-559-A virtual environment is a **directory**, very similar to the global one, where you can install the packages for a project.560-561-This way, each project will have its own virtual environment (`.venv` directory) with its own packages.562-563-```mermaid564-flowchart 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-```579-580-## What Does Activating a Virtual Environment Mean581-582-When you activate a virtual environment, for example with:583-584-//// tab | Linux, macOS585-586-<div class="termy">587-588-```console589-$ source .venv/bin/activate590-```591-592-</div>593-594-////595-596-//// tab | Windows PowerShell597-598-<div class="termy">599-600-```console601-$ .venv\Scripts\Activate.ps1602-```603-604-</div>605-606-////607-608-//// tab | Windows Bash609-610-Or if you use Bash for Windows (e.g. [Git Bash](https://gitforwindows.org/)):611-612-<div class="termy">613-614-```console615-$ source .venv/Scripts/activate616-```617-618-</div>619-620-////621-622-That command will create or modify some [environment variables](environment-variables.md) that will be available for the next commands.623-624-One of those variables is the `PATH` variable.625-626-/// tip627-628-You can learn more about the `PATH` environment variable in the [Environment Variables](environment-variables.md#path-environment-variable) section.629-630-///631-632-Activating a virtual environment adds its path `.venv/bin` (on Linux and macOS) or `.venv\Scripts` (on Windows) to the `PATH` environment variable.633-634-Let's say that before activating the environment, the `PATH` variable looked like this:635-636-//// tab | Linux, macOS637-638-```plaintext639-/usr/bin:/bin:/usr/sbin:/sbin640-```641-642-That means that the system would look for programs in:643-644-* `/usr/bin`645-* `/bin`646-* `/usr/sbin`647-* `/sbin`648-649-////650-651-//// tab | Windows652-653-```plaintext654-C:\Windows\System32655-```656-657-That means that the system would look for programs in:658-659-* `C:\Windows\System32`660-661-////662-663-After activating the virtual environment, the `PATH` variable would look something like this:664-665-//// tab | Linux, macOS666-667-```plaintext668-/home/user/code/awesome-project/.venv/bin:/usr/bin:/bin:/usr/sbin:/sbin669-```670-671-That means that the system will now start looking first look for programs in:672-673-```plaintext674-/home/user/code/awesome-project/.venv/bin675-```676-677-before looking in the other directories.678-679-So, when you type `python` in the terminal, the system will find the Python program in680-681-```plaintext682-/home/user/code/awesome-project/.venv/bin/python683-```684-685-and use that one.686-687-////688-689-//// tab | Windows690-691-```plaintext692-C:\Users\user\code\awesome-project\.venv\Scripts;C:\Windows\System32693-```694-695-That means that the system will now start looking first look for programs in:696-697-```plaintext698-C:\Users\user\code\awesome-project\.venv\Scripts699-```700-701-before looking in the other directories.702-703-So, when you type `python` in the terminal, the system will find the Python program in704-705-```plaintext706-C:\Users\user\code\awesome-project\.venv\Scripts\python707-```708-709-and use that one.710-711-////712-713-An 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).714-715-Activating a virtual environment also changes a couple of other things, but this is one of the most important things it does.716-717-## Checking a Virtual Environment718-719-When you check if a virtual environment is active, for example with:720-721-//// tab | Linux, macOS, Windows Bash722-723-<div class="termy">724-725-```console726-$ which python727-728-/home/user/code/awesome-project/.venv/bin/python729-```730-731-</div>732-733-////734-735-//// tab | Windows PowerShell736-737-<div class="termy">738-739-```console740-$ Get-Command python741-742-C:\Users\user\code\awesome-project\.venv\Scripts\python743-```744-745-</div>746-747-////748-749-That means that the `python` program that will be used is the one **in the virtual environment**.750-751-you use `which` in Linux and macOS and `Get-Command` in Windows PowerShell.752-753-The 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.754-755-The most important part is that when you call `python`, that is the exact "`python`" that will be executed.756-757-So, you can confirm if you are in the correct virtual environment.758-759-/// tip760-761-It's easy to activate one virtual environment, get one Python, and then **go to another project**.762-763-And the second project **wouldn't work** because you are using the **incorrect Python**, from a virtual environment for another project.764-765-It's useful being able to check what `python` is being used. 🤓766-767-///768-769-## Why Deactivate a Virtual Environment770-771-For example, you could be working on a project `philosophers-stone`, **activate that virtual environment**, install packages and work with that environment.772-773-And then you want to work on **another project** `prisoner-of-azkaban`.774-775-You go to that project:776-777-<div class="termy">778-779-```console780-$ cd ~/code/prisoner-of-azkaban781-```782-783-</div>784-785-If 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`.786-787-<div class="termy">788-789-```console790-$ cd ~/code/prisoner-of-azkaban791-792-$ python main.py793-794-// Error importing sirius, it's not installed 😱795-Traceback (most recent call last):796- File "main.py", line 1, in <module>797- import sirius798-```799-800-</div>801-802-But 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`.803-804-<div class="termy">805-806-```console807-$ cd ~/code/prisoner-of-azkaban808-809-// 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-$ deactivate811-812-// Activate the virtual environment in prisoner-of-azkaban/.venv 🚀813-$ source .venv/bin/activate814-815-// Now when you run python, it will find the package sirius installed in this virtual environment ✨816-$ python main.py817-818-I solemnly swear 🐺819-```820-821-</div>822-823-## Alternatives824-825-This is a simple guide to get you started and teach you how everything works **underneath**.826-827-There are many **alternatives** to managing virtual environments, package dependencies (requirements), projects.828-829-Once 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).830-831-`uv` can do a lot of things, it can:832-833-* **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 things839-840-## Conclusion841-842-If you read and understood all this, now **you know much more** about virtual environments than many developers out there. 🤓843-844-Knowing 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**. 😎35+Read 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 lines81 - 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.md142 unmodified linesmodified:README.md+15 added-12 removed
41 unmodified lines42 **Typer** is [FastAPI](https://fastapi.tiangolo.com)'s little sibling, it's the FastAPI of CLIs.43 44 ## Installation45 46-Create and activate a [virtual environment](https://typer.tiangolo.com/virtual-environments/) and then install **Typer**:46+First, [install `uv`](https://docs.astral.sh/uv/getting-started/installation/), and then add **Typer** to your project:47 48 <div class="termy">49 50 ```console51-$ pip install typer51+$ uv add typer52 ---> 100%53-Successfully installed typer rich shellingham53 ```54 55 </div>56 57+This 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).58+59+If you prefer to use `pip`, install `typer` inside a virtual environment. See the [installation guide](tutorial/install.md) for the alternative steps.60+61 ## Example62 63 ### The absolute minimum64 73 unmodified lines138 <div class="termy">139 140 ```console141 // Run your application139-$ python main.py142+$ uv run python main.py143 144 // You get a nice error, you are missing 'name'145 Usage: main.py [OPTIONS] {name}146 Try 'main.py --help' for help.2 unmodified lines149 ╰───────────────────────────────────────────────────╯150 151 152 // You get a --help for free150-$ python main.py --help153+$ uv run python main.py --help154 155 Usage: main.py [OPTIONS] {name}156 157 ╭─ Arguments ───────────────────────────────────────╮3 unmodified lines161 │ --help Show this message and exit. │162 ╰───────────────────────────────────────────────────╯163 164 // Now pass the 'name' argument162-$ python main.py Camila165+$ uv run python main.py Camila166 167 Hello Camila168 169 // It works! 🎉51 unmodified lines221 222 <div class="termy">223 224 ```console222-$ python main.py --help225+$ uv run python main.py --help226 227 Usage: main.py [OPTIONS] COMMAND [ARGS]...228 229 ╭─ Options ─────────────────────────────────────────╮24 unmodified lines254 255 <div class="termy">256 257 ```console255-$ python main.py hello --help258+$ uv run python main.py hello --help259 260 Usage: main.py hello [OPTIONS] {name}261 262 ╭─ Arguments ───────────────────────────────────────╮10 unmodified lines273 274 <div class="termy">275 276 ```console274-$ python main.py goodbye --help277+$ uv run python main.py goodbye --help278 279 Usage: main.py goodbye [OPTIONS] {name}280 281 ╭─ Arguments ───────────────────────────────────────╮16 unmodified lines298 299 ```console300 // Use it with the hello command301 299-$ python main.py hello Camila302+$ uv run python main.py hello Camila303 304 Hello Camila305 306 // And with the goodbye command307 305-$ python main.py goodbye Camila308+$ uv run python main.py goodbye Camila309 310 Bye Camila!311 312 // And with --formal313 311-$ python main.py goodbye --formal Camila314+$ uv run python main.py goodbye --formal Camila315 316 Goodbye Ms. Camila. Have a good day.317 ```318 62 unmodified lines