简体   繁体   English

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

[英]pygame error: “ImportError: No module named 'pygame'”

This is my situation:这是我的情况:

在此处输入图片说明

I tried importing pygame in both python 3.4.2 and python 3.6.3 using both pip and pip3 respectively.我尝试分别使用pippip3在 python 3.4.2 和 python 3.6.3 中导入pygame

In the python 3.4.2 shell:在 python 3.4.2 外壳中:

Traceback (most recent call last) is:回溯(最近一次通话)是:
File "", line 1, in文件“”,第 1 行,在
import pygame导入pygame
ImportError: No module named 'pygame'导入错误:没有名为“pygame”的模块

In the python 3.6.3 shell:在 python 3.6.3 shell 中:

Traceback (most recent call last):回溯(最近一次调用最后一次):
File "", line 1, in文件“”,第 1 行,在
import pygame导入pygame
File "C:\\Users\\aditya dand\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\pygame__init__.py", line 141, in from pygame.base import *文件“C:\\Users\\aditya dand\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\pygame__init__.py”,第141行,来自pygame.base import *
ModuleNotFoundError: No module named 'pygame.base' ModuleNotFoundError:没有名为“pygame.base”的模块

Those are the errors that occurred.这些是发生的错误。

I also used pygame-1.9.2a0-hg_5974ff8dae3c%2B.win32-py3.4.msi .我还使用了pygame-1.9.2a0-hg_5974ff8dae3c%2B.win32-py3.4.msi
It's showing the header's file of pygame , but it's not importing something.它显示了pygame的头文件,但它没有导入任何东西。

What can I do to solve this?我能做些什么来解决这个问题?

Step1:第1步:

check http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame to find a Windows installer that matches the version of Python you're running.检查http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame以找到与您正在运行的 Python 版本匹配的 Windows 安装程序。 The current version of pygame is : pygame-1.9.4-cp37-cp37m-win_amd64.whl pygame的当前版本是:pygame-1.9.4-cp37-cp37m-win_amd64.whl

Move this file to the folder C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python37\\Scripts将此文件移动到文件夹C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python37\\Scripts

========================================= ==========================================

Step2:第2步:

open cmd and then type:打开cmd ,然后输入:

cd\
cd /d C:\Windows
cd /d C:\Users\username\AppData\Local\Programs\Python\Python37\Scripts

and then type the command:然后输入命令:

pip install pygame

Now you have successfully installed pygame package.现在你已经成功安装了 pygame 包。

========================================= ==========================================

Step3:第三步:

open PyCharm File >> setting make sure the "Project Interpreter" has the "pygame" package like the following:打开 PyCharm 文件 >> 设置确保“项目解释器”具有如下所示的“pygame”包:

File>>Setting>> make sure the "pygame" package is there.文件>>设置>>确保“pygame”包在那里。

检查您是否已将python添加到路径

ans done if any one need help delete all python version from system and install python 3.5.2 from https://www.python.org/downloads/release/python-352/如果有人需要帮助从系统中删除所有 python 版本并从https://www.python.org/downloads/release/python-352/安装 python 3.5.2 /

then go to https://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame然后去https://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame

and search pygame and download pygame‑1.9.3‑cp35‑cp35m‑win_amd64.whl并搜索 pygame 并下载 pygame‑1.9.3‑cp35‑cp35m‑win_amd64.whl

then go to C:\\Users\\"your username"\\AppData\\Local\\Programs\\Python\\Python35\\Scripts> in this location in cmd ( command prompt )然后转到 C:\\Users\\"your username"\\AppData\\Local\\Programs\\Python\\Python35\\Scripts> 在 cmd 中的这个位置(命令提示符)

type this line and done " pip3 install pygame-1.9.3-cp35-cp35m-win_amd64.whl "输入这一行并完成“pip3 install pygame-1.9.3-cp35-cp35m-win_amd64.whl”

hope this help you希望这对你有帮助

Edit - pygame only works in python >= 3.6.*编辑 - pygame 仅适用于 python >= 3.6.*

If you are using Windows i had the same problem and i know how to fix it, follow the steps:如果您使用的是 Windows,我遇到了同样的问题并且我知道如何解决它,请按照以下步骤操作:

  1. Go to the pygame website, then go to the downloads page转到 pygame 网站,然后转到下载页面
  2. Once there scroll down to the windows section and copy the bit that it says to put into pip It should start with py一旦那里向下滚动到 windows 部分并复制它所说的位放入 pip 它应该以 py 开头
  3. Once this short piece of code is copied, open command prompt (search it on start, it is nothing to do with python)复制这段代码后,打开命令提示符(在开始时搜索它,与python无关)
  4. Open it and paste the line of code, it should say downloading and then installing and just wait a bit for it to finish.打开它并粘贴这行代码,它应该说下载然后安装,然后等待它完成。 Then in the command prompt paste the example.aliens thing in and it should work.然后在命令提示符中粘贴 example.aliens 的东西,它应该可以工作。
  5. Now the complicated part, open file explorer, go to this pc and search pygame once it has fully loaded, look near the top if the list of things it came up with.现在是复杂的部分,打开文件资源管理器,在它完全加载后转到这台电脑并搜索 pygame,如果它想出的东西列表,请查看顶部附近。 There should be 2 thing that are called pygame.应该有两个叫做 pygame 的东西。
  6. Now leave this open and hold the windows button and press r现在保持打开状态并按住 Windows 按钮并按r
  7. Then type %appdata% and press enter然后输入%appdata%并按回车键
  8. This will open the appdata roming file这将打开 appdata roming 文件

    Look at the bit at the top that says Appdata > roaming and click on app data查看顶部显示Appdata > roaming ,然后单击应用数据

  9. Then click local, then programs, then python, then python36 then go to scripts然后依次单击本地、程序、python、 python36然后转到脚本
  10. Now go back to the pygame search.现在回到 pygame 搜索。 Click and drag the 2nd thing called pygame into the scripts folder and you should be good to go.单击并将名为 pygame 的第二个东西拖到脚本文件夹中,您应该很高兴。

Pls comment if this doesnt make sense or you would like further details如果这没有意义,请发表评论,或者您想了解更多详细信息

Did you install pygame?你安装pygame了吗? You need to type pip install pygame or sudo install pygame into terminal.您需要在终端中输入pip install pygamesudo install pygame If you are using windows 10, press the windows logo key then type terminal.如果您使用的是 Windows 10,请按 Windows 徽标键,然后键入终端。 You can check it's installed with: pygame -H or pygame help您可以使用以下命令检查它是否已安装: pygame -Hpygame help

You can either use cmd to install pygame type this pip install pygame (you have to make sure that python is installed) If this doesn't help Use thonny idle I use it because python doesn't work with my pc basically Thonny is a python idle which runs without the requirement of python to be installed and you can also install modules using thonny and type python code as well but make sure that if you import a module in thonny that module can be only used in thonny and can't be used in any other idle unless you install it in cmd but you don't need it as thonny idle is great so if you want to install modules like pygame in thonny 1) Open thonny 2) click on tools 3) click on open shell There you can type pip install pygame and install it successfully I have a potato pc which can't run python because it requires service pack 1 windows7 or above I don't have it I only have windows 7 so I use thonny and use pygame using thonny only.您可以使用 cmd 安装 pygame 键入此 pip install pygame(您必须确保安装了 python) 如果这没有帮助 使用 thonny idle 我使用它,因为 python 不适用于我的电脑基本上 Thonny 是一个 python idle 无需安装 python 即可运行,您也可以使用 thonny 安装模块并键入 python 代码,但请确保如果您在 thonny 中导入模块,该模块只能在 thonny 中使用,不能使用在任何其他空闲中,除非您在 cmd 中安装它但您不需要它,因为 thonny idle 很棒,所以如果您想在 thonny 中安装像 pygame 这样的模块 1) 打开 thonny 2) 单击工具 3) 单击打开外壳可以输入 pip install pygame 并成功安装我有一台无法运行 python 的土豆电脑,因为它需要 service pack 1 windows7 或更高版本 我没有它 我只有 windows 7 所以我使用 thonny 并使用 pygame 仅使用 thonny . :D :D

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

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