使用cx_freeze和PyInstaller在Python中创建.exe文件时出错(包括xlwings)

时间:2022-01-28 18:03:15

Yesterday I asked a question about using Excel and Python simultaneously. The solution was found: using xlwings package.

昨天我问了一个关于同时使用Excel和Python的问题。找到了解决方案:使用xlwings包。

However, there is another problem connected with that - I can not save my .py file as an executable file (exe).

但是,还有另一个问题 - 我无法将.py文件保存为可执行文件(exe)。

Here is the code I try to save:

这是我尝试保存的代码:

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)

I tried to use cx_freezer and made a setup.py file with the following code:

我尝试使用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")]
        )

Such setyp.py files with the similar code properly worked with other modules. However, this time I got an error no file named sys:

具有类似代码的此类setyp.py文件与其他模块一起正常工作。但是,这次我收到错误没有名为sys的文件:

    cx_Freeze.freezer.ConfigError: no file named sys (for module collections.sys)

使用cx_freeze和PyInstaller在Python中创建.exe文件时出错(包括xlwings)

I tried to use PyInstaller package with the following command:

我尝试使用PyInstaller包以及以下命令:

使用cx_freeze和PyInstaller在Python中创建.exe文件时出错(包括xlwings)

and again faced an error:

并再次面临一个错误:

    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc0 in position 7: ordinal not in range(128)

使用cx_freeze和PyInstaller在Python中创建.exe文件时出错(包括xlwings)

I searched through Google and * and found some comments on this problem that may help to find the solution:

我搜索了Google和*,发现了一些有关此问题的评论,可能有助于找到解决方案:

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

cx_freeze无法使用pandas库创建exe cx-freeze错误构建来自CX_Freeze的Traceback没有意义

My version of Python is 2.7.

我的Python版本是2.7。

Please, help to solve the problem and create a working executable file!

请帮助解决问题并创建可运行的可执行文件!

1 个解决方案

#1


2  

At least in the case of cx_freeze an explanation can be found: https://bitbucket.org/anthony_tuininga/cx_freeze/issues/127/collectionssys-error

至少在cx_freeze的情况下,可以找到解释:https://bitbucket.org/anthony_tuininga/cx_freeze/issues/127/collectionssys-error

Unfortunately Python Package Index does not provide a version of cx_freeze that includes the necessary changes. A new version of cx_Freeze can be installed after Microsoft Visual C++ Compiler for Python 2.7 has been installed. It is possible to install python packages from other locations than Python Package Index with pip command, in this case

不幸的是,Python Package Index不提供包含必要更改的cx_freeze版本。安装Microsoft Visual C ++ Compiler for Python 2.7后,可以安装新版本的cx_Freeze。在这种情况下,可以使用pip命令从Python Package Index以外的其他位置安装python包

pip install --upgrade https://bitbucket.org/anthony_tuininga/cx_freeze/get/tip.zip

This needs to be done in Anaconda prompt that should be found from the Start menu. command prompt suffices if the PATH has been modified during the installation of Anaconda.

这需要在Anaconda提示符中完成,该提示应该从“开始”菜单中找到。如果在安装Anaconda期间修改了PATH,则命令提示符就足够了。

#1


2  

At least in the case of cx_freeze an explanation can be found: https://bitbucket.org/anthony_tuininga/cx_freeze/issues/127/collectionssys-error

至少在cx_freeze的情况下,可以找到解释:https://bitbucket.org/anthony_tuininga/cx_freeze/issues/127/collectionssys-error

Unfortunately Python Package Index does not provide a version of cx_freeze that includes the necessary changes. A new version of cx_Freeze can be installed after Microsoft Visual C++ Compiler for Python 2.7 has been installed. It is possible to install python packages from other locations than Python Package Index with pip command, in this case

不幸的是,Python Package Index不提供包含必要更改的cx_freeze版本。安装Microsoft Visual C ++ Compiler for Python 2.7后,可以安装新版本的cx_Freeze。在这种情况下,可以使用pip命令从Python Package Index以外的其他位置安装python包

pip install --upgrade https://bitbucket.org/anthony_tuininga/cx_freeze/get/tip.zip

This needs to be done in Anaconda prompt that should be found from the Start menu. command prompt suffices if the PATH has been modified during the installation of Anaconda.

这需要在Anaconda提示符中完成,该提示应该从“开始”菜单中找到。如果在安装Anaconda期间修改了PATH,则命令提示符就足够了。