How to input arguments after compiling python program with PyInstaller

Question:

After import sys, I use sys.argv to get input arguments.

But after I compile my program with PyInstaller, the exe program will not accept my input. Instead, it uses the default value I set for the program.

If I run it with python this_script.py it waits for my input to specify the wait_time. However, after I compile it with PyInstaller, if I double click the exe file there is no place for me to input the wait_time.

How can I compile it and let the exe file accept my input?

import sched, time
import sys
    
s = sched.scheduler(time.time, time.sleep)
    
# wait_time is an integer representing how many seconds to wait.
def do_something(sc, wait_time): 
    # Here will be the code for doing something every after "wait_time " seconds
    sc.enter(wait_time, 1, do_something, (sc, wait_time))  
    
    try:
        wait_time = int(sys.argv[1])
    except IndexError:
        wait_time = 5    
    
    
# s.enter(wait_time, 1, do_something, (s,))
s.enter(wait_time, 5, do_something, (s, wait_time))
s.run()
Asked By: ohmygoddess

||

Answers:

If you click on the exe to open it:

Usually, when you double click the exe, there is only one argument which is <EXEfilename>. Create a shortcut for that exe. In the properties for that shortcut, you will see a property called Target which will contain <EXEfilename> change that to <EXEfilename> <arg1> <arg2>. When you use this shortcut to open the exe, it calls the target, which is this call <EXEfilename> <arg1> <arg2>. You can then access arg1 and arg2 using sys.argv

If you use command line:

Just call it as C:> <EXEfilename> <arg1> <arg2>

Answered By: ashwinjv

sys.arg[0] is helpful if you run the program through command line. Instead of using sys.arg[0], use

input()

You can also use input("<Statement to show to user>: "), to show a statement to the user. Then create your executable (.exe) by compiling the Python script using

pyinstaller –onefile pythonscript.py

Simply double click on the generated .exe file, a console will appear showing the following statement, Statement to show to user: .

You can also take multiple user inputs from user by using multiple input() statements in pythonscript.py. For example:

input_customer_segment = input("Enter a customer segment (For example: Daily user): ")
input_number_of_months = input("Enter number of months of consumption: ")

This will take multiple user inputs on pressing Enter after every input in the console that appears.

Answered By: Aruparna Maity
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.