1

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?

OCa
  • 298
  • 2
  • 13
Diamoniner12345
  • 414
  • 2
  • 10
  • Why do you want an `.exe`? `.exe` files only works on Windows, whereas python files work on anything that has python installed. Having a hugely bloated executable is pretty much worse in every respect compared to just the .py file. – Mike 'Pomax' Kamermans Jul 20 '23 at 17:31
  • That would depend on how you "converted to an .exe file". – Scott Hunter Jul 20 '23 at 17:32
  • 1
    In case a user doesn't have python installed, i wanna make it like they can just run a regular application – Diamoniner12345 Jul 20 '23 at 17:32
  • That's a little weird... why wouldn't you just tell them to grab a copy of python? – Mike 'Pomax' Kamermans Jul 20 '23 at 17:33
  • I have to assume that the user has no knowledge of coding. Which is why I was hoping to just make an exe file that you can just double click on to run the code – Diamoniner12345 Jul 20 '23 at 17:36
  • All you need is a short `main` method to convert the command line parameters to arguments to the function. The function by itself is not executable. – Tim Roberts Jul 20 '23 at 17:38
  • @Diamoniner12345 how would this work? A python function is meant to be used by Python code. How would the user invoke the program, and how would that executable be related to the function? – juanpa.arrivillaga Jul 20 '23 at 18:21
  • In other words, if you used some tool to make an `.exe` from your current program, the `.exe` would simply run python with the above module, but that module just imports some things and defines a function. then the program would terminate, and *nothing really would happen* – juanpa.arrivillaga Jul 20 '23 at 18:23
  • "What will happen if this gets converted to an .exe file. Will it automatically request arguments for the function MySummary or something like that?" no, it won't just magically do all that for you. – juanpa.arrivillaga Jul 20 '23 at 18:23
  • @juanpa.arrivillaga Yeah, the aim would be to automatically request arguments when double clicking the exe file. Then, it calls the function. Do you think using input() would work? – Diamoniner12345 Jul 21 '23 at 17:29

3 Answers3

1

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
nisakova
  • 89
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Fredrik Aug 02 '23 at 12:08
  • @nisakova Could you please explain the meaning behind the line `if __name__ == "__main__"`? If I convert your code to an exe file and run it, will it automatically open up the command line to ask for the arguments of the function MySummary? – Diamoniner12345 Aug 14 '23 at 21:13
  • @Diamoniner12345 so you will be running your module like python musummary.py 'path/to/file_in', 'you_method', https://docs.python.org/3/library/argparse.html and here is the more information " '__main__ is the name of the environment where top-level code is run, because it imports all other modules that the program needs " https://docs.python.org/3/library/__main__.html#module-__main__ – nisakova Aug 15 '23 at 00:10
0

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.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • It looks like the OP requests a standalone executable. Shouldn't the file contain an installation of Python? – OCa Jul 28 '23 at 09:26
  • 1
    No, the user asked "what will happen if this gets converted to an exe", so I was providing a method that exposes the function at the command line. They already seem to know about pyinstaller. – Tim Roberts Jul 28 '23 at 17:39
0

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.