16
votes

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.

It will just use the default value I set up for the program. How can I compile it and let the exe file accept my input?

I hope I don't need to write a gui for it. Sorry for late reply. Here is the code:


import sched, time

import sys



s = sched.scheduler(time.time, time.sleep)




def do_something(sc,wait_time): //wait_time is an integer representing how many seconds to wait.

    //  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()    

If I run it with "python this_script.py", it will wait for my input of how many seconds for wait. But after I compiled it with pyinstaller, I double clicked the exe file. There is no place for me to input a number which is the wait_time.

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
How are you running the resulting .exe file? Are you running it from a command prompt or are you clicking on it? - Greg Hewgill
Are you supplying any command line arguments? - user2357112 supports Monica
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

1 Answers

19
votes

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>