22

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()
bicarlsen
  • 1,241
  • 10
  • 27
ohmygoddess
  • 619
  • 1
  • 7
  • 23
  • 1
    can you give us a small short concise and working example of your code that demonstrates this problem? Have you attempted to run it as a script and not a compiled executable? – Mike McMahon Sep 22 '14 at 23:07
  • How are you running the resulting `.exe` file? Are you running it from a command prompt or are you clicking on it? – Greg Hewgill Sep 22 '14 at 23:08
  • Are you supplying any command line arguments? – user2357112 Sep 22 '14 at 23:23
  • Above is the code. I want the program repeatly doing something every 5 mins. Then I can input wait_time = 300. I want to run the .exe directly instead of using python command line. – ohmygoddess Sep 25 '14 at 16:31

2 Answers2

25

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>

ashwinjv
  • 2,787
  • 1
  • 23
  • 32
  • 1
    Thank you! After I change add in the target, it works. But is there any way to allow me input the arg1 interactively? – ohmygoddess Sep 25 '14 at 16:39
  • I see this problem is due the the windows system. For linux, we can just run in the shell. For windows, we used to double click the exe, we cannot input unless we use cmd or add a gui to the program. – ohmygoddess Sep 25 '14 at 16:46
  • 2
    Or you can use the console with that opens up. If you are using python 2 its `arg1 = raw_input('Enter arg1')` if you are using python 3 its `arg1 = input('Enter arg1')`. This lets users enter the args in interactively. – ashwinjv Sep 25 '14 at 16:51
0

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.

Aruparna Maity
  • 174
  • 3
  • 7