【问题标题】:Generate an executable from python project从 python 项目生成可执行文件
【发布时间】:2021-04-16 14:40:55
【问题描述】:

我需要从一个包含多个文件夹和文件的 python 项目生成一个可执行文件。 我尝试使用库 cx_Freeze,但仅适用于单个文件项目。

你能告诉我怎么做吗?

【问题讨论】:

标签: python exec executable cx-freeze generate


【解决方案1】:

在您的“主”python 文件上运行 pyinstaller 应该可以工作,因为 PyInstaller 会自动导入您使用的任何依赖项(例如其他 python 文件)。

【讨论】:

    【解决方案2】:

    使用 pyinstaller。赶紧跑 pip install pyinstaller 然后用shell打开文件所在的文件夹并运行pyinstaller --onefile FILE.py 其中file是运行exe时应该运行的python文件的名称

    【讨论】:

    • 我想我试过 pyinstaller 一次但它没有工作,因为它不在 PATH 中。此外,pyinstaller 看起来更有限。 cx_Freeze 可以在 PATH 中没有 Python 脚本的情况下工作,因为您可以制作构建脚本并导入 cx_Freeze 然后执行 python build.py build
    • 感谢它几乎可以工作。使用我的 code.exe,我的文件之间的链接不起作用。我做了一个界面,当我按下一个按钮打开另一个界面时,它没有找到该文件(它说是因为路径),但是当我在终端中测试我的 code.py 时它工作了
    • 我希望这里有新的东西。 pyinstaller 非常适合单个文件。我的项目具有通常的设置:代码文件夹、.venv 文件夹、大量文件等。它是一个烧瓶服务器,当我运行可执行文件时它报告ModuleNotFoundError: No module named 'flask_cors'。不用说,非编译版本可以正常工作。这里是否有任何详细信息假设,例如PYTHONPATH,是否激活 venv 或运行此程序时的位置?更好的问题:你真的尝试过这个吗?
    【解决方案3】:

    这是你必须做的:

    使用如下指令创建一个 build.py 文件:

    import cx_Freeze
    
    executables = [cx_Freeze.Executable("main.py")] # The main script
    
    cx_Freeze.setup(
        name="Platform Designer", # The name of the exe
        options={"build_exe": {
            "packages": ["pygame"], # Packages used inside your exe
            "include_files": ["data", "instructions.md"], # Files in the exe's directory
            "bin_path_includes": ["__custom_modules__"]}}, # Files to include inside the exe
        executables=executables
    )
    

    在命令提示符下运行:

    • python build.py build 构建一个exe
    • python build.py bdist_msi 构建一个 msi 安装程序

    确保在更新 exe 或 msi 时删除 builddist 文件夹。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-23
      • 2020-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      相关资源
      最近更新 更多