简体   繁体   English

如何在执行 python 文件时修复导入错误(“没有名为 pygame 的模块”)?

[英]How can I fix the ImportError ("No module named pygame") on executing a python file?

After typing SnakeGame.py I am getting the error:输入SnakeGame.py后出现错误:

Traceback (most recent call last): File "SnakeGame.py", line 1, in import pygame ImportError: No module named pygame).回溯(最近一次调用最后一次):文件“SnakeGame.py”,第 1 行,在 import pygame 导入错误:没有名为 pygame 的模块)。

Pls give me a solution on how to solve this, I have installed pygame also.请给我一个如何解决这个问题的解决方案,我也安装了pygame。

thanks in advance.提前致谢。

That error means that the python interpreter isn't able to find the pygame module.该错误意味着 python 解释器无法找到 pygame 模块。
Are you using a virtual environment?您使用的是虚拟环境吗? If you are using an IDE like PyCharm, try to install the pygame module inside the IDE console.如果您使用的是 PyCharm 之类的 IDE,请尝试在 IDE 控制台中安装 pygame 模块。

pip install pygame pip 安装 pygame

install pygame in CMD write pip install pygame if you use visual studio code go to market and type pygame then install it ...在 CMD 中安装 pygame 写 pip install pygame 如果您使用 Visual Studio 代码去市场并输入 pygame 然后安装它...

enter image description here在此处输入图片说明

visual studio code视觉工作室代码

I had this same problem a few days ago.几天前我遇到了同样的问题。 My solution was to use a virtual environment.我的解决方案是使用虚拟环境。 To do this, open a terminal in your folder and use the following commands: 1.) pip install pipenv 2.) pipenv shell 3.) pipenv install pygame为此,请在您的文件夹中打开一个终端并使用以下命令:1.) pip install pipenv 2.) pipenv shell 3.) pipenv install pygame

After this, you need to select the correct python interpreter.在此之后,您需要选择正确的 python 解释器。 In VS Code, hit ctrl+shift+p Then click the correct interpreter (should have the name of the folder).在 VS Code 中,点击 ctrl+shift+p 然后点击正确的解释器(应该有文件夹的名称)。 Then try to run the file.然后尝试运行该文件。 If that doesn't work, try to run this in the terminal: pipenv run {name of the python file, example: app.py}如果这不起作用,请尝试在终端中运行: pipenv run {name of the python file, example: app.py}

Hope that works for you希望对你有用

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

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