简体   繁体   English

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

[英]ImportError: No module named 'pygame'

I have installed python 3.3.2 and pygame 1.9.2a0.我已经安装了 python 3.3.2 和 pygame 1.9.2a0。 Whenever I try to import pygame by typing:每当我尝试通过键入以下内容导入 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我有 64 位机器和 Win7 操作系统

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

To test it, open python IDE and type要测试它,请打开 python IDE 并键入

import pygame

print (pygame.ver)

It worked for me...它对我有用...

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安装Python,记住安装位置
  • Go here and download pygame-1.9.2a0-cp35-none-win32.whl这里下载pygame-1.9.2a0-cp35-none-win32.whl
  • Move the downloaded .whl file to your python35/Scripts directory将下载的 .whl 文件移动到您的python35/Scripts目录
  • Open a command prompt in the Scripts directory ( Shift - Right click in the directory > Open a command window here )Scripts目录中打开命令提示符( Shift - 在目录中Right click > 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

And that should do it.那应该这样做。 Tested as working on Windows 10 64bit.经测试可在 Windows 10 64 位上运行。

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!幸运的是,我找到了 64 pygame 下载的链接! Here it is: http://www.lfd.uci.edu/~gohlke/pythonlibs/#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.只需根据您的 python 版本选择相应的版本,它应该像魔术一样工作。 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.安装功能将带您进入亮蓝色屏幕作为安装(此时您知道安装是正确的。

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。

  1. open the folder where your python is installed打开安装python的文件夹
  2. open scripts folder打开脚本文件夹
  3. type cmd in the address bar.在地址栏中输入cmd It opens a command prompt window in that location它会在该位置打开一个命令提示符窗口
  4. type pip install pygame and press enter输入pip install pygame并按回车键
  5. it should download and install pygame module它应该下载并安装 pygame 模块
  6. now run your code.现在运行你的代码。 It works fine :-)它工作正常:-)

I had the same problem and discovered that Pygame doesn't work for Python3 at least on the Mac OS, but I also have Tython2 installed 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.我遇到了同样的问题,发现Pygame至少在 Mac OS 上不适用于 Python3,但我的计算机中也安装了Tython2 ,您可能也这样做,所以当我使用Pygame ,我切换路径,以便它使用 python2 而不是 python3。 I use Sublime Text as my text editor so I just go to Tools > Build Systems > New Build System and enter the following:我使用 Sublime Text 作为我的文本编辑器,所以我只需转到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。

Resolved !解决 !

Here is an example这是一个例子

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

The current PyGame release, 1.9.6 doesn't support Python 3.9.当前的PyGame版本 1.9.6 不支持 Python 3.9。 I fyou don't want to wait for PyGame 2.0, you have to use Python 3.8.如果你不想等待 PyGame 2.0,你必须使用 Python 3.8。 Alternatively, you can install a developer version by explicitly specifying the version ( 2.0.0.dev20 is the latest release at the time of writing):或者,您可以通过明确指定版本来安装开发人员版本( 2.0.0.dev20是撰写本文时的最新版本):

pip install pygame==2.0.0.dev20

or try to install a pre-release version by enabling the --pre option:或者尝试通过启用--pre选项来安装预发布版本:

pip install pygame --pre

I was getting the same error.我遇到了同样的错误。 It is because your version of Pygame is not compatible with your version of Python or Pydev.这是因为您的 Pygame 版本与您的 Python 或 Pydev 版本不兼容。 Go to this link and get the proper version of Pygame for your current version of Python.转到此链接并获取适合您当前 Python 版本的 Pygame 版本。 Ctrl F to find it faster or click on the word python in blue. Ctrl F 以更快地找到它或单击蓝色的单词 python。 up at the top.在顶部。 While you instal Pygame it should find the Python path by itself.当您安装 Pygame 时,它​​应该自己找到 Python 路径。 At least mind did any ways.至少头脑没有任何办法。 I run Pygame through Eclipse with Python 3.4.我使用 Python 3.4 通过 Eclipse 运行 Pygame。

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

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 activate like this: find . -name activate或者尝试查找名称为activate的文件,如下所示: find . -name activate find . -name activate . find . -name activate

  1. Install and download pygame .whl file.安装并下载 pygame .whl 文件。
  2. Move .whl file to your python35/Scripts将 .whl 文件移动到你的 python35/Scripts
  3. Go to cmd进入 cmd
  4. Change directory to python scripts将目录更改为 python 脚本
  5. Type:类型:

     pip install pygame

Here is an example:下面是一个例子:

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

在你的命令提示符下试试这个: python -m pip intsall pygame

I am a quite newbie to python and I was having same issue.我是 python 的新手,我遇到了同样的问题。 (windows x64 os) I have solved, doing below steps (windows x64 os) 我已经解决了,按照下面的步骤

  1. I removed python (x64 version) and pygame我删除了 python(x64 版本)和 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我已经下载并安装了 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我已经下载并安装了pygame(安装时我选择了我安装python的目录): http : //pygame.org/ftp/pygame-1.9.1.win32-py2.6.msi
  4. Works well :)效果很好 :)

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

I'm using the PyCharm IDE.我正在使用 PyCharm IDE。 I could get Pygame to work with IDLE but not with PyCharm.我可以让 Pygame 与 IDLE 一起工作,但不能与 PyCharm 一起工作。 This video helped me install Pygame through PyCharm.这个视频帮助我通过 PyCharm 安装了 Pygame。

https://youtu.be/HJ9bTO5yYw0 https://youtu.be/HJ9bTO5yYw0

(It seems that PyCharm only recognizes a package; if you use its GUI.) (似乎 PyCharm 只能识别包;如果您使用它的 GUI。)

However, there were a few slight differences for me;然而,对我来说有一些细微的不同。 because I'm using Windows instead of a Mac.因为我使用的是 Windows 而不是 Mac。

My “preferences” menu is found in: File->Settings…我的“首选项”菜单位于: File->Settings…

Then, in the next screen, I expanded my project menu, and clicked Project Interpreter.然后,在下一个屏幕中,我展开了我的项目菜单,并单击了 Project Interpreter。 Then I clicked the green plus icon to the right to get to the Available Packages screen.然后我单击右侧的绿色加号图标进入可用包屏幕。

I ran into the error a few days ago!我前几天遇到了这个错误! Thankfully, I found the answer.谢天谢地,我找到了答案。

You see, the problem is that pygame comes in a .whl (wheel) file/package.你看,问题是 pygame 来自一个 .whl(wheel)文件/包。 So, as a result, you have to pip install it.因此,因此,您必须 pip 安装它。

Pip installing is a very tricky process, so please be careful. Pip 安装是一个非常棘手的过程,所以请小心。 The steps are:-步骤是:-

Step1.第1步。 Go to C:/Python (whatever version you are using)/Scripts.转到 C:/Python(无论您使用什么版本)/Scripts。 Scroll down.向下滚动。 If you see a file named pip.exe, then that means that you are in the right folder.如果您看到名为 pip.exe 的文件,则表示您位于正确的文件夹中。 Copy the path.复制路径。

Step2.第2步。 In your computer, search for Environment Variables.在您的计算机中,搜索环境变量。 You should see an option labeled 'Edit the System Environment Variables'.您应该会看到一个标记为“编辑系统环境变量”的选项。 Click on it.点击它。

Step3.第三步。 There, you should see a dialogue box appear.在那里,您应该会看到一个对话框出现。 Click 'Environment Variables'.单击“环境变量”。 Click on 'Path'.单击“路径”。 Then, click 'New'.然后,单击“新建”。 Paste the path that you copies earlier.粘贴您之前复制的路径。

Step4.第四步。 Click 'Ok'.单击“确定”。

Step5.第五步。 Shift + Right Click wherever your pygame is installed. Shift + 右键单击​​ pygame 的安装位置。 Select 'Open Command Window Here' from the dropdown menu.从下拉菜单中选择“在此处打开命令窗口”。 Type in 'pip install py' then click tab and the full file name should fill in. Then, press Enter, and you're ready to go!输入“pip install py”,然后单击选项卡,应该填写完整的文件名。然后,按 Enter,您就可以开始了! Now you shouldn't get the error again!!!现在你不应该再收到错误了!!!

首先执行python3然后输入命令import pygame ,现在你可以看到输出

For this you have to install pygame package from the cmd (on Windows) or from terminal (on mac).为此,您必须从 cmd(在 Windows 上)或从终端(在 mac 上)安装 pygame 包。 Just type pip install pygame .If it doesn't work for you, then try using this statement pip3 install pygame .只需输入pip install pygame 。如果它对您不起作用,请尝试使用此语句pip3 install pygame If it is still showing an error then you don't have pip installed on your device and try installing pip first.如果它仍然显示错误,那么您的设备上没有安装 pip,请先尝试安装 pip。

我刚刚遇到了同样的问题,发现我的系统中安装了多个不同版本的解释器,当我使用命令安装它时,pygame 安装在其中一个中,但在我的 IDE 中选择了另一个解释器,所以这弄乱了我的系统,试试看你是否也有同样的情况。

确保如果您在 Windows 上,您的库目录已添加到路径

This may happen when pygame didn't installed, install the pygame first没有安装pygame时可能会发生这种情况,请先安装pygame

pip
pip install pygame

if dont work update the PIP by goto python install folder and type如果不工作,请通过转到 python 安装文件夹并键入更新 PIP

python -m pip install --upgrade pip

hope it work希望它有效

Try this solution: Type in to cmd (Windows):试试这个解决方案:输入 cmd(Windows):

C:\Users\'Your name'> pip install -U pygame

You should remove python -m , py -m , python3 -m before the pip Also remove --user behind it.您应该在 pip 之前删除python -mpy -mpython3 -m还要删除它后面的--user

It will said:它会说:

C:\Users\viait>pip install -U pygame
Defaulting to user installation because normal site-packages is not writeable
Collecting pygame
  Downloading pygame-2.1.2-cp310-cp310-win_amd64.whl (8.4 MB)
     ---------------------------------------- 8.4/8.4 MB 1.7 MB/s eta 0:00:00
Installing collected packages: pygame
Successfully installed pygame-2.1.2

Then test it in your IDE or cmd: (CMD example)然后在您的 IDE 或 cmd 中测试它:(CMD 示例)

C:\Users\viait>python
Python 3.10.3 (tags/v3.10.3:a342a49, Mar 16 2022, 13:07:40) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
pygame 2.1.2 (SDL 2.0.18, Python 3.10.3)
Hello from the pygame community. https://www.pygame.org/contribute.html

(IDE example) (IDE 示例)

import pygame

You can do this without any errors.您可以做到这一点而不会出现任何错误。

Just use this command in the terminal python3 -m pip install -U pygame --user只需在终端中使用此命令python3 -m pip install -U pygame --user

You could use你可以使用

pip install pygame

but if you use IDE like PyCharm, then you could just either install it from Python Packages or use right click at the package name then left click on Show Context Actions then left click on Install package pygame但是如果你像 PyCharm 一样使用 IDE,那么你可以从Python Packages安装它,或者右键单击 package 名称,然后左键单击Show Context Actions ,然后左键单击Install package pygame

(Personally, I recommended using Python Packages for the package installing because it has documentation with it) (就个人而言,我建议使用Python Packages进行 package 安装,因为它有文档)

您必须使用 Pycharm 并使用 pip install pygame 在终端中安装它,然后输入 Pycharm 并将鼠标悬停在“导入 pygame”中的 pygame 上,在 Pycharm 中它会告诉您下载它,您可以轻松下载它并享受您的结果

I was having the same trouble and I did我遇到了同样的问题,我做到了

pip install pygame

and that worked for me!这对我有用!

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

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