Create Python CLI Tool | Python Click Tutorial | Python Automation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone my name is hayden and welcome to my channel in this video we're going to be building a command line utility tool to optimize and streamline your life [Music] so in this video series we are going to be building a command line utility tool to optimize organize and streamline your workflow on your pc i've wanted to build something like this for a while because i have a lot of kind of disconnected python scripts and other kind of utility scripts that i have around my pc that i run for various things and i want to bring them into kind of one complete package so in short we're going to be building a cli tool that will kind of be a foundation for lots of repetitive tasks that we could automate so we can start writing things on the command line like check the weather or amazon this or google that or you know turn off the bedroom lights things like this so we can start building a foundation that we can build upon and add more and more utility scripts on top of help us automate and speed up our workflow on our computers the goal of this project is to bring everything into one place and have centralized location for all these utility scripts that i can access at any time so i decided to build this using a cli tool if you're not familiar with what cli tool is or stands for it stands for a command line interface tool which is basically command prompt on windows or terminal on mac where you can start typing commands in and it's basically a way of interacting with the computer without a kind of a user interface as a developer i use clitorals quite frequently and and i've never really built one myself and i've always wanted to so it's kind of a good way for me to learn and document the process at the same time so i'm going to call my command line tool eve a because it sounds quite cool mystical but b because it's kind of super quick to type on a keyboard we're going to be building the tool in python and we're going to be using a package called click which allows you to create very powerful and useful command line tools and we'll go through more about that when we're kind of doing the coding section but kind of in short what you can expect will be we'll build a tool that you can type for example like eve space weather and that will tell us the weather in some location or you know if people like this series you know i'll start to build out eve bigger and bigger and bigger and more and more apis and you know hopefully it'll be really interesting to see where the project goes so now let's jump over to vs code where we'll start the coding section for this so as you can see here i've got an empty vs code project called eve and we're going to use this to build out from so first things first we are going to need a required file and like i said we're going to be using the click package and as of recording the current latest version is click 7.1.2 so basically what happens here well what we want is to have the ability to say eve you know get me the weather or eve amazon me this or eve you know turn off the lights in my room so all these kind of commands will just point to different functions in python and they'll run and they'll hopefully do you know whatever we want to do so for example you know if we want to do podcasts there'll be a podcast function that it'll point to but obviously as you can see at the minute there is nothing called eve the e distribution was not found on my computer so what we need to do is use a package called pip that will install it onto our computer and but what first thing i want to do is just make myself a virtual environment so that we can um keep everything contained i just like to do it this way so once you've made your virtual environment you can just jump into it and activate it uh there we go and we now have it activated so i don't want to explicitly install pip myself or sorry i don't want it because obviously we've got this requirement.txt file we need to click package but what we can actually do is uh make what's known as a setup setup file which uses which is what pip uses to build packages out so in a setup file you basically need to tell pip how to install your package what requirements to gather and kind of all the things that basically in order to bundle this kind of executable up so what we can do is say from uh set up tools import setup and find alt right find packages and then we can use the setup function in order to build our eve package so let's call it the name of our package eve this is version 0.1 [Music] we need to tell pip where to find our package and by default i believe it looks for a folder in your directory that is the same name as the name you put here so let's make that now so let's make a new folder called eve and i just want you to put inside that folder for now an init dot py file just to make it a legitimate python package so next thing we need to say is say include package data true this basically just bundles up all the requirements when you do the pip install rather than to have to install themself and we also just need to say install requires um we just need to say what requirements does this install require so i do this by making a function called read requirements rather than duplicating the requirements basically so this needs a list of strings a python list of strings in order to work and i hate of of all the requirements we need by duplicating the requirements so what i do is um make a function like this which basically reads the requirements in off the fly so you only need to change it in that one place um and you don't need to kind of like put it twice this all so this is basically just going to read in our requirements.txt file and it'll read in our requirements.tdxt file as a string and then we'll split it on a new line and then return us a list of strings and which will go here so that basically means we only need to change it in our requirements.txt file and it will get picked up every time so the last thing we need to do is tell pip when it installs our package where the entry point to the package is so like where the initial part of um part of code it needs to look at is so in order to do this because it's a console or cli tool we just need to say console scripts and all we need to say is basically when you see the command eve i want you to look into the eve package and uh there'll be a file called cli so let's make that now so we'll go up to the eve package uh let's make a cli.py file just an empty file for the minute so go into the eve package look for a cli.py file and look for a function in that file called cli and that'll be the entry point to our um eve command line tool cool so that is all you need to know uh reading in the requirements so we've got a package called eve version 0.1 find the package in you know the eve directory include all the requirements bundled up these are the requirements and this is the entry point perfect so what we can do now is jump over to the eve actual package and start building out the cli function so let's do that now so go into eve and cli.py jump into here and like i said we're going to need click in order to do this like it said in that um like i said in the setup py file we need a function inside this file called cli which is the entry point and for now i'm just going to print hello world but uh click offers us a nice um a nice uh method called um echo which um basically handles all kind of like unicode escaping characters and stuff like this when uh when you run it on the command line um but that's not all we need to do we also need to basically tell uh click that this is a click command with the click dot command decorator so just like that so that's all you need in that file so that's pretty much it once you're there once you've uh installed well got a requirement so i've called that copied the setup url to look like this so it looks for the packages and installs them correctly and so once we're here we can now say so make sure that you're inside uh not inside this directory the eve we created but actually inside your project directory and we can just say pip install and if we say dot it means here so pip install into it this package here it will look for setup py on its current directory and you can see that because if i go um ol you can see that we've got a setup py in our current and directory so if i say pip install here and i just want to add a little flag in front of this called editable which basically means um every time i'd have to make a code change here i'd have to reinstall the package for it to pick it up but if i put editable as the flag it will pick up those code changes without me having to reinstall so now i can do pip install this and you can see it's looking in there it's installing the click version and successfully installed click and it's installed the eve package so now you can see if i come here and type eve oh a little typo there so it's click command um and yeah you can see if i type eve again now i fix that packaging because we've got the editable flag on you can see it picked up my code change straight away so for example yeah again i could change this to you know hello hayden and then run run that and you can see it's now changing to hello hayden and so what we'll do in the next couple of videos is build these out to have more and more complex ones so for example we'll have like a click uh we'll have a click command you know that's like uh weather and that will return us the weather or have one amazon or google to amazon or google something um you know we'll basically just build this out and it's a nice way to kind of bring together all the code into one place that we can just now all access through this eve command or whatever you want to call yours obviously so there we have it we've basically built the foundation of the eve command line tool there in the next video i'll be building out more to add a command called eve weather so it will use the weather api in order for you to get the weather at your current location and we'll expand on this more and more to add more you know helpful and useful commands that we need to do to kind of like automate and streamline our life hope you enjoyed this tutorial there's other tutorials on my page so be sure to check out those playlists and if you could take a moment out of your day to like this video and subscribe i'd also really appreciate that thank you very much for watching and have a great day
Info
Channel: Software Engineer Haydn
Views: 9,282
Rating: undefined out of 5
Keywords: software tutorials, coding tips, software developer haydn, cli tool python, python command line tool, create cli tool python, build cli tool python, python cli tool, python cli tool library, python cli tool template, how python cli tools, python click, python click tutorial, python cli, python click examples, python click option, python click argument, how to create python cli, build cli python, python click cli tutorial, python click cli example, click tutorial python
Id: Jr4QDJwwj60
Channel Id: undefined
Length: 10min 46sec (646 seconds)
Published: Wed Aug 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.