How to import pygame.locals successfully - python

Hello there Stackoverflowers,
I am learning how to program with Pygame and I am trying to import pygame.locals
According to the tutorial I am learning from, I am starting of as such;
import pygame, sys
from pygame.locals import *
I get this error message:
Traceback (most recent call last): File "C:/Python33/test.py", line 2, in from pygame.locals import * ImportError: No module named 'pygame.locals'
I have downloaded Pygame (binary package) for Python 3.2, and I am currently running 3.3.
When I import pygame in the Python shell, no error is returned, and thus I am lead to believe that Pygame was successfully installed.
I am running Windows 7 64 Bit.
I've seen a similar post regarding pygame.locals and Raspberry Pi and Linux, so I think this post is still relevant. Let me know if it is not.

My guess is that you have a file named pygame.py in the current directory in which you are running you script.

I found that on Windows 7 64-bit, you need to get Python 3.3.5, as well as the pygame-1.9.2a0-hg version from Bitbucket.
This solved the problem ImportError: No module named 'pygame.locals' for me.
(Somehow it's a bit weird that the pygame website doesn't provide latest versions of pygame.)

yes #Bartlomiej Lewandowski, you were right.I also faced the same problem and came here to resolve it. I did not download any newer version as apt-get install was giving the message that this is the newest version.
I just renamed my file pygame.py to some other random name.
It is working fine now. So the problem was we named our file as pygame.py, Which is restricted in pygame module, I am not sure. But its working fine now.
Thanks Bartlomiej Lewandowski once again.

For RedHat I needed to install
yum install SDL-devel-1.2.14-7.el6_7.1.x86_64

Related

Problem with "No module named 'pygame'" while actually have it

So I just came up with a problem that i don't understand.
I have two python files one is for the separate game menu, and second for the game.
The menu is written with tkinter and the problem comes when I click( in my menu ) 'Start game' button. It destroys menu, but does not open another file - game. Here's the code from menu where the problem occurs.
root.destroy()
os.system('main.py')
That's what console says:
Traceback (most recent call last):
File "main.py", line 8, in <module>
import pygame
ModuleNotFoundError: No module named 'pygame'
Initial code from the second file (game file)
from backdoor import *
import pygame
from Config import *
pygame.init()
When I start only the game file it works as it should - there's no Errors, that's why i have no idea why this problem occurs. I'm new to coding so would appreciate any help and explanations.
To import pygame you must have it installed:
pip3 install pygame
If you have it already installed try to reinstall
pip3 uninstall pygame
pip3 install pygame
Found the solution, so the problem was my sillnes and a lack of knowledge. I was using Anaconda environment and I've installed pygame with navigator cmd, so i thought that it's installed. I've reinstalled Python and added pip path in cmd with setx PATH "%PATH%;path", then I could actually install pygame and now everything works properly. Thanks for help!

Why does it say that no module named pygame was found when I try to import turtle?

Basically, i'm trying to use the turtle module at home as I have been using it in school and it's quite fun to use. However, when I try to import it and run the code, it says that there is an error in line 1 of the code and says that no module named pygame was found. I didn't write pygame, I specifically wrote
from turtle import *
which at school works perfectly fine. Both my computer and the school computers use Python 3.6 but the turtle module will just not work at home. Any ideas why this might be happening? If it helps, I've tried to install pygame in the past but it wouldn't work so I stopped trying to install it, and just now I tried deleting and uninstalling pygame but it still shows up with the error. Thanks for taking your time to read my question.
The error that comes up is as follows:
Traceback (most recent call last):
File "C:/Users/homeuser/AppData/Local/Programs/Python/Python36-32/jsjs.py", line 1, in <module>
from turtle import *
File "C:/Users/homeuser/AppData/Local/Programs/Python/Python36-32\turtle.py", line 1, in <module>
import pygame
ModuleNotFoundError: No module named 'pygame'
>>>
Also, pip install pygame comes up with an error saying invalid syntax
First, you need to download the version of pygame suitable for your version of python. Here's a link to their download page: https://pypi.python.org/pypi/Pygame/1.9.3
Next, open the command prompt. This technique works provided that you have pip installed and you have the required environmental variables.
Now, type in the following into the command prompt: 'python -m pip install "Path/To/Downloaded/File.py"'
Here's a real-life example: 'python -m pip install "C:/Users/danie/Downloads/pygame-1.9.3-cp36-cp36m-win_amd64.whl"'
If you have any errors, it would most likely because:
- (A) You don't have pip installed properly.
- (B) You gave an invalid directory branch.
- (C) You spelled something in the command wrong.
- (D) You forgot to download the Pygame module.
I hope this helps.
Pygame should be a dependency of turtle. Try to download pygame with pip.
More information about the installation:
https://www.pygame.org/wiki/GettingStarted
You've been working inside a Python standard library directory, placing your own files there and clobbering Python's insides. The error you're seeing is because you've edited or replaced the turtle.py from the standard library. At this point, there's no telling how much damage has occurred, and manually fixing things isn't going to be practical.
Uninstall and reinstall Python to restore things to normal, and stop putting your own work in that directory.
Seeing the error code you've posted, it seems that some files placed in the python directory have been messed with or there must have been some changes. Nevertheless, You can try doing an uninstall and then doing a clean install of python.
Then proceed to download pygame by:
pip install pygame
and then turtle module. You may have to look up for the Tkinter module too.

Importerror on win32ui for 32 bit Python

I've been trying to run a bot for Super Hexagon (Just for fun), but when I try to run the module in IDLE 2.7.11 (32 bit) I get the error
Traceback (most recent call last) :
File "C:\Users(Me)\Desktop\Super-Hexagon-Bot\super_hexagon_bot.py", line 8, in < module >
import win32ui
ImportError: No module named win32ui
Unfortunately, right now I'm streaming my desktop to my Macbook, so I can't copy the code, but here's an image of the part the program has an error with.
I have 64 bit Windows 8, so the win32ui is most likely the problem, but I'm not sure how to change the code to accomodate 64 bit. Any suggestions?
I think the problem is not the the BIT version from your Windows PC.
It seems to me, that you have not installed he module win32ui.
After searching the web, I have found, the module pywin32, which should include all necessary WIN thinks.
You can install it via pip, easy_install of with these MSI installer:
http://sourceforge.net/projects/pywin32/files/pywin32/
Please use the newest and correct version.
But I think you have to change the code:
Sorry I have no Windows PC, so I could not try it, maybe something like that:
from pywin32 import *
It's most likely the Python libraries you are using here, is causing the issue. There is 64 bit build for python win32ui.
http://sourceforge.net/projects/pywin32/files/pywin32/Build%20219/
Download and install one of then and set the build path and import accordingly.

import pygame fails with embedded c++

Hi guys this is the problem
I have installed python 2.7 and pygame 1.9 in a windows 7 32 bin
When I use pygame with the python interpeter there is no problem it works fine
Then for the line
import pygame
no problem with console or calling python and the filename
But I want to embed this in a c++ project compiled with mingw32. When I import other packages like cv2 or numpy no problem but in the case of pygame I have this error
ImportError: DLL load failed: The specified module could not be found.
Does anybody knows where is the problem?
A lot of thanks
I have solved after a lot of googling. The problem was with the pygame packages in msi format.
I uninstalled it and I have installed wheel and the package
pygame‑1.9.2a0‑cp27‑none‑win_amd64.whl
from the page http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame
And that works for me. There was a problem with some dll missing in the case of msi packages and minggw32 compiling
A lot of thanks to all of us
:)

"no matching architecture in universal wrapper" when importing pygame

I'm on a MacBook Pro running Snow Leopard 10.6.8 and Python 2.7.2. I went on the pygame website, downloaded and installed all the required frameworks and programs, compiled and installed pygame, but I cant import the module into python. I downloaded an example program off of the pygame website and every time I run it I get this error:
Traceback (most recent call last):
File "/Users/jesse/Downloads/PurpleMines/PurpleMines.py", line 3, in <module>
from pygame import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/__init__.py", line 95, in <module>
from pygame.base import *
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/base.so, 2): no suitable image found. Did find:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/base.so: no matching architecture in universal wrapper
I'm guessing its a problem having to do with running a 64-bit OS?
Thanks in advance for the help.
Removing the old pygame install and reinstalling did the trick for me.
I'm running Mac OS 10.7
sudo rm -Rf /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame
Then copy and install pygame from:
http://www.pygame.org/ftp/pygame-1.9.2pre-py2.7-macosx10.7.mpkg.zip
While what Sylvain said above is correct, this error is often caused by differences in bit rate between python and pygame. It is possible that redownloading it only helps because you get the correct distribution that time. To tell the bit rate of python, do this:
import platform
platform.architecture()
If it is 32 bit, get a 32 bit distribution of pygame. Otherwise, get a 64 bit distribution.

Categories