Pygame.init() not working [duplicate] - python

This question already has answers here:
i keep getting the error 'module' object has no attribute 'init' [duplicate]
(2 answers)
Closed 4 years ago.
I downloaded pygame successfully but now when I do:
import pygame
pygame.init()
size = [400, 400]
pygame.display.set_mode(size)
it gives errors:
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module> pygame.init AttributeError:
'module' object has no attribute 'init'
And nothing is working for me. Please help me use init and display.
I am using
python 3.4.1 and
pygame 1.9.2 on
Windows 7 32bit and
I installed pygame on
c://python34/include/

You probably had named your file pygame.py
when you type:
import pygame
python won't import pygame module you want, instead it will try import local module pygame - your file. and your file hasn't method init.
try change file name from pygame.py to mypygame.py.

I had the same problem, but then I reinstalled everything and realized that I had a different version of pygame than I needed for python
I realized also that I shouldn't use the .zip archive to install. I looked in there and saw that there's an installer for it if you're on any major OS. For example, for Macintosh, there's .dmg files. For Windows, there's .msi, etc.

You probably installed your pygame library in the include/ folder, but somehow didn't have the correct files inside it. This may of happened because of some downloading issues or the website you were downloading from. Hope this helps! You want to type downloading pygame from pygame.org instead of some other places on the web because of possible errors or changes in code.

Related

Pygame window not showing up on macOS. Not my code, found on google [duplicate]

I'm trying to get Pygame running on my Mac OS Monterey. The library installs fine and ends up where it should be. I've written a number of functioning programs with Pygame on my Linux machine and am using them as tests. The problem from what I can gather is that pygame.display can't be initialized for some reason. Every time I run a Pygame program it fails on the call to pygame.display.set_mode().
I am primarily using a wack-a-mole clone for testing. Here is the error I get:
pygame 2.0.1 (SDL 2.0.14, Python 3.8.2) Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last): File "mole_attack.py", line 34, in <module>
WINDOWSURFACE = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.error: The video driver did not add any displays
Please let me know if anyone needs more info. I tried to be as detailed as possible. Thanks in advance for any help.
The problem was that the latest version of Pygame was 2.1.2 and I was using 2.1.0 which I thought was the latest. I updated Pygame and everything worked fine. I couldn’t find anything definitive on what changed between these versions and why this error existed to begin with. I’ll keep looking and see what I can find out.

Python-Pygame Not Working [duplicate]

This question already has answers here:
ImportError: No module named 'pygame'
(25 answers)
Closed last year.
I installed python(2.7) using anaconda on an ubuntu machine.
I installed pygame.
When I import pygame I get the error:
ImportError: No module named pygame
Interestingly, when I use /usr/bin/python,
the interpreter now gives no error for import python.
My code file has to run by command python x.py, not in te interpreter.
How could I resolve the issue?
Many Thanks.
Which version are you using? You most import pygame to a Python 2.7 shell.
http://pygame.org/download.shtml is for https://www.python.org/ version 2.7. Make sure you have downloaded and made the setup for Python 2.7 before you coming over and import pygame. If you have followed my steps, I hope you will see:
>>>
Pygame only works for python 2.7 and down. If you downloaded a later version make sure you go into setting in pygame and make sure you have selected python 2.7 to run. You can also do this by making a new project and when you name it scroll down to base interpreter and select python 2.7 since pygames will most likely have selected a higher version of python which will not work for pygames

Python Pygame Error

so i've recently installed pygame because i want to start programming with it. Before writing code, i decided to just make sure its running fine by testing it in IDLE.
i ran
import pygame
no error
i ran
pygame.init()
and got the following error
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
pygame.init()
AttributeError: module 'pygame' has no attribute 'init'
by doing some research i found out that it may be messing up the path, so i tried
pygame._path_
and got a similar error
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
pygame._path_
AttributeError: module 'pygame' has no attribute '_path_'
and i can't figure out whats wrong. i've tried solutions in other questions but nothing works
im using
Windows 10 Home
Python 3.5.2 32 Bit
Pygame 1.9.2a0 for python 3.2 (but after looking up if it would work with python 3.5, i found resources saying it would)
i tried installing the 3.5 version as a .whl but it had its own host of problems and just decided to use the installer. is the error because of the mismatch between python versions and pygame versions, or is there something else i can try?
I have tried the following
pygame._path_
there is nothing else named pygame.py that would confuse the path
i used the installer
i made sure there were no other versions of python/pygame installed
So are there any other solutions beyond the ones that i have tried to get this one working?
Thank you for your time.
EDIT 1: I have made sure that the only directory named pygame is the module.
to finding the pygame path:
Magic methods have two underscores on either side of them.
to the lack of pygame.init():
different software seem to be able to find different parts of pygame, because PyCharm cannot locate that function, but when running off the command line it operates perfectly. It may just be a fault of idle. Try running from the command line or double clicking the .py file, assuming it is associated with the interpreter. If that doesn't work, run the command
python -m pip install --upgrade pygame
Have you installed pygame correctly.
Put this in your cd (command prompt) to check if you have installed pygame properly
py -m pygame.examples.aliens
If it gives a error then you should use the pip install function
py -m pip install -U pygame --user
If everything fails you can install an IDE. I would recommend pycharm https://www.jetbrains.com/pycharm/download/#section=windows
It is a free IDE and you can install pygame from there for a project
File --> Settings --> Project:(Name) --> Project Interpreter --> pip --> type pygame and install package.
Pycharm pygame installation

Pygame - Import Error

I have downloaded pygame for 64 bit and when I try to import pygame , I keep on getting the following error.
Traceback (most recent call last):
File "D:\Python\test 2\test_2.py", line 4, in <module>
import pygame
ImportError: No module named 'pygame'
Note
According to http://www.pygame.org/download.shtml
NOTE: if you
had pygame 1.7.1 installed already, please uninstall it first. Either
using the uninstall feature - or remove the files:
c:\python25\lib\site-packages\pygame
Direct download link for python
Downoad link for python 2.7.11 ===> Here
Direct download link for pygame
Download link for pygame ===> Here
Make sure you install the correct python version and pygame version.Dont mix up the two.
The links I have provided are ONLY for windows and not any other operating system.
Well, you need to make sure you are downloading the correct version of pygame with python. Try redownloading pygame, and when your downloading it should say if it finds the proper python in the directory, if it doesn't theres a problem with the version you/your downloaded/downloading.

'Python Programming for the absolute beginner' - livewires [duplicate]

This is the third time pygame/livewires has led me to this website, and let it be the last! After installing 'Python 3.1.1' for the book, 'Python Programming for the Absolute Beginner', I installed pygame and then livewires. I tested Pygame by entering
import pygame
And it worked by properly running it. However, when I tested livewires with 'from livewires import games, colors it produced no errors in IDLE, but did produce an error when I properly ran it. The error flicked away so quick I couldn't see it, but I recorded it and paused it just at the right time and got this error:
Traceback (most recent call last):
File "C:\Python31\TEST", line 1, in , <module>
from livewires import games, colors
File "C:\Python34\lib\site-packages\livewires\games.py", line 57, in <module>
import pygame, pygame.image, pygame.nixer, pygame.font, pygame.transform
ImportError: No module named 'pygame.image'
Something I did notice about this is it says Python34, another version I have, in the error not Python31, though the problem may be somewhere else completely.
Please not that it is not my spelling of 'color' that is messing this up. The guy who made the book and bundled it with this special version of livewires seems to have made it spelled 'colors' and not 'colours'. I've tried both and using the British spelling (the one I normally use) just causes an error to appear in IDLE as well.
Please help, hopefully, for the last time.
Thanks in advance, Harry.
You should set your PATH and sys.path(the path modules are imported from) pointing to the same version of Python.
Currently Python 3.1 executable is in your path, but it is using the modules of Python 3.4.
To fix your python executable path: https://docs.python.org/2/using/windows.html#finding-the-python-executable
To fix your sys.path: https://docs.python.org/2/using/windows.html#finding-modules
And one way to have isolated versions of python on your system is using virtual-env: https://virtualenv.pypa.io/en/latest/

Categories

Resources