Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to input arguments after compiling python program with PyInstaller

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()
like image 993
ohmygoddess Avatar asked Sep 22 '14 23:09

ohmygoddess


People also ask

How do you pass an argument in Python exe?

In order to pass arguments to your Python script, you will need to import the sys module. Once this module is imported in your code, upon execution sys. argv will exist, containing a list of all of the arguments passed to your script.

Does PyInstaller compile Python code?

However, PyInstaller bundles compiled Python scripts ( . pyc files).

Does PyInstaller include imports?

python - PyInstaller does not include imports - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.


1 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>

like image 163
ashwinjv Avatar answered Oct 06 '22 12:10

ashwinjv