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

查看:142
本文介绍了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天全站免登陆