简体   繁体   English

Python sdl2无效路径

[英]Python sdl2 Invalid path

I'm testing the learn to fly tutorial for pysdl2. 我正在测试pysdl2的“学习飞行”教程。 (I'm also new to python) http://pysdl2.readthedocs.io/en/rel_0_9_4/tutorial/index.html (我也是python的新手) http://pysdl2.readthedocs.io/en/rel_0_9_4/tutorial/index.html

I'm getting an error, thinking it's just some path problem. 我遇到错误,认为这只是一些路径问题。

Error: 错误:

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'

Current code: 当前代码:

import sys
import sdl2.ext

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

I'm on ubuntu 16.04 installed python version 我在Ubuntu 16.04安装的python版本上

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

python-sdl2 version: 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: 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$ 

You have likely failed to include a resources folder in your project dir. 您可能无法在项目目录中包含资源文件夹。

The error is somewhat ambiguous but this fixed it for me. 该错误有些模棱两可,但这已为我解决了。

"resource" refers to the name of a sub-directory from where you are running your sdl2-python script. “资源”是指从中运行sdl2-python脚本的子目录的名称。

In the sdl2 example on importing , they need a directory called "resource" to load images that will be displayed them in the sdl2 window in their subsequent examples.. 在sdl2 导入示例中,他们需要一个名为“ resource”的目录来加载图像,这些图像将在后续示例的sdl2窗口中显示。

We need some resources from the resources folder, so that we have a test image around to display on the window later on. 我们需要从resources文件夹中获取一些资源,以便我们可以在周围的窗口中看到一个测试图像。 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. 在您自己的应用程序中,您几乎不需要导入它们,但是我们在这里需要它们,因此我们使用sdl2.ext.Resources类使它们可用。

If you want to continue to use the line: 如果要继续使用该行:

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

then from the directory where you are running your script with this line, create a child-directory called "resources" and run your script. 然后从此行在运行脚本的目录中创建一个名为“ resources”的子目录并运行脚本。 You will see the error message will not appear. 您将看到错误消息不会出现。 You can use names other than "resources", just make sure a child directory with the same name exist. 您可以使用“资源”以外的名称,只要确保存在具有相同名称的子目录即可。

More importantly, to use sdl2, you just need the following 2 lines to load the sdl2 modules and you are good to go.. 更重要的是,要使用sdl2,您只需要执行以下两行即可加载sdl2模块,因此一切顺利。

import sdl2
import sdl2.ext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM