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()
8
  • 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 Commented Jun 6, 2020 at 14:21
  • 1
    Does this answer your question? How to stop Python closing immediately when executed in Microsoft Windows or: stackoverflow.com/questions/3591807/… Commented Jun 6, 2020 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. Commented Jun 6, 2020 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. Commented Jun 6, 2020 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 Commented Jun 6, 2020 at 14:25

2 Answers 2

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?"))
Sign up to request clarification or add additional context in comments.

4 Comments

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...
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...
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?
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
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.