Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass arguments to a python made exe at runtime?

Tags:

python

I'm experimenting with file I/O. I have a small practice program that creates a text file when run. I packaged it with pyinstaller so that double clicking on the exe creates a new folder and places a text file with "hello world" inside of it. Easy peasy.

Then I started wondering about main(). This is just a function like any other, right? So does that mean I can pass arguments to it at runtime?

I was thinking about the Steam client and how you can put stuff like '-dev' and '-console' in the shortcut. Is there a way to do this to a python exe that I have made?

I may be explaining terribly, so here's an example:

def makeFile(string):
    if string:
        f = open('mytext.txt', 'w') #create text file in local dir
        print >> f, 'hello, ' + string + '! \nHow are ya?'
        f.close()
    else:
        f = open('mytext.txt', 'w') #create text file in local dir
        print >> f, 'hello, person! \nHow are ya?'
        f.close()

def main(string = None):
    makeFile(string)     

So if I take this code and make it an exe, would I be able to add my optional arguments somehow.

I tried the above code, and the running test.exe --"myname" but that didn't work.

Is there a way to do this?

like image 326
Zack Avatar asked Apr 06 '12 17:04

Zack


People also ask

How do you pass an argument in Python exe?

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.

Can you package python into an exe?

You can use PyInstaller to package Python programs as standalone executables. It works on Windows, Linux, and Mac.

Does converting Python to exe make it faster?

No, not really. Since it's merely a wrapper it provides the necessary files needed to run your code. Using Cython could make your program run faster by being able to compile it using C.

Can Python script be run as an executable?

py extension with a file type (Python. File) and gives that file type an open command that runs the interpreter ( D:\Program Files\Python\python.exe "%1" %* ). This is enough to make scripts executable from the command prompt as 'foo.py'.


1 Answers

What you're looking for is either the sys module, or the optparse module.

sys will give you very basic control over command line args.

For example:

import sys

if __name__ == "__main__":
    if len(sys.argv)>1:
        print sys.argv[1]

In the above example, if you were to open up a shell and type -

test.exe "myname"

The resultant output would be:

myname

Note that sys.argv[0] is the name of the script you are currently running. Each subsequent argument is defined by a space, so in your example above

test.exe -- myname

argv[0] = "test.exe"
argv[1] = "--"
argv[2] = "myname"

Optparse gives a much more robust solution that allows you to define command line switches with multiple options and defines variables that will store the appropriate options that can be accessed at runtime.

Re-writing your example:

from optparse import OptionParser

def makeFile(options = None): 
    if options:
        f = open('mytext.txt', 'w') #create text file in local dir
        print >> f, 'hello, ' + options.name + '! \nHow are ya?'
        f.close()
    else:
        f = open('mytext.txt', 'w') #create text file in local dir
        print >> f, 'hello, person! \nHow are ya?'
        f.close()



if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option('-n','--name',dest = 'name',
                      help='username to be printed out')
    (options,args) = parser.parse_args()
    makeFile(options)

You would run your program with :

test.exe -n myname

and the output (in myfile.txt) would be the expected:

Hello, myname!
How are ya?

Hope that helps!

like image 117
Jeff_C Avatar answered Oct 04 '22 18:10

Jeff_C