How to convert a python function (with parameters) into a standalone executable?

129 Views Asked by At

I have a python pandas function that uses some libraries and takes in a couple of parameters. I was wondering if it is possible to convert a python function with parameters to an application (so, .exe file). Would pyinstaller do the trick in this case?

Here is the code for my function:

import math
import statistics
import pandas as pd
import numpy as np
from scipy.stats import kurtosis, skew
from openpyxl import load_workbook

def MySummary(fileIn, MyMethod):
    DF = pd.read_csv(fileIn)
    temp = DF['Vals']
    
    if (MyMethod == "mean"):
        print("The mean is " + str(statistics.mean(temp)))
    elif (MyMethod == "sd"):
        print("The standard deviation is " + str(temp.std()))
    elif (MyMethod == "Kurtosis"):
        print("The kurtosis is " + str(kurtosis(temp)))
    else:
        print("The method is not valid")

What will happen if this gets converted to an .exe file. Will it automatically request arguments for the function MySummary or something like that?

3

There are 3 best solutions below

3
On BEST ANSWER

using the argparse module

import argparse

...
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Calculate stats: mean, sd, Kurtosis")
    parser.add_argument('fileIn', help="input .csv file")
    parser.add_argument('MyMethod', help="stats method")
    args = parser.parse_args()
    MySummary(args.fileIn, args.MyMethod)

[references][1]


  [1]: https://docs.python.org/3/library/argparse.html
2
On

The simplest way is:

import sys
...
if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage:  xxx.py  csvfile.csv  method")
    else:
        MySummary( sys.argv[1], sys.argv[2] )

If you want to be more use friendly, you could use the argparse module to process the arguments.

0
On

I think, it will be better if you host your code piece on any of the server and as ask user to append inputs in the link. In code you can use the values as system parameters something like sys.argv[1].your req link could look like https://RunmyScript/{value1}/{value2}

or you can provide user a ssh command to run the script on the machine where you have python and code setup.