2017-07-05 107 views
0

我正在測試學習飛行教程爲pysdl2。 (我也是python的新手) http://pysdl2.readthedocs.io/en/rel_0_9_4/tutorial/index.htmlPython sdl2無效路徑

我得到一個錯誤,認爲這只是一些路徑問題。

錯誤:

[email protected]:~/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版本

[email protected]:~/script$ python -V 
Python 2.7.12 

蟒蛇,SDL2版本:

[email protected]:~/script$ dpkg -s python-sdl2 
Package: python-sdl2 
Status: install ok installed 
Priority: optional 
Section: python 
Installed-Size: 392 
Maintainer: Ubuntu Developers <[email protected]> 
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 <[email protected]> 
Homepage: https://bitbucket.org/marcusva/py-sdl2 

PYTHONPATH:

[email protected]:~/script$ echo $PYTHONPATH 
/usr/lib/python2.7/dist-packages/sdl2: 
[email protected]:~/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 
[email protected]:~/script$ 

回答

0

你可能已經沒有包括在您的項目目錄一個資源文件夾中。

該錯誤有點含糊不清,但這爲我解決了它。

0

「資源」指您從中運行sdl2-python腳本的子目錄的名稱。

在上importing的SDL2例如,他們需要一個叫做「資源」來加載將要顯示他們在他們隨後的例子SDL2窗口圖像目錄..

We need some resources from the resources folder, so that we have a test image around to display on the window later on. In your own applications, it is unlikely that you will ever need to import them, but we need them here, so we use the sdl2.ext.Resources class to have them available.

如果你想繼續使用該行:

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

然後從您正在運行腳本的目錄中,使用此行創建一個名爲「resources」的子目錄並運行腳本。你會看到錯誤信息不會出現。您可以使用「resources」以外的名稱,只要確保具有相同名稱的子目錄存在。

更重要的是,要使用SDL2,你只需要以下2行加載模塊SDL2,你是好去..

import sdl2 
import sdl2.ext