Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making exe file from python that uses command line arguments

Tags:

python

py2exe

I want to create an exe from a python script that uses command line arguments (argv)
From what I've seen py2exe doesn't support command-line-arguments

What can I do?

EDIT: I was using a GUI2Exe tool, so I just missed the Console flag, but the accepted answer is perfectly correct

like image 467
Boaz Avatar asked Feb 29 '12 10:02

Boaz


People also ask

Can you turn a Python file into an EXE?

It is not that you share that Python program with everyone, and they will run this script in some IDLE shell. But you want everyone to run your Python script without the installation of Python. So for this work, you can convert the . py file to .exe file.

How do you use command line arguments with a Python program?

To add arguments to Python scripts, you will have to use a built-in module named “argparse”. As the name suggests, it parses command line arguments used while launching a Python script or application. These parsed arguments are also checked by the “argparse” module to ensure that they are of proper “type”.

Which is better PyInstaller or py2exe?

In PyInstaller it is easy to create one exe, By default both create a bunch of exes & dlls. In py2exe its easier to embed manifest file in exe, useful for run as administrator mode in windows vista and beyond. Pyinstaller is modular and has a feature of hooks to include files in the build that you like.


1 Answers

setup(console=['hello.py'])

I believe the line you want to use looks like this.

I tested this with 2 files:

hello.py

import sys

for arg in sys.argv:
    print arg
print "Hello World!"

And setup.py

from distutils.core import setup
import py2exe

setup(console=['hello.py'])

I ran these commands:

python setup.py py2exe

And then in the dist folder, I ran this:

hello.exe foo bar

Result:

hello.exe
foo
bar
Hello World!
like image 191
jgritty Avatar answered Sep 29 '22 11:09

jgritty