Yesterday我问了一个关于同时使用 Excel 和 Python 的问题。找到了解决方案:使用 xlwings
包。
但是,还有另一个问题 - 我无法将我的 .py
文件保存为可执行文件 (exe
)。
这是我尝试保存的代码:
doiterations.py
import xlwings as xl
import numpy
import time
wb = xl.Workbook.active()
sheet = wb.active
iter = input("How many iterations do you need? \n")
i = 0
cell1 = raw_input("Write a column where you need to iterate \n")
cell2 = int(raw_input("Write a row where you need to iterate \n"))
while True:
i += 1
if i <= iter:
arg = numpy.random.uniform()
xl.Range("%s%d" % (cell1, cell2)).value = arg
else:
break
wb.save()
print("Done!")
time.sleep(2)
我尝试使用 cx_freezer
并使用以下代码制作了一个 setup.py
文件:
from cx_Freeze import setup, Executable
setup(
name = "Uniform distribution generator",
version = "1.0",
description = "Uniform distribution generator",
executables = [Executable("doiterations.py")]
)
此类具有类似代码的 setyp.py
文件可与其他模块正常配合使用。然而,这次我得到了一个错误no file named sys
:
cx_Freeze.freezer.ConfigError: no file named sys (for module collections.sys)
我尝试通过以下命令使用 PyInstaller
包:
再次遇到错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc0 in position 7: ordinal not in range(128)
我通过 Google 和 Stackoverflow 进行了搜索,发现了一些关于此问题的评论,可能有助于找到解决方案:
https://mborgerson.com/creating-an-executable-from-a-python-script http://www.dreamincode.net/forums/topic/192592-making-an-exe-file-with-pyinstaller/
cx_freeze fails to create exe with pandas library cx-freeze error on build Traceback from CX_Freeze doesn't make sense
我的 Python 版本是 2.7。
请帮助解决问题并创建一个有效的可执行文件!
最佳答案
至少在 cx_freeze 的情况下可以找到解释:https://bitbucket.org/anthony_tuininga/cx_freeze/issues/127/collectionssys-error
不幸的是Python Package Index不提供包含必要更改的 cx_freeze 版本。 Microsoft Visual C++ Compiler for Python 2.7之后可以安装新版本的cx_Freeze已安装。在这种情况下,可以使用 pip 命令从 Python 包索引以外的其他位置安装 python 包
pip install --upgrade https://bitbucket.org/anthony_tuininga/cx_freeze/get/tip.zip
这需要在Anaconda 提示符 中完成,该提示符应该可以从“开始”菜单 中找到。如果在安装 Anaconda 期间修改了 PATH,则命令提示符即可。
关于python - 使用 cx_freeze 和 PyInstaller 在 Python 中创建 .exe 文件时出错(包括 xlwings),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37896236/