pyInstaller 多次加载脚本 [英] pyInstaller loads script multiple times

查看:43
本文介绍了pyInstaller 多次加载脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经设法让 pyinstaller 或多或少地正确运行,只是它打开了太多窗口.这是 pygame 项目,它每隔一秒左右就会重新加载整个内容.几秒钟后,我的电脑充满了游戏窗口,一切都停止了.

I've managed to get pyinstaller to run more or less correctly now, except that it opens too many windows. It's pygame project, and it keeps loading the entire thing over again every second or so. My PC fills with game windows after a few seconds and everything grinds to a halt.

从命令行运行它,我只能看到打印输出说应用程序开始在命令行窗口中一遍又一遍地粘贴.据我所知,应用程序没有关闭或退出,只是越来越多地产生.

Running it from commandline, I can just see print outs saying the app is starting pasted over and over again in commandline window. As far as I can tell the Apps aren't closing or exiting, just spawning more and more.

我调用来启动pyinstaller的命令是这样的:

The command I call to start pyinstaller is this:

pyinstaller --onedir main_local.py

主文件如下所示:

# Library imports
import pygame

# Project imports
from multiroidal_client    import MultiroidalClient
from settings              import *
import time

# Main program function
def main():

    # Initialise game class
    game = MultiroidalClient(SCREEN_SIZE, ('127.0.0.1', 8080))

    # Start game loop
    game.main_game_loop()


# Execute main function
if __name__ == '__main__':
    main()

我尝试注释掉 if __ name __ ... 位,看看是否出于某种原因它正在执行主函数并每次意外调用重复项或其他什么.如您所料,当注释掉时,exec 什么也不做.

I tried commenting out the if __ name __ ... bit to see if for some reason it was executing the main function and calling a duplicate each time by accident or something. When commented out the exec does nothing, as you might expect.

有什么想法吗?我没有包含更多代码,因为有很多代码,而且我不确定哪些部分是相关的.如果您需要查看其他内容,请告诉我.

Any ideas? I've not included any more code because there is loads of it, and I'm not sure which parts are relevant. Let me know if you need to see anything else.

干杯

我在 game.main_game_loop() 之后添加了一个快速打印,只是为了检查脚本是否跳出无限游戏循环.没有这样的运气.它正在加载脚本的并行实例,所有脚本都同时运行.

I added a quick print after the game.main_game_loop() just to check if the script jumped out of the infinite game loop. No such luck. It's loading a parallel instances of the scripts, all running simultaneously.

我查看了 pyinstaller 文档并设法进行了更多调试.不确定它是否相关,但它在这里.这一屏对话只是一遍遍地重印.我还尝试了 --noupx 选项,它有时显然有帮助 - 没有这样的运气.

I looked through the pyinstaller docs and managed to get some more debug out. Not sure if it's relevant or not, but here it is. This screen of dialog just keeps reprinting over and over again. I also tried the --noupx option which can sometimes help apparently - no such luck.

推荐答案

多处理!

我尝试了一些更简单的 Python 程序,它们都打包得很好.所以我的方法不是问题,而是代码.再想一想,在项目中打包代码肯定是有些异常或者困难.嗯,也许是线程?

I tried some simpler python programs, and they packaged up all fine. So it wasn't a problem with my method it was the code. Thinking about it again, it must be some unusual or difficult to package code in the project. Hmmm, maybe threading?

我有几个并行运行的线程,它们是多处理线程.在谷歌搜索之后,我发现了这个 magic fix.

I've got a couple of threads running in parallel, and they are multiprocessing threads. After googling about I found this magic fix.

您只需在 if __name__ == "__main__" 之后和调用 main()<之前直接粘贴这一行 multiprocessing.freeze_support()/code> 函数.

You simply stick this line, multiprocessing.freeze_support(), straight after if __name__ == "__main__" and before you call the main() function.

if __name__ == '__main__':

    # Pyinstaller fix
    multiprocessing.freeze_support()

    main()

看起来开发多处理模块的人必须包含一个 hack 以允许冻结打包(py2exe、pyinstaller 等).看起来很奇怪,它没有更顺利地包含在内.如果您保留那个 freeze_support() 调用,即使您不是从打包代码而不是标准 python 文件运行,它仍然可以工作.

It looks like the guys who develop the multiprocessing module had to include a hack to allow freeze packaging (py2exe, pyinstaller etc). Seems weird it isn't included a bit more smoothly. If you leave that freeze_support() call in, it still works even when you aren't running from packaged code as opposed to standard python files.

无论如何,如果您冻结/打包代码并且它似乎一遍又一遍地执行自己,请检查您是否使用了多处理.一个简单的谷歌multiprocessing pyinstaller"给出了答案.

Anyway, if you are freezing/pacakaging code up and it seems to execute itself over and over, check if you are use multiprocessing. A simple google "multiprocessing pyinstaller" gave the answer.

仅供参考,这仅适用于 --onedir 模式,--onefile 模式是另一回事,Windows 不支持.你必须深入挖掘才能解决这个问题.

FYI this only works on --onedir mode, --onefile mode is a different story and not supported on windows. You'll have to dig around more to solve that one.

这篇关于pyInstaller 多次加载脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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