2

I have a python script that will ultimately be converted into an .exe file. The way it is supposed to work is that a user will drag an excel file and drop it on to the python file, which will run, and create another output excel file.

However, it needs 2 inputs from the user: groups and size.

When I drop the excel file, the command prompt opens up automatically, and prompts me for the inputs. However, after submitting the values, the command prompt closes automatically without executing the rest of the script.

The problem goes away if I hard-code the values for groups and size without prompting the user.

Here is the code:

import pandas as pd 
import sys

x = sys.argv[1]
file = pd.read_excel(x)
file = file.sample(frac=1).reset_index(drop=True)
file['Order'] = file.groupby('GENDER').cumcount()
file = file.sort_values(['Order', 'GENDER'], ascending=[True,False]).reset_index(drop=True)

groups = input ("Group number?") 
size = input ("Group size?") 
out = {}
count = 1
students = len(file)
std_sorted = 0

for i in range (0, len(file)-size, size): 
    if count != groups: 
        out["group"+str(count)] = file[i:(i+size)]
    else: 
        out["group"+str(count)] = file[i:]       
    count += 1 
    std_sorted += size 


if std_sorted != students: 
    out["group"+str(count)] = file[std_sorted:]

index = 0
writer = pd.ExcelWriter('output.xlsx')
for i in out:
    out[i].pop("Order")
    x = pd.DataFrame(out[i])
    x.to_excel(writer, index = False, sheet_name="Sheet"+str(index))
    workbook = writer.book # Access the workbook
    worksheet= writer.sheets["Sheet"+str(index)] # Access the Worksheet

    header_list = x.columns.values.tolist() # Generate list of headers
    for i in range(0, len(header_list)):
        worksheet.set_column(i, i, len(header_list[i])+2) # Set column widths based on len(header)

    index += 1

writer.save()
  • *the command prompt closes automatically without executing the rest of the script.* - are you sure it is not executed? The window will close if nothing is holding it. That doesn't mean it wasn't executed. You can add `input("Press Enter to exit...")` at the end of your script – Tomerikoo Jun 06 '20 at 14:21
  • 1
    Does this answer your question? [How to stop Python closing immediately when executed in Microsoft Windows](https://stackoverflow.com/questions/12375173/how-to-stop-python-closing-immediately-when-executed-in-microsoft-windows) or: https://stackoverflow.com/questions/3591807/how-can-i-stop-python-exe-from-closing-immediately-after-i-get-an-output – Tomerikoo Jun 06 '20 at 14:22
  • Yes, I am confident that it was not executed. I tried the ```print (groups)``` and even that output was not printed. If I hard-code the values inside the script for ```groups``` and ```size``` then the file is executed, which is indicated by the generation of an output excel file. – Mohammad Osaama Bin Shehzad Jun 06 '20 at 14:23
  • @Tomerikoo Unfortunately, prompting user again for an input at the end of the script does not happen either. As soon as I provide the values for ```groups``` and ```size``` and hit enter in the command prompt, it just closes even if I prompt the user again at the end of the script. This is why it's so confusing. – Mohammad Osaama Bin Shehzad Jun 06 '20 at 14:25
  • Again, the output was not printed because probably the window closed before you could even see it. Add the `input` line at the end and the print statements after the `groups` and `size` input and I'm sure they will be printed – Tomerikoo Jun 06 '20 at 14:25
  • You might have an exception in the middle. Try running it from an IDE or from an open shell/cmd to see if there are any errors – Tomerikoo Jun 06 '20 at 14:26
  • @Tomerikoo they are not printed. I checked many times before coming here. I just checked again. please feel free to use my code to check on your own. – Mohammad Osaama Bin Shehzad Jun 06 '20 at 14:27
  • @MohammadOsaamaBinShehzad weclome to SO! Perhaps adding some logging throughout the script will help you understand what's going on? https://docs.python.org/3/howto/logging-cookbook.html – Aleksander Lidtke Jun 06 '20 at 14:27

2 Answers2

3

I could find your problem just from copy-pasting your code to Pycharm without even running it (Pycharm does a great job of code inspection)...

The line:

for i in range (0, len(file)-size, size): 

will always raise a TypeError exception. This happens because len(...) is an int, BUT size is a string because it's an input. You need to change your inputs to:

groups = int(input("Group number?"))
size = int(input("Group size?"))
Tomerikoo
  • 12,112
  • 9
  • 27
  • 37
  • My bad! Thanks so much @Tomerikoo! I just realised it too. Thank you so much! Can't believe how I missed it after years of coding... – Mohammad Osaama Bin Shehzad Jun 06 '20 at 14:33
  • 1
    Happens to the best. But as I tried to tell you in the comments, the basic step of debugging this will be to run in an IDE or a shell as using an executable you can't really see what happened. If you would just run the script from command line you will immediately see the error... – Tomerikoo Jun 06 '20 at 14:35
  • By the way, @Tomerikoo, it's the first time that I will be converting a ```.py``` into an ```.exe``` . If the user gets my ```.exe``` only and they don't have python or its libraries e.g. pandas installed, will they be able to run this file without any issue? – Mohammad Osaama Bin Shehzad Jun 06 '20 at 14:36
  • Unfortunately I don't have a lot of experience converting `.py` to `.exe`. I mostly use Python for scripting in a work environment where the scripts are run directly from a shell. But I believe that there are packeges that pack your program as a stand-alone with all necessary dependencies. I am sure you can find alot of information on the subject here on SO – Tomerikoo Jun 06 '20 at 14:38
3

Try

groups = int(input ("Group number?")) size = int(input ("Group size?"))

instead of

groups = input ("Group number?") size = input ("Group size?")

I think it a typecasting issue. Try it maybe it fixes your issue.