python - 如何修复外星入侵文件中 pygame 的 ImportError“库未加载”?
问题描述
这是我的代码:
import sys
import pygame
class AlienInvasion:
"""Overall class to manage game assets and behavior"""
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.screen = pygame.display.set_mode((1200,800))
pygame.display.set_caption("Alien Invasion")
def run_game(self):
"""Start the main loop for the game"""
while True:
# Watch for keyboard and mouse events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Make the most recently drawn screen visible
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance and run the game
ai = AlienInvasion()
ai.run_game()
我已经通过我的 Mac 上的终端安装了 pygame。当我尝试运行我的代码时,它给了我下面的错误。我期待一个空白屏幕。我试过pip uninstall pygame
and pip install pygame
,但没有奏效。如果有人可以提供帮助,那就太好了。
Traceback (most recent call last): File "/Users/mycomputer/Desktop/python_work/alien_invasion.py", line 3, in <module> import pygame File "/Users/mycomputer/Library/Python/3.9/lib/python/site-packages/pygame/__init__.py", line 81, in <module> from pygame.base import * # pylint: disable=wildcard-import; lgtm[py/polluting-import]
ImportError: dlopen(/Users/mycomputer/Library/Python/3.9/lib/python/site-packages/pygame/base.cpython-39-darwin.so, 2): Library not loaded: /System/Library/Frameworks/Metal.framework/Versions/A/Metal Referenced from: /Users/mycomputer/Library/Python/3.9/lib/python/site-packages/pygame/base.cpython-39-darwin.so Reason: image not found
解决方案
你确实卸载了 pygamepip
并brew
安装了 pygamebrew
pip uninstall pygame
brew uninstall pygame
brew install pygame
推荐阅读
- javascript - 如何在React中一个一个地循环遍历div标签内的数组元素
- angular - 在Angular中获取“类型'MediaDevices'上不存在属性'getDisplayMedia'”
- c# - HttpClient 返回不完整的响应内容
- python - Bluez 5.23 -> Bluez 5.43 - 蓝牙已连接但没有声音
- python - AWS Lambda 函数如何获取并行函数的结果?
- css - 即使使用 flex-start 或 spece,Flex-box 元素也会拉伸
- mysql - mySQL比较年和周的两个日期
- asp.net-core - .net core SPA Angular中的脚手架管理员CRUD
- python - 从 pathlib 源文件路径导入模块
- javascript - 如果解决时间超过 5 秒,是否可以拒绝 Promise.allSettled 中的每个 Promise?