Build Command Line Interface in Python with pyinstaller

Author - Webner
1.05.2020
|
0 Comments
||

Command Line Interface (CLI)

A command-line interface is a tool through which the user can interact with a program. Users can run the necessary commands and pass system arguments to the code and the response is generated as per written in the program.

Advantages of Command Line Interface:

  1. It requires fewer resources.
  2. Easy to Use.
  3. Available in all operating systems.

Pyinstaller

Pyinstaller is used to merge all the code of python (single or multiple), all installed libraries and dependencies into one single file. Using this, the user can run the program from the packaged code without the need for any library or any modules used in the code.
If a user sends that packaged app to other users and that user does not have any dependencies installed in its system(not even python) even then the user can run that packaged app.

Installation

To install Pyinstaller through pip:
Pyinstaller requires python script to make the packaged app. The code can be grouped into a single folder or a single file. It automatically detects the libraries or other methods that have used in python script using the import method to merge in a single file. However, it is not a cross-compiler. To make an app for the Windows app, the pyinstaller commands need to run on the window operating system and the same for the Linux case also.

Usage

Pyinstaller –onefile /path/to/yourscript.py
This will generate a single file in a directory named dist. This file can be run using the command-line interface.

Example – To make a CSV file reader –

csvFile.csv

S.No. RollNo Name
1 Webner1 Test User1
2 Webner2 Test User2

Helpers.py

import CSV

def readCsvFile(fileLocation):

with open(fileLocation,’rt’)as f:
data = csv.reader(f)
for row in data:
print(row)
return

CSVReader.py

import sys
from Helpers import *

def main():
csvFileLocation = sys.argv[1] csvFileData = readCsvFile(csvFileLocation)

main()

Then run pyinstaller command –
pyinstaller –onefile csvReader.py
It will generate a packaged app in the dist folder.
Note – The generate packaged app will work only for the operating system from which it is generated.

Now, open command line –

  1. cd packaged app directory
  2. For Ubuntu – ./csvReader /home/User/Desktop/csvFile.csv
  3. For Windows – csvReader D:/test/csvFile.csv



Output –
[‘S.No.’, ‘RollNo’, ‘Name’]

[‘1’, ‘Webner1’, ‘Test User1’]

[‘2’, ‘Webner2’, ‘Test User2’]

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *