【问题标题】:RuntimeError for SDL2 after installing pySDL2安装 pySDL2 后 SDL2 的 RuntimeError
【发布时间】:2013-08-12 06:15:04
【问题描述】:

我通过以下方式安装了 pySDL2 0.4.1:

下载源码包, 输入python setup.py install。 然后我尝试运行copypaste到PyDev eclipse“The Pong Game: Getting Started”教程代码示例:

导入操作系统,系统

try:
    os.environ["PYSDL2_DLL_PATH"] = "/home/me/workspace/Pong/third-party"
    print(os.getenv("PYSDL2_DLL_PATH"))
    from sdl2 import events, SDL_QUIT
    import sdl2.ext as sdl2ext
except ImportError:
    import traceback
    traceback.print_exc()
    sys.exit(1)

def run():
    sdl2ext.init()
    window = sdl2ext.Window("The Pong Game", size=(800, 600))
    window.show()
    running = True
    while running:
        events = sdl2ext.get_events()
        for event in events:
            if event.type == SDL_QUIT:
                running = False
                break
        window.refresh()
    return 0

if __name__ == "__main__":
    sys.exit(run())

我收到以下错误:

Traceback (most recent call last):
  File "/home/me/workspace/Pong/Main.py", line 11, in <module>
    from sdl2 import *
  File "/usr/local/lib/python3.3/dist-packages/sdl2/__init__.py", line 2, in <module>
    from .dll import get_dll_file, _bind
  File "/usr/local/lib/python3.3/dist-packages/sdl2/dll.py", line 90, in <module>
    dll = _DLL("SDL2", ["SDL2", "SDL2-2.0"], os.getenv("PYSDL2_DLL_PATH"))
  File "/usr/local/lib/python3.3/dist-packages/sdl2/dll.py", line 51, in __init__
    raise RuntimeError("could not find any library for %s" % libinfo)
RuntimeError: could not find any library for SDL2

我通过 Synaptic 安装了 Pypy 和 libSDL,并且没有在 PyDev - PythonPath 中添加任何外部库。

我做错了什么?

【问题讨论】:

    标签: eclipse ubuntu sdl pydev pysdl2


    【解决方案1】:

    您的 PySDL2 似乎没有找到 SDL2 运行时库。它们在 libsdl download page 上可用(Linux 除外)。

    然后您应该通过设置PYSDL2_DLL_PATH 让 PySDL2 位于库所在的位置,如下所示:

    # Win32 platforms
    set PYSDL2_DLL_PATH=C:\path\to\fancy_project\third_party
    
    # Unix/Posix-alike environments - bourne shells
    export PYSDL2_DLL_PATH=/path/to/fancy_project/third_party
    
    # Unix/Posix-alike environments - C shells
    setenv PYSDL2_DLL_PATH /path/to/fancy_project/third_party
    

    或在 python 脚本中:

    # Win32 Platform path
    os.environ["PYSDL2_DLL_PATH"] = "C:\\path\\to\\fancy_project\\third_party"
    # Unix/Posix-alike environments path
    os.environ["PYSDL2_DLL_PATH"] = "/path/to/fancy_project/third_party"
    

    那样,PySDL2 将始终找到库文件(当然只要路径正确)!这在我看来是更简单的方法。

    使用 SDL2 编码愉快!

    【讨论】:

    • 在我的情况下,通过 Windows 命令行使用“set”设置路径实际上并没有帮助。为了运行任何示例,我每次都需要包含 python 脚本行。我不确定这是否是其他人会遇到的问题,但我想提一下,到目前为止我一直无法成功设置 DLL 路径
    • 'set' 命令是临时的,你必须每次在脚本之前在同一个 shell 中运行它。如果您希望它持续存在,请使用此处所示的“setx”:superuser.com/questions/284342/…
    • 好的,谢谢!实际上我后来发现了这一点,并通过环境变量添加了它。
    【解决方案2】:

    几乎解决了。 Ubuntu 有 SDL1.2,SDL 的主下载页面也指向 SDL1.2。需要通过键入以下内容来安装源包:

    mkdir /opt/sdl2
    cd /opt/sdl2
    hg clone http://hg.libsdl.org/SDL SDL
    cd SDL
    mkdir build && cd build
    ../configure
    make
    sudo make install 
    

    然后从下载 SDL_image-2.x.x.tar.gz http://www.libsdl.org/tmp/SDL_image/

    然后输入:

    ../configure
    make
    sudo make install 
    

    最后输入:

    ldconfig /usr/local/lib

    现在唯一不起作用的是 SDL_QUIT 未被识别。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 2014-03-02
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多