4

I installed pySDL2 0.4.1 by:

Downloading the source package, entering python setup.py install. Then I tried to run the copypasted into PyDev eclipse "The Pong Game: Getting Started" tutorial code example:

import os, sys

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())

I got the following error:

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

I have both Pypy and libSDL installed via Synaptic and have no external libraries added in PyDev - PythonPath.

What am I doing wrong?

Mark Hildreth
  • 36,998
  • 9
  • 113
  • 105
Folaht
  • 849
  • 8
  • 27

2 Answers2

1

It seems that you PySDL2 doesn't find the SDL2 runtime library. They are available on the libsdl download page (except for linux).

You should then let PySDL2 where the library is by setting the PYSDL2_DLL_PATH like so:

# 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

or in a python script:

# 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"

that way, PySDL2 will always find the library files (as long as the path is correct of course) ! This seems to me the easier way.

Happy coding with SDL2 !

achedeuzot
  • 3,659
  • 4
  • 38
  • 48
  • Setting the path using 'set' via the Windows command line doesn't actually help in my case. I need to include the python script line every time in order to run any example. I'm not sure if this is a problem other people will encounter, but I would like to mention that I have been thus far unable to successfully set the DLL path – Micrified Jun 27 '14 at 18:06
  • The 'set' command is temporary, you have to run it before the script every time and in the same shell. If you want it to persist, use 'setx' as seen here : http://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them – achedeuzot Jun 27 '14 at 19:30
  • Yeah, thanks! I actually figured that out later, and added it via Environment Variables. – Micrified Jun 27 '14 at 19:46
0

Almost solved. Ubuntu has SDL1.2 and SDL's main download page leads to SDL1.2 as well. One needs to install the source package by typing:

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 

Then download SDL_image-2.x.x.tar.gz from http://www.libsdl.org/tmp/SDL_image/

and type:

../configure
make
sudo make install 

and finally type:

ldconfig /usr/local/lib

Now the only thing that's not working is that SDL_QUIT is not being recognized.

Folaht
  • 849
  • 8
  • 27