Python sdl2路径无效

时间:2017-07-05 02:35:18

标签: python pysdl2

我正在测试pysdl2的学习飞行教程。 (我也是python的新手) http://pysdl2.readthedocs.io/en/rel_0_9_4/tutorial/index.html

我收到错误,认为这只是一些路径问题。

错误:

guillaume@ubuntu:~/script$ python sdlTest.py 
Traceback (most recent call last):
  File "sdlTest.py", line 4, in <module>
    RESOURCES = sdl2.ext.Resources(__file__, "resources")
  File "/usr/lib/python2.7/dist-packages/sdl2/ext/resources.py", line 139, in __init__
    self.scan(path, subdir, excludepattern)
  File "/usr/lib/python2.7/dist-packages/sdl2/ext/resources.py", line 313, in scan
    raise ValueError("invalid path '%s'" % path)
ValueError: invalid path 'sdlTest.py'

当前代码:

import sys
import sdl2.ext

RESOURCES = sdl2.ext.Resources(__file__, "resources")

我在ubuntu 16.04上 安装了python版本

guillaume@ubuntu:~/script$ python -V
Python 2.7.12

python-sdl2版本:

guillaume@ubuntu:~/script$ dpkg -s python-sdl2
Package: python-sdl2
Status: install ok installed
Priority: optional
Section: python
Installed-Size: 392
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: all
Source: pysdl2
Version: 0.9.3+dfsg2-1
Depends: python:any (<< 2.8), python:any (>= 2.7.5-5~), libsdl2-2.0-0, libsdl2-gfx-1.0-0, libsdl2-image-2.0-0, libsdl2-mixer-2.0-0, libsdl2-ttf-2.0-0
Recommends: python-numpy
Suggests: pysdl2-doc
Description: Python bindings to the SDL2 C-library (Python 2 build)
 PySDL2 is a ctypes based wrapper around the Simple DirectMedia Layer 2 library
 to allow portable low level access to a video framebuffer, audio output, mouse
 and keyboard.
 .
 This module is built for Python version 2.x.
Original-Maintainer: Debian Python Modules Team <python-modules-team@lists.alioth.debian.org>
Homepage: https://bitbucket.org/marcusva/py-sdl2

PYTHONPATH:

guillaume@ubuntu:~/script$ echo $PYTHONPATH
/usr/lib/python2.7/dist-packages/sdl2:
guillaume@ubuntu:~/script$ ls /usr/lib/python2.7/dist-packages/sdl2/
audio.py       filesystem.py       keycode.pyc     render.py     stdinc.pyc
audio.pyc      filesystem.pyc      loadso.py       render.pyc    surface.py
blendmode.py   gamecontroller.py   loadso.pyc      rwops.py      surface.pyc
blendmode.pyc  gamecontroller.pyc  log.py          rwops.pyc     syswm.py
clipboard.py   gesture.py          log.pyc         scancode.py   syswm.pyc
clipboard.pyc  gesture.pyc         messagebox.py   scancode.pyc  timer.py
cpuinfo.py     haptic.py           messagebox.pyc  sdlgfx.py     timer.pyc
cpuinfo.pyc    haptic.pyc          mouse.py        sdlgfx.pyc    touch.py
dll.py         hints.py            mouse.pyc       sdlimage.py   touch.pyc
dll.pyc        hints.pyc           pixels.py       sdlimage.pyc  version.py
endian.py      __init__.py         pixels.pyc      sdlmixer.py   version.pyc
endian.pyc     __init__.pyc        platform.py     sdlmixer.pyc  video.py
error.py       joystick.py         platform.pyc    sdlttf.py     video.pyc
error.pyc      joystick.pyc        power.py        sdlttf.pyc
events.py      keyboard.py         power.pyc       shape.py
events.pyc     keyboard.pyc        rect.py         shape.pyc
ext            keycode.py          rect.pyc        stdinc.py
guillaume@ubuntu:~/script$ 

2 个答案:

答案 0 :(得分:0)

您可能无法在项目目录中包含资源文件夹。

错误有些含糊不清,但这对我来说是固定的。

答案 1 :(得分:0)

&#34; resource&#34; 是指运行sdl2-python脚本的子目录的名称。

importing上的sdl2示例中,他们需要一个名为&#34; resource&#34;的目录。在后续的例子中加载将在sdl2窗口中显示的图像..

  

我们需要资源文件夹中的一些资源,以便我们有一个   测试图像以便稍后在窗口上显示。在你自己   应用程序,您不太可能需要导入它们,   但我们在这里需要它们,所以我们使用sdl2.ext.Resources类   他们可以。

如果您想继续使用该行:

RESOURCES = sdl2.ext.Resources(__file__, "resources")

然后从您使用此行运行脚本的目录中,创建一个名为&#34; resources&#34;的子目录。并运行您的脚本。您将看到不会出现错误消息。您可以使用&#34; resources&#34;以外的名称,只需确保存在具有相同名称的子目录。

更重要的是,要使用sdl2,你只需要以下2行来加载sdl2模块,你就可以了..

import sdl2
import sdl2.ext