ActiveState Powered by ActiveState

Recipe 266471: makeExe.py


Simple Python script to automate the creation of Python executables using py2exe.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
makeExe.py
- Simple Python script to automate the creation
  of Python executables using py2exe.

(c) 2004 Premshree Pillai (24/01/04)
http://www.qiksearch.com/
"""

## Run this file from Python root dir

import sys
import re

def getFileName():
	global fileName
	fileName = raw_input("Enter file name (rel or abs path, eg., python/file.py): ")
	try:
		fp = open(fileName)
		fp.close()
	except IOError:
		print "File does not exist!"
		getFileName()

getFileName()

package = re.split(":",fileName)
package = re.split("/",package[len(package) - 1])
package = re.split(".py",package[len(package) - 1])
package = package[0]

def getSetupName():
	global setupName
	setupName = raw_input("Enter name of setup file (or <enter> for default): ")
	if(setupName == ''):
		setupName = "setup.py"
	try:
		fp = open(setupName)
		fp.close()
		flag = raw_input("Setup file exists! Rewrite (0=no; else <enter>)? ")
		if(flag == "1"):
			getSetupName()
	except IOError:
		setupName = setupName

getSetupName()

fp = open(setupName,"w")
temp = """from distutils.core import setup
import py2exe
setup(name = "%s",
     scripts = ["%s"],
)""" % (package,fileName)
fp.write(temp)
fp.close()

sys.argv.append("py2exe")
execfile(setupName)

fp = open(setupName,"w")
temp = ""
fp.write(temp)
fp.close()

print "\n", "Executable created!"
print "Press <enter> to exit..."
if(raw_input()):
	exit

Discussion

To create Windows executables from Python scripts, we could make use of py2exe (available from http://starship.python.net/crew/theller/py2exe/). However this process is a little time-consuming. This Python script (makeExe.py) is an attempt to simplify this process.

Sign in to comment