This simple guide will show you how you can use pyinstaller and argparse to create a simple console application with python.
$ pip install --upgrade psutil
$ pip install --upgrade pyinstaller
Lets try this out by creating a simple console app that will list processes or applications that are currently running on your PC.
- Create a python script named proclist.py
- We use psutil.process_iter([‘pid’, ‘name’]) to get a list of all processes running on a machine
- The argparse ibrary allows the user of our script to specify a limit on the number of processes with print to the console.
- At this point we can distribute out script as a .py file and any user with python installed on their machine will be able to use our app.
Lets take it a step further
# We want the user to run the app as follows$ proclist --limit 9# instead of,$ python proclist.py --limit 9
2. Create an executable using pyinstaller
- Inside the directory where you script is located, run
$ pyinstaller --name proclist --onefile proclist.py
- This will create an executable inside distpath directory name proclist.exe
- At this point we can proceed to run our standalone executable as follows:
$ cd dist
$ proclist --limit 10
- You should see an output similar to this:
- You can go ahead and add the generated executable to the system path so that it can be executed from any location in the command line shell.
- You might also want to explore the argparse library further to see if you can add more functionality to our simple application. e.g. List 5 applications with the highest cpu usage in the last 5 minutes.