The Best way to build a Python command line tool - Python Typer Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ส.ค. 2024
  • In this video, I go over what I think is one of the best modules for creating Python Command Line (CLI) Tools.
    I go over What Python Typer is, how to install and import it and how to use variables and command-line switches.
    Make Professional look Python CLI Applications today.
    If you want to know how to package a python application:
    packaging.python.org/tutorial...
    ☕ Buy me a coffee: www.buymeacoffee.com/bradmorg
    👨‍💻 Join our Discord Community of DevOps Engineers: / discord
    Code: devopslifecycle.com/lessons/1...
    📘 Chapters:
    0:00 What is Python Typer
    0:26 Typer Code Example, building commands using functions
    3:36 Adding variables to our Typer CLI application
    5:42 Adding in CLI Switches
    7:22 Adding in autocompletion to our Python CLI App
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 44

  • @DevOpsJourney
    @DevOpsJourney  3 ปีที่แล้ว +16

    What's your favorite Python module for creating CLI applications?

    • @bendover4728
      @bendover4728 3 ปีที่แล้ว +2

      Click!

    • @jwright123456789
      @jwright123456789 3 ปีที่แล้ว +1

      I have been using docopt, because I like having good docstrings, but it is limited and doesn't always behave like I think it should. Click and typer look good!

  • @dramastar30
    @dramastar30 3 ปีที่แล้ว +5

    Cool 😎. Typer is simply smart. Thanks for the demo🙏🏻

  • @FrancoisSchnell
    @FrancoisSchnell 3 ปีที่แล้ว +4

    Neat! Didn't know about typer, thanks!

  • @cralx2k
    @cralx2k 2 ปีที่แล้ว +1

    Amazing channel... Thank you

  • @rickysmith1263
    @rickysmith1263 2 ปีที่แล้ว

    Cool, thanks 😃

  • @zaurmahmudov1438
    @zaurmahmudov1438 3 ปีที่แล้ว +1

    thanks, helped me a lot today

  • @dragon3602010
    @dragon3602010 3 ปีที่แล้ว +3

    hello, what if we are working with pipenv, how to make it working with the command typer to get the autocomplete ? and also can we combine fzf instead of the autocomplete? thanks

  • @philtoa334
    @philtoa334 3 ปีที่แล้ว +1

    Nice.

  • @yoyopiyoyopi
    @yoyopiyoyopi 3 ปีที่แล้ว +3

    Nice Mate!

  • @unstoppablegaming2079
    @unstoppablegaming2079 2 ปีที่แล้ว +1

    thanks

  • @SHONNER
    @SHONNER 2 ปีที่แล้ว +1

    Reminds me of click 8.0.4 for Python.

  • @abhityagi6562
    @abhityagi6562 2 ปีที่แล้ว +2

    I am trying to use nubian, it is full fledged CLI creator with a feel of Cisco IOS or other network vendor devices, it was developed for LDShell by Facebook developers, but I can gurantee it is a hard nut to crack trying to include my commands and no luck so far after 5 hours..

  • @Lunolux
    @Lunolux 3 ปีที่แล้ว +1

    nice

  • @winnumber101
    @winnumber101 3 ปีที่แล้ว +4

    "and it's just gonna show me my IQ of 500... so this is a pretty good feel-good app right now, feeling pretty smart" lmao

  • @islandcave8738
    @islandcave8738 3 ปีที่แล้ว +1

    This is better than optparse and argparse.

  • @VOGTLANDOUTDOORS
    @VOGTLANDOUTDOORS ปีที่แล้ว +1

    ENJOYED the video - nicely done !
    INTERESTING little package, but some comments/challenges already encountered:
    1. the documentation uses the term "main" FAR TOO MUCH and in TOO MANY PLACES to see when you NEED to use "main" and when it's OPTIONAL (sloppy documentation);
    THAT IS:
    - you DON'T need to name your file "main.py" python mtcbrain.py hello
    2. the FIRST thing I noticed was that the command names MUST be in ALL LOWERCASE; NO CamelCase, NO underscored_Names :-( !!!
    THAT IS, the following function names either WORK or DON'T
    - hello() WORKS (we KNEW this) :-)
    - Hello() DOESN'T WORK (that came as a surprise, but it's also BAD NAMING CONVENTION for python FUNCTIONS, so nobody noticed anyways) :-O
    - helloThere() DOESN'T WORK (this came as a REAL SURPRISE, because this is likely the MOST COMMON NAMING CONVENTION in all of python) :-O
    - hello_there() also DOESN'T WORK (this came as ANOTHER SURPRISE) because this is the 2ND-MOST-COMMON NAMING CONVENTION in all of python) :-O
    - hellothere() WORKS and will have to do for anyone trying to use multi-word (sorry "multiword") function names ;-)
    3. even your ARGUMENT NAMES must also be ALL LOWER CASE !!!
    THAT IS:
    - hellothere(userName: str) DOESN'T WORK
    - hellothere(username: str) DOES WORK
    I'll CONTINUE test-driving this package as it's the FIRST one I tried, so I've NOTHING to compare to which to compare it, and will report back.
    HOPE THIS HELPS OTHERS TRYING TO BUILD CLI-TYPE APPS IN PYTHON - it's a GOOD THING TO DO, and containerizing PYSIMPLEGUI type apps ISN'T CROSS-PLATFORM (yet) !
    Cheers,
    -Mark Vogt, Principal Data Scientist, AVANADE (www.avanade.com)

  • @thiagoalves5404
    @thiagoalves5404 2 ปีที่แล้ว

    Is it possible to make the CLI commands interactive?

  • @JH-vz3yj
    @JH-vz3yj 26 วันที่ผ่านมา

    All nice and good but this, like all of the other tutorials it is for the command line arguments when you LAUNCH the python script
    (i.e., python nameOffScript --arg1 val etc...).
    My use case is that I have a command line within the launched program and I want it to parse my dozens of commands every time I receive command input. How do I bend it to that? I naively tried putting the app() call after the input command but it is just confused

  • @suwenhao9864
    @suwenhao9864 5 หลายเดือนก่อน

    hi , sir . How could I install "typer" in powershell?

  • @jpeg.600x2
    @jpeg.600x2 3 ปีที่แล้ว +1

    how do u start the program from the command line? ive tried "python .\Program.py" (without quotations) but it hasnt worked

    • @oxycodin2253
      @oxycodin2253 3 ปีที่แล้ว

      Try python3 ./program.py

  • @charleswilson7596
    @charleswilson7596 3 ปีที่แล้ว +2

    I'm having a little trouble getting the auto completion to work in ubuntu. I'm probably missing something basic.
    (venv) charles@pc-1:~/Projects/py_trader$ type
    type typeset
    (venv) charles@pc-1:~/Projects/py_trader$ typer --install-completion
    Command 'typer' not found, but can be installed with:
    sudo snap install erlang
    If you can take a look at it I would appreciate it.
    Also this is the simplest cli I have tried very intuitive.
    edit: didn't realize I had to pip install typer-cli

    • @python360
      @python360 ปีที่แล้ว

      Thank you, this tip helped me get it working!

  • @ItzShinePlayz
    @ItzShinePlayz 2 ปีที่แล้ว

    How do I code a invalid cli when user type the wrong command that is not in the list using type?

    • @jwalantbhatt527
      @jwalantbhatt527 10 หลายเดือนก่อน

      Create a Class with Enum

  • @muhammadnoorani7369
    @muhammadnoorani7369 ปีที่แล้ว

    I made a cli similar project, I want to deploy it and make it accessible to users. How can I do it??

  • @jabuci
    @jabuci 9 หลายเดือนก่อน

    How does it differ from the click module?

    • @kylethescientist
      @kylethescientist 5 หลายเดือนก่อน

      Typer is built on top of click, so they are similar in many ways

  • @writethatdown100
    @writethatdown100 2 ปีที่แล้ว

    What do I do if it says: "zsh: permission denied: ./main.py"?

    • @DevOpsJourney
      @DevOpsJourney  2 ปีที่แล้ว

      Permission issue. Try marking your script as executable. I think this might help you if not google. chmod +x ./main.py

  • @Wallee580
    @Wallee580 2 ปีที่แล้ว +1

    4:10 You can just add the name variable as another argument to print, no need for an F string :D

  • @mac-cygwin6264
    @mac-cygwin6264 3 ปีที่แล้ว +1

    i cant install typer autocompletion

    • @DevOpsJourney
      @DevOpsJourney  3 ปีที่แล้ว

      What error do you get?

    • @mac-cygwin6264
      @mac-cygwin6264 3 ปีที่แล้ว +1

      @@DevOpsJourney typer is not a cmdlet, program or batch file, i used powershell

  • @yogimanuk
    @yogimanuk 2 ปีที่แล้ว

    So I am not trying to be a smart ass here, this is simply an observation that has puzzled me for years. You need 7 lines of code to output 1 line to the console using python, in BASIC for example you need 1 [ 10 Print "Hello" ] using BASH you need 1 echo "Hello" so I have to ask why does python seem so inefficient and long winded. Oh and this doesn't seem to be just Python other languages have similar issues. It seems like a very poor way to program computers to me. Perhaps this explains why software is so over bloated these days.

    • @DevOpsJourney
      @DevOpsJourney  2 ปีที่แล้ว

      I'm not sure I agree. If all you wanted was to do was print to the console you would just type print("hello")

    • @ragnarresh
      @ragnarresh 2 ปีที่แล้ว +1

      "7 lines of code to output 1 line " LMAO

    • @GunZFoX
      @GunZFoX 2 ปีที่แล้ว

      You are wrong. Some languages uses those 7 lines of code to print 1 line, that's true, but the other lines could say to the system "Simply import this library which is the one I need for this 1 line task code" so you don't overload your system loading unecessary libraries.
      You can see this from 2 perspectives: 7 lines of code to realize 1 task, or 7 line of codes to realize the same task but in a more ordered and logic way.