将.py转换为.exe
好的,相关的信息可以在这个讨论串里找到(这里叫讨论串吗?)。
抱歉,如果我应该只在那个讨论串里发言,我对这里的规矩不太熟悉,也不确定我的问题会不会被看到。
不过,我对那里的代码做了一些修改。这是我目前的最终版本。
import math
def convertString(str):
try:
returnValue = int(str)
except ValueError:
returnValue = float(str)
return returnValue
def addition(a, B):
return convertString(a) + convertString(B)
def subtraction(a, B):
return convertString(a) - convertString(B)
def multiplication(a, B):
return convertString(a) * convertString(B)
def division(a, B):
return convertString(a) / convertString(B)
def sqrt(a):
return math.sqrt(convertString(a))
def expo(a, B):
x = convertString(a)
y = convertString(B)
return math.pow(x, y)
def fact(a):
return math.factorial(convertString(a))
keepProgramRunning = True
print "Welcome to [Removed]'s 2011 4-H Project! This is a simple calculator coded in Python, which is a high-level programming language. Java, C, C++, and Perl are other high-level programming languages that you may have heard of. Press Enter to get started!"
print ""
raw_input('')
while keepProgramRunning:
print "Please select what you would like to do:"
print ""
print "1) Addition"
print "2) Subtraction"
print "3) Multiplication"
print "4) Division"
print "5) Square Root"
print "6) Exponentiation"
print "7) Factorial"
print "8) Quit Program"
print ""
print "Input the number of the action that you wish to do here, then press Enter:",
choice = raw_input()
if choice == "1":
print ""
numberA = raw_input("Enter the first addend: ")
numberB = raw_input("Enter the second addend: ")
print ""
print "The sum of those numbers is", addition(numberA, numberB)
print ""
print "Press the Enter key to continue."
raw_input('')
elif choice == "2":
print ""
numberA = raw_input("Enter the first term: ")
numberB = raw_input("Enter the second term: ")
print ""
print "The difference of those numbers is", subtraction(numberA, numberB)
print ""
print "Press the Enter key to continue."
raw_input('')
elif choice == "3":
print ""
numberA = raw_input("Enter the first factor: ")
numberB = raw_input("Enter the second factor: ")
print ""
print "The product of those numbers is", multiplication(numberA, numberB)
print ""
print "Press the Enter key to continue."
raw_input('')
elif choice == "4":
print ""
numberA = raw_input("Enter the dividend: ")
numberB = raw_input("Enter the divisor: ")
while float(numberB) == 0:
print ""
print "You cannot divide by zero. Please choose another divisor."
print ""
numberB = raw_input("Enter your divisor: ")
print ""
print "The quotient of those numbers is", division(numberA, numberB)
print ""
print "Press the Enter key to continue."
raw_input('')
elif choice == "5":
while True:
print ""
numberA = raw_input("Enter the number you wish to find the square root of: ")
if float(numberA) >= 0:
break
print ""
print "You cannot take the square root of a negative number."
print ""
print "The square root of that number is", sqrt(numberA)
print ""
print "Press the Enter key to continue."
raw_input('')
elif choice == "6":
print ""
numberA = raw_input("Enter the base: ")
numberB = raw_input("Enter the exponent: ")
print ""
print "The solution to that expression is", expo(numberA, numberB)
print ""
print "Press the Enter key to continue."
raw_input('')
elif choice == "7":
while True:
print ""
numberA = raw_input("Enter the number you wish to find the factorial of: ")
if float(numberA) >= 0:
break
print ""
print "You can only find the factorial of non-negative integers."
print ""
print "The factorial of that number is", fact(numberA)
print ""
print "Press the Enter key to continue."
raw_input('')
elif choice == "8":
print ""
print "Goodbye! Thank you for your time spent both judging my project and those of everyone else! Have a nice day! :)"
print ""
print "Press the Enter key to close."
raw_input('')
keepProgramRunning = False
else:
print ""
print "The key you have selected is not assigned to an action. Please choose from the listed options."
print ""
print "Press the Enter key to continue."
raw_input('')
我解决了关闭程序的问题,并且已经测试过,确保一切都能正常运行,显示也没问题(该有的空行都有,没有单词被拆分到不同的行等等)。现在我(我认为我)准备把它做成一个独立的程序。从我看到的情况来看,这是可能的,而且应该会把所有导入的内容都包含进去(在这个例子中,数学库是被导入的,我相信这就是它的名字,所以它应该会包含在独立版本里,对吧?)。所以,正如我标题所说的,我该如何把一个Python文件变成可执行文件呢?我已经尝试自己找答案,但提供的工具要么过时,要么我用的时候不管用。
有什么建议吗?
2 个回答
1
Py2exe 一直对我很有效。我用它把一个使用了PIL的脚本做成了exe文件,而且运行得很顺利。它的说明文档也很好,我几分钟就把它打包好了。
6
正如你提到的其他问题和过时的工具(我猜你是指py2exe,它最后一次更新是在2008年),可以看看PyInstaller和它的文档。
另一个工具是cx_freeze。