鱼C论坛

 找回密码
 立即注册
查看: 1095|回复: 2

pyinstaller 打包出现错误

[复制链接]
发表于 2019-7-26 17:16:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
最近做了一个测试打字速度和反应力的小游戏,代码如下:

main.py
  1. # !usr/bin/env python
  2. # -*- coding:utf-8 -*-

  3. from time import time
  4. import pygame
  5. import color
  6. import button
  7. import random
  8. import pickle
  9. from os.path import isfile


  10. class Game:
  11.     def __init__(self):
  12.         # 初始化pygame的模块
  13.         pygame.init()
  14.         pygame.font.init()
  15.         pygame.display.init()
  16.         # 窗口尺寸
  17.         self.screen_size = self.screen_width, self.screen_height = 420, 550
  18.         self.screen: pygame.Surface = pygame.display.set_mode(self.screen_size)
  19.         pygame.display.set_icon(pygame.image.load("icon.png").convert_alpha())
  20.         self.started = False
  21.         self.clock = pygame.time.Clock()
  22.         self.numdisplay_once = True
  23.         self.second = 1.2
  24.         self.time_complete = False
  25.         self.score = []
  26.         self.complete_timer_once = True
  27.         self.second_level_once = True
  28.         self.third_level_once = True
  29.         self.fourth_level_once = True
  30.         self.blit_raise_level_text = True

  31.         # 事件id
  32.         self.numchange_timer = pygame.USEREVENT
  33.         self.complete_timer = pygame.USEREVENT + 1
  34.         self.raise_level_timer = pygame.USEREVENT + 2
  35.         if not isfile("result.pkl"):
  36.             open("result.pkl", "wb").close()

  37.     def Start(self):
  38.         while True:
  39.             self.start_button = button.StartButton()
  40.             for event in pygame.event.get():
  41.                 if event.type == pygame.QUIT:
  42.                     exit()
  43.                 if event.type == pygame.MOUSEBUTTONUP:
  44.                     if not self.started:
  45.                         if 135 <= pygame.mouse.get_pos()[0] <= 285 and 300 <= pygame.mouse.get_pos()[1] <= 360:
  46.                             self.started = True
  47.                 if event.type == self.numchange_timer:
  48.                     if self.second <= 0.01:
  49.                         self.time_complete = True
  50.                     else:
  51.                         self.second -= 0.01
  52.                 if event.type == self.complete_timer:
  53.                     exit()
  54.                 if event.type == pygame.KEYUP:
  55.                     if event.key == pygame.K_ESCAPE:
  56.                         exit()
  57.                     if event.key == ord(self.letter) + 32:
  58.                         if self.started and (not self.time_complete):
  59.                             self.numdisplay_once = True
  60.                             self.stop_time = time()
  61.                             self.score.append(self.stop_time - self.start_time)
  62.                             self.ShowLetter()

  63.             self.screen.fill(color.GREEN)
  64.             if not self.started:
  65.                 self.StartButton()
  66.                 self.screen.blit(self.start_button, (135, 300))
  67.             else:
  68.                 if not self.time_complete:
  69.                     for index, each in enumerate(self.Text()):
  70.                         if index == 0:
  71.                             self.screen.blit(each, (10, 10))
  72.                         else:
  73.                             self.screen.blit(each, (15, 40))
  74.                     self.screen.blit(self.Time(), (190, 80))
  75.                     self.ShowLetter()
  76.                     self.ShowScore()
  77.                 else:
  78.                     if self.complete_timer_once:
  79.                         pygame.time.set_timer(self.complete_timer, 3000)
  80.                     self.complete_timer_once = False
  81.                     font_surface: pygame.Surface = pygame.font.SysFont("SimHei", 23).render(
  82.                         "时间不够啦!", False, color.WHITE)
  83.                     self.screen.blit(
  84.                         font_surface,
  85.                         ((self.screen_width - font_surface.get_width()) / 2,
  86.                          (self.screen_height - font_surface.get_height()) / 2)
  87.                     )
  88.                     if self.score:
  89.                         font_surface: pygame.Surface = pygame.font.SysFont("SimHei", 23).render(
  90.                             "你的成绩是:平均{:.5f}秒/字符。".format(sum(self.score) / len(self.score)), False, color.WHITE
  91.                         )
  92.                     else:
  93.                         font_surface: pygame.Surface = pygame.font.SysFont("SimHei", 23).render(
  94.                             "你的成绩是:平均1.2秒/字符。", False, color.WHITE
  95.                         )
  96.                     self.screen.blit(
  97.                         font_surface,
  98.                         ((self.screen_width - font_surface.get_width()) / 2,
  99.                          (self.screen_height - font_surface.get_height()) / 2 + 45)
  100.                     )
  101.                     self.Save()
  102.             self.clock.tick(60)
  103.             pygame.display.flip()

  104.     def StartButton(self):
  105.         if 135 <= pygame.mouse.get_pos()[0] <= 285 and 300 <= pygame.mouse.get_pos()[1] <= 360:
  106.             self.start_button = button.StartButtonHover(self.start_button)
  107.         else:
  108.             self.start_button = button.StartButton()

  109.     @staticmethod
  110.     def Text() -> list:
  111.         font = pygame.font.SysFont("SimHei", 20)
  112.         font_surface_list = [font.render("游戏开始啦!屏幕出现了哪个字母,你就要按", False, color.WHITE),
  113.                              font.render("下它!难度会不断增加哦!", False, color.WHITE)]
  114.         return font_surface_list

  115.     def Time(self) -> pygame.Surface:
  116.         if self.numdisplay_once:
  117.             self.start_time = time()
  118.             self.second = self.RaiseLevel()
  119.             pygame.time.set_timer(self.numchange_timer, 10)
  120.             self.letter = chr(random.randint(65, 90))
  121.         self.numdisplay_once = False
  122.         font_surface = pygame.font.SysFont("SimHei", 115).render("{:.2f}".format(self.second), False, color.RED)
  123.         return font_surface

  124.     def ShowLetter(self):
  125.         self.screen.blit(
  126.             pygame.font.SysFont("SimHeu", 180).render(self.letter, False, color.BLUE),
  127.             (150, 260)
  128.         )

  129.     def ShowScore(self):
  130.         self.stop_time = time()
  131.         if self.score:
  132.             score = (sum(self.score) + (self.stop_time - self.start_time)) / (len(self.score) + 1)
  133.         else:
  134.             score = self.stop_time - self.start_time
  135.         self.screen.blit(pygame.font.SysFont("SimHei", 25).render(
  136.             "你目前的成绩是:" + "{:.5f}".format(score) + "秒/字符", False, color.WHITE
  137.         ), (20, 200))

  138.     def Save(self):
  139.         if self.score:
  140.             result = float("{:.5f}".format(sum(self.score) / len(self.score)))
  141.         else:
  142.             result = 1.2000
  143.         try:
  144.             highest_score = pickle.load(open("result.pkl", "rb"))
  145.         except EOFError:
  146.             font_surface: pygame.Surface = pygame.font.SysFont("SimHei", 23).render(
  147.                 "创造新纪录!", False, color.WHITE
  148.             )
  149.             self.screen.blit(font_surface,
  150.                              ((self.screen_width - font_surface.get_width()) / 2,
  151.                               (self.screen_height - font_surface.get_height()) / 2 + 100))
  152.             open("result.pkl", "w").close()
  153.             pickle.dump(result, open("result.pkl", "wb"))
  154.         else:
  155.             if highest_score >= result:
  156.                 font_surface: pygame.Surface = pygame.font.SysFont("SimHei", 23).render(
  157.                     "创造新纪录!", False, color.WHITE
  158.                 )
  159.                 self.screen.blit(font_surface,
  160.                                  ((self.screen_width - font_surface.get_width()) / 2,
  161.                                   (self.screen_height - font_surface.get_height()) / 2 + 100))
  162.                 open("result.pkl", "w").close()
  163.                 pickle.dump(result, open("result.pkl", "wb"))
  164.             else:
  165.                 font_surface: pygame.Surface = pygame.font.SysFont("SimHei", 23).render(
  166.                     "你的最高纪录是:平均" + str(highest_score) + "秒/字符。", False, color.WHITE
  167.                 )
  168.                 self.screen.blit(font_surface,
  169.                                  ((self.screen_width - font_surface.get_width()) / 2,
  170.                                   (self.screen_height - font_surface.get_height()) / 2 + 100))

  171.     def RaiseLevel(self):
  172.         if len(self.score) >= 75:
  173.             return 0.5
  174.         elif len(self.score) >= 38:
  175.             return 0.8
  176.         elif len(self.score) >= 15:
  177.             return 1
  178.         else:
  179.             return 1.2


  180. if __name__ == '__main__':
  181.     game = Game()
  182.     game.Start()
复制代码


button.py
  1. # !usr/bin/env python
  2. # -*- coding:utf-8 -*-

  3. import pygame
  4. import color


  5. def StartButton() -> pygame.Surface:
  6.     font = pygame.font.SysFont("SimHei", 33)
  7.     font_surface = font.render('开始游戏', False, color.BLACKRED)
  8.     button_surface = pygame.Surface((150, 60))
  9.     button_surface.fill(color.GRAY)
  10.     button_surface.blit(font_surface, (10, 10))

  11.     return button_surface


  12. def StartButtonHover(start_button: pygame.Surface) -> pygame.Surface:
  13.     st = start_button
  14.     st.fill(color.BLACK)
  15.     font = pygame.font.SysFont("SimHei", 33)
  16.     font_surface = font.render('开始游戏', False, color.BLACKRED)
  17.     st.blit(font_surface, (10, 10))

  18.     return st
复制代码


color.py
  1. # !usr/bin/env python
  2. # -*- coding:utf-8 -*-

  3. GREEN = 0, 255, 0
  4. GRAY = 55, 50, 45
  5. BLACK = 0, 0, 0
  6. BLACKRED = 160, 0, 0
  7. WHITE = 255, 255, 255
  8. RED = 255, 0, 0
  9. BLUE = 0, 0, 255
复制代码


运行也没问题

pyinstaller 打包也没问题
  1. pyinstaller -F Python\项目\TestTypingSpeed\main.py
  2. 265 INFO: PyInstaller: 3.5
  3. 280 INFO: Python: 3.7.4
  4. 280 INFO: Platform: Windows-7-6.1.7601-SP1
  5. 280 INFO: wrote F:\编程\main.spec
  6. 280 INFO: UPX is not available.
  7. 296 INFO: Extending PYTHONPATH with paths
  8. ['F:\\编程\\Python\\项目\\TestTypingSpeed', 'F:\\编程']
  9. 296 INFO: checking Analysis
  10. 296 INFO: Building Analysis because Analysis-00.toc is non existent
  11. 296 INFO: Initializing module dependency graph...
  12. 312 INFO: Initializing module graph hooks...
  13. 312 INFO: Analyzing base_library.zip ...
  14. 8793 INFO: running Analysis Analysis-00.toc
  15. 8856 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  16.   required by f:\python 3.7.4\python.exe
  17. 10915 INFO: Caching module hooks...
  18. 10931 INFO: Analyzing F:\编程\Python\项目\TestTypingSpeed\main.py
  19. 11087 INFO: Loading module hooks...
  20. 11087 INFO: Loading module hook "hook-encodings.py"...
  21. 11258 INFO: Loading module hook "hook-pydoc.py"...
  22. 11258 INFO: Loading module hook "hook-xml.py"...
  23. 11945 INFO: Looking for ctypes DLLs
  24. 11945 INFO: Analyzing run-time hooks ...
  25. 11960 INFO: Looking for dynamic libraries
  26. 12475 INFO: Looking for eggs
  27. 12475 INFO: Using Python library f:\python 3.7.4\python37.dll
  28. 12475 INFO: Found binding redirects:
  29. []
  30. 12491 INFO: Warnings written to F:\编程\build\main\warn-main.txt
  31. 12600 INFO: Graph cross-reference written to F:\编程\build\main\xref-main.html
  32. 12647 INFO: checking PYZ
  33. 12647 INFO: Building PYZ because PYZ-00.toc is non existent
  34. 12647 INFO: Building PYZ (ZlibArchive) F:\编程\build\main\PYZ-00.pyz
  35. 13978 INFO: Building PYZ (ZlibArchive) F:\编程\build\main\PYZ-00.pyz completed successfully.
  36. 13994 INFO: checking PKG
  37. 13994 INFO: Building PKG because PKG-00.toc is non existent
  38. 13994 INFO: Building PKG (CArchive) PKG-00.pkg
  39. 18424 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
  40. 18471 INFO: Bootloader f:\python 3.7.4\lib\site-packages\PyInstaller\bootloader\Windows-32bit\run.exe
  41. 18471 INFO: checking EXE
  42. 18471 INFO: Building EXE because EXE-00.toc is non existent
  43. 18471 INFO: Building EXE from EXE-00.toc
  44. 18471 INFO: Appending archive to EXE F:\编程\dist\main.exe
  45. 18486 INFO: Building EXE from EXE-00.toc completed successfully.
复制代码


但用 pyinstaller 打包之后,惨了……
  1. Traceback (most recent call last):
  2.   File "main.py", line 5, in <module>
  3. ModuleNotFoundError: No module named 'pygame'
  4. [3288] Failed to execute script main
复制代码


这是怎么回事呀,我明明安装了pygame的……

求各位大神解答!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-7-27 18:06:33 | 显示全部楼层
为什么没人啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-7-29 10:52:52 | 显示全部楼层
Request 发表于 2019-7-29 10:51
本地装了pygame 模块吗

装了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-17 04:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表