Python 3.5 App to .exe 或复制库到另一台PC

Python 3.5 App to .exe or copy libraries to another PC

我遇到了下一个问题。 在过去的 3 个月里,我在 Python 3.5 中构建了一个应用程序来测试灯具。 对于这个应用程序,我使用了下一个库,"tkinter"、"sys"、"threading"、"os"、"time"、"shutil",它们包含在主程序中Python 3.5 解释器。除了这个系统库,我还使用 "pyserial"、"PyGreSQL"、"PIL"、"win32com" 和 "qrcode"。

该程序将在多台 PC 上使用,因此我需要编译所有代码,以便在安装的每台 PC 上制作一个功能齐全的应用程序。 我尝试使用 PyInstaller,但它对我不起作用,由于我不明白的原因,我无法使其工作。

因此,由于我认为没有更多方法可以将我的 python 3.5 应用程序 "compile" 转换为 exe(所有解决方案仅适用于 Python 2.x ),我觉得解决办法只有两种方法可以解决。

第一个解决办法是,复制Python35文件夹,把我已经安装的所有库都放到另一台电脑上(这是因为我安装的每一个都在安装时出现问题,特别是 pygresql),如果我可以制作一个 "Installer" 覆盖此 Python35 文件夹到新 PC 中,并在运行 .py 脚本的桌面上创建一个 link 图标。 (模拟安装一个APP)

或第二种解决方案,将所有代码重写为 2.x 语法并尝试再次安装 python 2.x 的库,并使所有代码再次正常运行...

你推荐我哪个...或者如果你知道我可以做到这一点...如果你能帮助我,我将不胜感激。

谢谢!

我过去在我的程序中使用过 CX_Freeze,到目前为止,我只在 python 3.4.3 中使用过它,但我看不出有什么理由不能使用它python3.5.

你也可以用pip安装:

pip install CX_Freeze

有关教程,请参阅 here

我现在不在,所以我会在下次有我的电脑时评论我使用的代码。

希望对您有所帮助。

编辑:

这是代码的一个版本(用于批处理):

set PATH=%PATH%;C:\Python34\ python "setup.py" build pause

要使用此代码,您需要创建一个 setup script 指向您的程序,示例如下:

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning. This example will include the os package, and omit the tkinter package
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}

# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(  name = "test",
        version = "0.1",
        description = "My GUI application!",
        options = {"build_exe": build_exe_options},
        executables = [Executable("test.py", base=base)])

编辑2:

虽然上面的代码确实有效,但我使用的代码是:

cxfreeze "%cd%\File.py" --target-dir "%cd%\File\"