cocos2d python打包图片文件应放哪
cocos2d python文件图片放在py文件同目录下,能正常运行。但用pyinstaller打包后的exe文件不能运行,
错误显示如下:
Traceback (most recent call last):
File "pyglet\resource.py", line 453, in file
KeyError: 'ball111.png'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "cocosmoveby01.py", line 17, in <module>
File "cocosmoveby01.py", line 8, in __init__
File "cocos\sprite.py", line 120, in __init__
File "pyglet\resource.py", line 575, in image
File "pyglet\resource.py", line 500, in _alloc_image
File "pyglet\resource.py", line 456, in file
pyglet.resource.ResourceNotFoundException: Resource "ball111.png" was not found on the path.Ensure that the filename has the correct captialisation.
即使将图片文件放到exe同目录也不行。而纯代码的py文件打包后的exe文件能正常运行。
"pyglet\resource.py"这个文件看不明白,它到底是如何规定的呢?
这个名为"cocosmoveby01.py"的文件如下:
import cocos
from cocos.sprite import Sprite
class PhysicsDemo(cocos.layer.Layer):
def __init__(self):
super(PhysicsDemo, self).__init__()
sprite = Sprite('ball111.png')
sprite.position = 200, 240
self.add(sprite)
sprite.velocity = (100, 100)
sprite.do(cocos.actions.MoveBy((300, 0), 2) + cocos.actions.Reverse(cocos.actions.MoveBy((300, 0), 2)))
if __name__ == "__main__":
cocos.director.director.init()
physics_layer = PhysicsDemo()
main_scene = cocos.scene.Scene(physics_layer)
cocos.director.director.run(main_scene)
在使用 PyInstaller 打包包含资源文件(如图片)的 Python 项目时,需要确保这些资源文件在运行时也能被找到。问题的关键在于,当 PyInstaller 打包时,它只会包含 Python 源代码文件和经过指定处理的资源文件。因此,我们需要让 PyInstaller 明确地知道需要将哪些非 Python 文件包括在内。
您可以通过以下步骤解决这个问题:
1. **使用`--add-data`选项**: 在打包时使用 PyInstaller 的 `--add-data` 选项来指定额外需要包含的文件及其位置。
2. **更新代码来访问资源路径**: 为了确保在运行时能够定位到这些资源,通常需要更新代码来正确地访问这些资源文件。
### 详细步骤
1. **修改打包命令**:
如果 `ball111.png` 位于同一目录,您可以在打包命令中指定它:
pyinstaller --onefile --add-data "ball111.png;." cocosmoveby01.py
这里的 `.` 表示将文件打包到和 `.exe` 相同的资源目录。
2. **修改代码访问资源**:
因为打包后的路径可能会有所变化,可以在代码中添加一个获取资源正确路径的步骤。例如:
import os
import sys
import cocos
from cocos.sprite import Sprite
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
class PhysicsDemo(cocos.layer.Layer):
def __init__(self):
super(PhysicsDemo, self).__init__()
image_path = resource_path('ball111.png')
sprite = Sprite(image_path)
sprite.position = 200, 240
self.add(sprite)
sprite.velocity = (100, 100)
sprite.do(cocos.actions.MoveBy((300, 0), 2) + cocos.actions.Reverse(cocos.actions.MoveBy((300, 0), 2)))
if __name__ == "__main__":
cocos.director.director.init()
physics_layer = PhysicsDemo()
main_scene = cocos.scene.Scene(physics_layer)
cocos.director.director.run(main_scene)
这一方法可以确保在开发环境和打包后的运行环境中都能正确定位到资源文件。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 pyinstaller --onefile --add-data "ball111.png;." cocosmoveby01.py
按以上打包的exe文件一闪而过。
后面的代码不能运行,打包后的文件也不行。
显示如下:
Traceback (most recent call last):
File "F:\python3.8x64\lib\site-packages\pyglet\resource.py", line 453, in file
location = self._index
KeyError: 'D:\\bcpyqt\\game\\ball111.png'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/bcpyqt/game/cocosmoveby01fish.py", line 30, in <module>
physics_layer = PhysicsDemo()
File "D:/bcpyqt/game/cocosmoveby01fish.py", line 21, in __init__
sprite = Sprite(image_path)
File "F:\python3.8x64\lib\site-packages\cocos\sprite.py", line 120, in __init__
image = pyglet.resource.image(image)
File "F:\python3.8x64\lib\site-packages\pyglet\resource.py", line 575, in image
identity = self._cached_images = self._alloc_image(name, atlas, border)
File "F:\python3.8x64\lib\site-packages\pyglet\resource.py", line 500, in _alloc_image
file = self.file(name)
File "F:\python3.8x64\lib\site-packages\pyglet\resource.py", line 456, in file
raise ResourceNotFoundException(name)
pyglet.resource.ResourceNotFoundException: Resource "D:\bcpyqt\game\ball111.png" was not found on the path.Ensure that the filename has the correct captialisation.
ydwb 发表于 2024-11-14 14:32
pyinstaller --onefile --add-data "ball111.png;." c ...
在使用 PyInstaller 打包时,可能出现路径问题或资源访问问题。为了确保代码和资源能够正确运行和访问,请尝试以下步骤:
1. **调整 PyInstaller 的 `--add-data` 语法**:
在 Windows 上,路径分割符之间应该使用分号(`;`),具体指的是打包命令中的 `--add-data` 选项。尝试以下格式:
pyinstaller --onefile --add-data "ball111.png;ball111.png" cocosmoveby01.py
2. **检查资源路径**:
您的代码中,将资源路径处理函数调整成如下所示,确保无论是打包还是开发模式下,都能正确获取路径:
import os
import sys
import cocos
from cocos.sprite import Sprite
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except AttributeError:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
class PhysicsDemo(cocos.layer.Layer):
def __init__(self):
super(PhysicsDemo, self).__init__()
image_path = resource_path('ball111.png')
sprite = Sprite(image_path)
sprite.position = 200, 240
self.add(sprite)
sprite.velocity = (100, 100)
sprite.do(cocos.actions.MoveBy((300, 0), 2) + cocos.actions.Reverse(cocos.actions.MoveBy((300, 0), 2)))
if __name__ == "__main__":
cocos.director.director.init()
physics_layer = PhysicsDemo()
main_scene = cocos.scene.Scene(physics_layer)
cocos.director.director.run(main_scene)
3. **调试打包的程序**:
如果你的程序闪过又关闭,你可以在代码末尾加上输入等待,比如 `input("Press Enter to exit...")`,这样程序执行完毕后会等待输入以便你查看错误信息。
通过这些步骤,您应该能够确保资源能在执行时被正确访问。注意在 Windows 中确保路径分隔符和命令行语法正确。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 上述方法不行,换一种方法,用auto-py-to-exe打包成功。
先在cmd下安装 pip install auto-py-to-exe,安装完成后输入:auto-py-to-exe,来启动 auto-py-to-exe 程序。
出现打包界面后,在Additional File栏内拖入图片。可选择生成一个文件或一个文件夹的方式。生成的exe文件成功运行。
ydwb 发表于 2024-11-15 13:58
上述方法不行,换一种方法,用auto-py-to-exe打包成功 ...
很高兴您找到了使用 `auto-py-to-exe` 成功打包您的项目的方法。`auto-py-to-exe` 提供了一个更直观的界面,方便地将额外的文件(如图片、配置文件等)一起打包,这使得许多打包问题得到了简化。
使用 `auto-py-to-exe` 的步骤简要总结如下:
1. **安装**:通过 `pip install auto-py-to-exe` 安装工具。
2. **启动**:在命令行输入 `auto-py-to-exe` 启动控制台界面。
3. **配置打包**:
- 在脚本路径中选择您的 Python 文件。
- 在 "Additional Files" 栏中拖入或选择需要包含的资源文件(如图片)。
- 选择是否打包成一个文件或保持为文件夹形式。
4. **生成并运行**:点击转换按钮生成 `exe` 文件,并在生成后的路径下运行它。
感谢您的分享,希望您的解决方案对其他遇到类似问题的人也有所帮助!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]