Python - 创建一个运行编写的代码的 EXE,而不是编译时的代码 [英] Python - create an EXE that runs code as written, not as it was when compiled

查看:20
本文介绍了Python - 创建一个运行编写的代码的 EXE,而不是编译时的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个设计为模块化的 pygame 程序.我正在用文件 main.py 的 pygame2exe 构建一个 exe,它基本上只是导入真正的主游戏并运行它.我希望的是一种可以从 EXE 执行 Python 脚本的启动器,而不是包含所有不可变文件的单个程序.

I'm making a pygame program that is designed to be modular. I am building an exe with pygame2exe of the file main.py, which basically just imports the real main game and runs it. What I'm hoping for is a sort of launcher that will execute Python scripts from an EXE, rather than a single program containing all immutable files.

解决这个问题的最佳方法是什么?我曾尝试使用 imp 在运行时动态导入所有模块,而不是隐式导入它们,但这似乎破坏了对象继承.

What is the best way to go about this? I've tried using imp to dynamically import all modules at runtime instead of implicitly importing them, but that seems to break object inheritance.

推荐答案

经过一些实验,我找到了解决方案.

After some experiments I've found a solution.

  1. 在应用程序的主文件夹中创建一个单独的文件夹 source.这里将放置源文件.还将文件 __init__.py 放到文件夹中.让我们将主文件命名为 main_module.py.

  1. Create a separate folder source in the main folder of the application. Here will be placed source files. Also place file __init__.py to the folder. Lets name a main file like main_module.py.

将其所有内容作为数据文件添加到 py2exe 配置 setup.py.现在编译程序后,这些文件将放在dist文件夹中.

Add all of its contents as a data files to the py2exe configuration setup.py. Now after compiling the program, these files will be placed in the dist folder.

data_files += [('source', glob('source/*.py'),)]
setup(
      data_files=data_files,
      ....  # other options
     windows=[
          {
            "script": "launcher.py",
            "icon_resources": [(0, "resources/favicon.ico")]
          }
     )

  • 制作 launcher.py - 它的任务是导入所有系统和所需的库,如 pygame、pyqt 等.然后运行你的程序:

  • Make launcher.py - it's task is to import all system and required libraries like pygame, pyqt and so on. Then run you program:

    import sys, time, os, hashlib, atexit  # std modules
    import PyQt5, ...   # foreign libraries
    sys.path.insert(0, 'source')
    exec('import main_module')
    

  • 现在 main_module.py 将被导入,如果它导入你的模块,它们也会在层次结构中的位置被导入.例如 main_module.py 的头部可以是这样的:

  • Now main_module.py will be imported, if it imports your modules, they will be imported too in their places in hierarchy. For example head of the main_module.py can be like this:

     import user_tweaks
     from user_data import parser
    

    这些文件user_tweaks.pyuser_data.py 应位于source 文件夹中相对于main_module.py 的适当路径.

    These files user_tweaks.py and user_data.py should be located in source folder at appropriate paths relative to main_module.py.

    您可以在不重新编译程序本身的情况下更改source文件夹的内容.任何时候程序运行它都会使用 source 的新内容.

    You may change contents of source folder without recompilation program itself. Any time program runs it uses fresh contents of source.

    结果你有一个应用程序文件夹:

    As a result you have an application folder with:

    • 单独的启动器 - 简单的 .exe 文件
    • 所有必需的模块
    • 您的应用程序及其所有模块.

    这篇关于Python - 创建一个运行编写的代码的 EXE,而不是编译时的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆