Python 导入错误:没有名为“pygame”的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18317521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:28:03  来源:igfitidea点击:

ImportError: No module named 'pygame'

pythonimportpygame

提问by user2398618

I have installed python 3.3.2 and pygame 1.9.2a0. Whenever I try to import pygame by typing:

我已经安装了 python 3.3.2 和 pygame 1.9.2a0。每当我尝试通过键入以下内容导入 pygame 时:

import pygame

导入pygame

I get following error message :

我收到以下错误消息:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import pygame
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import pygame
ImportError: No module named 'pygame'
>>> 

I went through some of the questions related to this error but none of the solution helped. I have 64 bit machine with Win7 OS

我经历了一些与此错误相关的问题,但没有一个解决方案有帮助。我有 Win7 操作系统的 64 位机器

回答by aaron-coding

I was trying to figure this out for at least an hour. And you're right the problem is that the installation files are all for 32 bit.

我试图弄清楚这一点至少一个小时。你是对的,问题是安装文件都是 32 位的。

Luckily I found a link to the 64 pygame download! Here it is: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame

幸运的是,我找到了 64 pygame 下载的链接!这是:http: //www.lfd.uci.edu/~gohlke/pythonlibs/#pygame

Just pick the corresponding version according to your python version and it should work like magic. The installation feature will bring you to a bright-blue screen as the installation (at this point you know that the installation is correct for you.

只需根据您的 python 版本选择相应的版本,它应该像魔术一样工作。安装功能将带您进入亮蓝色屏幕作为安装(此时您知道安装是正确的。

Then go into the Python IDLE and type "import pygame" and you should not get any more errors.

然后进入 Python IDLE 并输入“import pygame”,您应该不会再出现任何错误。

Props go to @yuvi who shared the link with StackOverflow.

道具转到与 StackOverflow 共享链接的@yuvi。

回答by Decoded

I am a quite newbie to python and I was having same issue. (windows x64 os) I have solved, doing below steps

我是 python 的新手,我遇到了同样的问题。(windows x64 os) 我已经解决了,按照下面的步骤

  1. I removed python (x64 version) and pygame
  2. I have downloaded and installed python 2.6.6 x86: https://www.python.org/ftp/python/2.6.6/python-2.6.6.msi
  3. I have downloaded and installed pygame (when installing, I have chosen the directory that I installed python): http://pygame.org/ftp/pygame-1.9.1.win32-py2.6.msi
  4. Works well :)
  1. 我删除了 python(x64 版本)和 pygame
  2. 我已经下载并安装了 python 2.6.6 x86:https: //www.python.org/ftp/python/2.6.6/python-2.6.6.msi
  3. 我已经下载并安装了pygame(安装时我选择了我安装python的目录):http: //pygame.org/ftp/pygame-1.9.1.win32-py2.6.msi
  4. 效果很好 :)

回答by KalELonRedKryptonite

You don't need 64 bit Python on Win64 system, just install the 32bit versions of both Python and Pygame and they will work just fine (and there is a ton more modules for them anyways).

在 Win64 系统上您不需要 64 位 Python,只需安装 Python 和 Pygame 的 32 位版本,它们就可以正常工作(无论如何,它们还有很多模块)。

回答by TriGeo

I was getting the same error. It is because your version of Pygame is not compatible with your version of Python or Pydev. Go to this link and get the proper version of Pygame for your current version of Python. Ctrl F to find it faster or click on the word python in blue. up at the top. While you instal Pygame it should find the Python path by itself. At least mind did any ways. I run Pygame through Eclipse with Python 3.4.

我遇到了同样的错误。这是因为您的 Pygame 版本与您的 Python 或 Pydev 版本不兼容。转到此链接并获取适合您当前 Python 版本的 Pygame 版本。Ctrl F 以更快地找到它或单击蓝色的单词 python。在顶部。当您安装 Pygame 时,它​​应该自己找到 Python 路径。至少头脑没有任何办法。我使用 Python 3.4 通过 Eclipse 运行 Pygame。

http://www.lfd.uci.edu/~gohlke/pythonlibs/

http://www.lfd.uci.edu/~gohlke/pythonlibs/

回答by Charles

Here are instructions for users with the newer Python 3.5(Google brought me here, I suspect other 3.5 users might end up here as well):

以下是针对使用较新Python 3.5 的用户的说明(Google 把我带到这里,我怀疑其他 3.5 用户也可能会在这里):

I just successfully installed Pygame 1.9.2a0-cp35 on Windows and it runs with Python 3.5.1.

我刚刚在 Windows 上成功安装了 Pygame 1.9.2a0-cp35,它在 Python 3.5.1 上运行。

  • Install Python, and remember the install location
  • Go hereand download pygame-1.9.2a0-cp35-none-win32.whl
  • Move the downloaded .whl file to your python35/Scriptsdirectory
  • Open a command prompt in the Scriptsdirectory (Shift-Right clickin the directory > Open a command window here)
  • Enter the command:

    pip3 install pygame-1.9.2a0-cp35-none-win32.whl

  • If you get an error in the last step, try:

    python -m pip install pygame-1.9.2a0-cp35-none-win32.whl

  • 安装Python,记住安装位置
  • 这里下载pygame-1.9.2a0-cp35-none-win32.whl
  • 将下载的 .whl 文件移动到您的python35/Scripts目录
  • Scripts目录中打开命令提示符(Shift-Right click在目录中 > Open a command window here
  • 输入命令:

    pip3 install pygame-1.9.2a0-cp35-none-win32.whl

  • 如果在最后一步出现错误,请尝试:

    python -m pip install pygame-1.9.2a0-cp35-none-win32.whl

And that should do it. Tested as working on Windows 10 64bit.

那应该这样做。经测试可在 Windows 10 64 位上运行。

回答by TheHyperCake

I was having the same trouble and I did

我遇到了同样的问题,我做到了

pip install pygame

and that worked for me!

这对我有用!

回答by Megan Chang

I had the same problem and discovered that Pygamedoesn't work for Python3 at least on the Mac OS, but I also have Tython2installed in my computer as you probably do too, so when I use Pygame, I switch the path so that it uses python2 instead of python3. I use Sublime Text as my text editor so I just go to Tools> Build Systems> New Build Systemand enter the following:

我遇到了同样的问题,发现它Pygame至少在 Mac OS 上不适用于 Python3,但我也Tython2像您一样安装在我的计算机中,所以当我使用 时Pygame,我切换了路径,以便它使用 python2蟒蛇3。我用崇高句话作为我的文本编辑器,所以我只是去 Tools> Build Systems>New Build System并输入以下内容:

{
    "cmd": ["/usr/local/bin/python", "-u", "$file"],    
}

instead of

代替

{
    "cmd": ["/usr/local/bin/python3", "-u", "$file"],   
}

in my case. And when I'm not using pygame, I simply change the path back so that I can use Python3.

就我而言。当我不使用 时pygame,我只需将路径改回来,以便我可以使用 Python3。

回答by Srikar Madhavapeddy

go to python/scripts folder, open a command window to this path, type the following:

转到 python/scripts 文件夹,打开该路径的命令窗口,键入以下内容:

C:\python34\scripts> python -m pip install pygame --user

To test it, open python IDE and type

要测试它,请打开 python IDE 并键入

import pygame

print (pygame.ver)

It worked for me...

它对我有用...

回答by user2089810

Since no answer stated this:

由于没有答案说明这一点:

Make sure that, if you are using a virtual environment, you have activated it before trying to run the program.

如果您使用的是虚拟环境,请确保在尝试运行该程序之前已将其激活。

If you don't really know if you are using a virtual environment or not, check with the other contributors of the project. Or maybe try to find a file with the name activatelike this: find . -name activate.

如果您真的不知道您是否在使用虚拟环境,请咨询项目的其他贡献者。或者尝试查找名称activate如下的文件:find . -name activate.

回答by EDGE 074

  1. Install and download pygame .whl file.
  2. Move .whl file to your python35/Scripts
  3. Go to cmd
  4. Change directory to python scripts
  5. Type:

    pip install pygame
    
  1. 安装并下载 pygame .whl 文件。
  2. 将 .whl 文件移动到你的 python35/Scripts
  3. 进入 cmd
  4. 将目录更改为 python 脚本
  5. 类型:

    pip install pygame
    

Here is an example:

下面是一个例子:

C:\Users\user\AppData\Local\Programs\Python\Python36-32\Scripts>pip install pygame