windows 导入 pygame 失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8660716/
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
import pygame fails
提问by Andrey Rubshtein
I have installed python 2.6.6 and pygame 2.6 from here:
http://pygame.org/ftp/pygame-1.9.1.win32-py2.6.msi
我已经从这里安装了 python 2.6.6 和 pygame 2.6:http:
//pygame.org/ftp/pygame-1.9.1.win32-py2.6.msi
However, when I try to run this simple program, I get a an error:
但是,当我尝试运行这个简单的程序时,出现错误:
import pygame
pygame.init()
pygame.quit ()
The error says:
错误说:
unresolved import:pygame
未解决的导入:pygame
What could it be?
会是什么呢?
Edit: When I am doing
编辑:当我在做
import pygame
in C:\Python26\python.exe , the interpreter, it works ok.
So it is probably an Eclipse problem.
Anyone has an idea what to do?
在 C:\Python26\python.exe 中,解释器,它工作正常。
所以这可能是一个Eclipse问题。
任何人都知道该怎么做?
回答by eduffy
Before
前
import pygame
add the lines
添加行
import sys
print sys.path
if the path to pygame
isn't shown, then you've don't have the PYTHONPATH
environment variable set correctly.
如果pygame
未显示路径,则说明您没有PYTHONPATH
正确设置环境变量。
回答by Olu Smith
sudo pip install pygame
And then try:
然后尝试:
import pygame
That should work for you.
那应该对你有用。
回答by TriGeo
If Eclipse can not find Pygame when you import it. There is an issue with Pygame being in Python's path. If you are trying to run Pygame through Eclipse like I am. The easiest way is to uninstal Pygame. Download Pygame from here.
如果 Eclipse 导入时找不到 Pygame。Pygame 在 Python 的路径中存在问题。如果您像我一样尝试通过 Eclipse 运行 Pygame。最简单的方法是卸载 Pygame。从这里下载 Pygame。
http://www.lfd.uci.edu/~gohlke/pythonlibs/
http://www.lfd.uci.edu/~gohlke/pythonlibs/
Make sure it is the correct version of Pygame for your version of Python. When installing if it does not find the path by itself you will need to place it in yourself. My version of Pygame placed itself in the correct path.
确保它是适用于您的 Python 版本的正确 Pygame 版本。安装时,如果它自己找不到路径,则需要自己将其放置。我的 Pygame 版本将自己置于正确的路径中。
To check Python's path with out going in to preference. You can type the code.
在不进入偏好的情况下检查 Python 的路径。您可以键入代码。
import sys
print (sys.path)
If it is showing it in your path and it is still not working. You have the wrong version for you current Python. If every thing is right then your code should work. You can use the code below to print your Pygame version.
如果它显示在您的路径中,但仍然无法正常工作。您当前的 Python 版本错误。如果一切正常,那么您的代码应该可以工作。您可以使用下面的代码打印您的 Pygame 版本。
import pygame
pygame.init()
print (pygame.ver)
回答by V.desk
I had the same problem, so I suggest you check if you have created a path when you have installed your version of Python. For create the path, or check it, just go to the panel control, click "System and Security"
then "System"
, click on "Advanced system settings"
and on "Environment variables"
. Now you only need to click on "Path"
and "Edit"
: now you can see if you have already created a path or if you need to create a new path.
我遇到了同样的问题,因此我建议您在安装 Python 版本时检查是否已创建路径。要创建路径或检查它,只需转到面板控件,单击"System and Security"
then "System"
,单击"Advanced system settings"
和 on "Environment variables"
。现在你只需要点击"Path"
和"Edit"
:现在你可以看到你是否已经创建了一个路径,或者你是否需要创建一个新路径。
In the second case click on "new"
. Find YOUR path where Python is on computer and put it on string. Click "OK". Restart your computer, go to cmd and write
在第二种情况下,单击"new"
。找到 Python 在计算机上的路径并将其放在字符串上。单击“确定”。重启电脑,进入cmd,写
pip3 install pygame
then go to IDLE and run this simple code:
然后转到 IDLE 并运行这个简单的代码:
import pygame
I hope that you successfully installed pygame
.
希望你安装成功pygame
。
If it doesn't work, reinstall python and check to create a path during download.
如果它不起作用,请重新安装python并检查以在下载过程中创建路径。