1

我下载了 pySDL2(来自https://bitbucket.org/marcusva/py-sdl2/downloads)并将 SDL2 包解压缩到我的文件夹 C:\Python34\Lib\site-packages\PySDL2-0.9.3,它有一个子文件夹sdl2 有一个子文件夹 ext。

我还使用标题将“hello world”程序复制到同一个文件夹:

import os
os.environ["PYSDL2_DLL_PATH"] = "/Python34/Lib/site-packages/PySDL2-0.9.3"
import sys
import sdl2.ext

我从同一个文件夹运行它,它说它找不到 sdl2。(我使用了 os.environ 行,因为我已经“设置”了环境变量,但它没有帮助)

ImportError:找不到 SDL2 的任何库(PYSDL2_DLL_PATH:/Python34/Lib /site-packages/PySDL2-0.9.3/sdl2)

所以我运行了 pip install PySDL2,它说: C:\Python34\Lib\site-packages\PySDL2-0.9.3>pip install pysdl2 要求已经满足(使用 --upgrade 升级): pysdl2 in c:\python34\ lib\site-packages 清理...

所以,我在 python 库中有包,我在环境中指向它,pip 说它已经存在,但不知何故 python 找不到它来导入。

我应该做什么?

4

2 回答 2

2

PySDL2 不附带 SDL2 库。

您需要 SDL2 库才能使 PySDL2 工作。SDL2 是完成所有艰苦工作的库。PySDL2 只是允许您从 Python 访问它的接口。

有关如何安装它的详细信息,请查看http://pysdl2.readthedocs.org/en/latest/install.html 。然后查看http://pysdl2.readthedocs.org/en/latest/integration.html以获取有关如何使用 PYSDL2_DLL_PATH 的信息

对于我的项目,我选择根本不在 Python 中安装 PySDL2。我将所有 PySDL2 内容放在名为“sdl2”的项目子目录中,并将所有 Windows SDL2 DLL 放在名为“sdl2_dll”的单独子目录中。然后在项目的根目录中,我有以下名为 sdlimport.py 的文件

"""Imports PySDL2 

This module imports PySDL2 and the SDL2 libraries held within the project
structure (i.e. not installed in Python or in the system).

Setup:

    [myproject]
     |-sdlimport.py
     |-main.py
     |-[sdl2]
     |  |-The PySDL2 files
     |-[sdl2_dll]
        |-SDL2.dll
        |-SDL2_image.dll
        |-SDL2_mixer.dll
        |-SDL2_ttf.dll
        |-and all the other dlls needed

    Edit sdlimport.py to include which bits of sdl2 you need.

Example:

    from sdlimport import *
    sdl2.dostuff()

"""

import os

# app_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")
app_dir = os.path.dirname(os.path.realpath(__file__))
"""str:  the path to your project, detected at import time
"""
sdl2_dll_path = os.path.join(app_dir, "sdl2_dll")
os.environ["PYSDL2_DLL_PATH"] = sdl2_dll_path

#--- Comment these out as needed ---
import sdl2
import sdl2.sdlimage
import sdl2.sdlttf
#import sdl2.sdlgfx 
#import sdl2.sdlmixer 
import sdl2.ext

然后,在每个需要 pysdl2 的文件中,使用from sdlimport import *

于 2015-11-03T19:58:36.307 回答
0

在我的身上,我必须这样做才能让它知道 sdl2 的位置(在我下载了 sdl2,pysdl2 之后)。

导入操作系统

os.environ["PYSDL2_DLL_PATH"] = r"c:\yourdirectory"

导入 sdl2.ext

我尝试过的任何其他方式都行不通。只需更改到 sdl2.dll 所在的目录即可。

于 2016-02-21T21:28:03.427 回答