【问题标题】:How can I get python3.4 to find the PySDL2 module I downloaded on win7?如何让python3.4找到我在win7上下载的PySDL2模块?
【发布时间】:2015-09-19 08:42:45
【问题描述】:

我下载了 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 升级):c:\python34\ 中的 pysdl2 库\站点包 清理...

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

我该怎么办?

【问题讨论】:

  • 在使用 PIP 安装后,我必须下载 DLL 并使用 os.environ["PYSDL2_DLL_PATH"] 指向它。不要认为这是预期的行为,但至少它有效。

标签: python-3.x installation pysdl2


【解决方案1】:

SDL2 库不附带 PySDL2。

您需要 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 *

【讨论】:

    【解决方案2】:

    我必须这样做才能知道 sdl2 在哪里(在我下载了 sdl2 之后,pysdl2 已经安装)。

    导入操作系统

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

    导入 sdl2.ext

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 2016-02-29
      • 2022-01-26
      • 1970-01-01
      • 2016-04-19
      相关资源
      最近更新 更多