Why won't this work? The best solution I could find is that the name of my game can't be pygame and I changed the name, yet it still doesn't work. I'm getting this error message:
Traceback (most recent call last):
File "C:\Users\dnw13\Desktop\Python projects and notes\pygame games.py", line 1, in <module>
import pygame, sys
File "C:\Users\dnw13\Desktop\Python projects and notes\pygame.py", line 2, in <module>
from pygame.locals import *
ModuleNotFoundError: No module named 'pygame.locals'; 'pygame' is not a package
here is my code
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')
while True: # main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
First off, if you're going to use a library called pygame, you shouldn't also have a .py file named pygame.py in your working directory. Second, pygame, unlike tkinter, is not pre-installed the way tkinter was. You will need to install it separately using one of the following methods:
1st way, using pip:
# in the console (Terminal or cmd) depending on your OS, type:
# for windows: (replace 3 with 2 if using python 2)
py -3 -m pip install pygame
# or: (just pip for python 2)
pip3 install pygame
# for Debian: (Mac OS or Unix or Linux etc.)
pip3 install pygame
# just pip for python 2
# or: (replace 3 with 2 for python 2)
python3 -m install pygame
2nd way, from pre-compiled file:
Go to the official download page. And choose your operating system to download the file. You can also follow the official installation tutorial page.
3rd way, build from source:
Follow the same method from the second method and choose the source files. Then do ./configure then make && make install in the console. This method is not recommended.
If you work in Windows (I solved it in Win 10), look at global variables:
search for System Info
choose button Environment Variables...
look at the Path variable to see if Python has the right path
if not change it :)
if necessary - restart programm(s)
It happens when you have multiple versions of Python installed
Make sure pygame is added to the correct path. It installed pygame in the wrong place and python couldn't find it. It took it from where it had installed it (it tells you were this is when you install it.) and dragged it into where i save my games and it now works.
Related
I already have installed the pygame package and get this error :
ModuleNotFoundError: No module named 'pygame'
This is an issue I had for like a week now however I have found something that works with me and hopefully it works with you!
If you already have installed the pygame package and get this error :
ModuleNotFoundError: No module named 'pygame'
Try to debug your python file and hopefully the error wont appear. Also, make sure to not "Run Code" but instead "Run Python File". To do this at the top right corner should be a play button press that an select "Run Python File".
You can test your pygame with this code that will open up a blank pygame window:
import pygame
WIDTH, HEIGHT = 800, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
def main():
run = True
while run:
for event in pygame.event.get ():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if __name__ == "__main__":
main()
If the above doesn't work then try to upgrade or downgrade your python version and pygame version to:
Python: 3.7.4(32-bit)
Pygame: 1.9.6
To change python version:
Go to https://www.python.org/downloads/windows/ and scroll down till you find Python 3.7.4 and download the "Download Windows x86 executable installer"
Install pyhton
Add python to PATH
WINDOWS + R
sysdm.cpl
Advanced > Enviroment Variables > Path > Edit
Add the following to path on User Variable and System Variable
C:\Users\user\AppData\Local\Programs\Python\Python37-32\Scripts
C:\Users\user\AppData\Local\Programs\Python\Python37-32
To install pygame version 1.9.6:
WINDOWS + R
CMD
Enter the following
pip install pygame==1.9.6
4. To double check if version is correct
pip show pygame
Ensure to "Run Python File" and not "Run Code"
Hopefully this has helped!
I am trying to learn pygame and I have an issue. I use PyCharm and when I start to code, PyCharm cannot find references about pygame (I tried on VScode, it does the same). For exemple, the references 'init', 'quit', 'KEYDOWN', ... My code still work, I can execute it and no problems appear but I don't have the auto completation (this is annoying) and if it cannot find the reference, it might have a problem.
I have pygame installed, I tried to install with .whl files on python.org, with the pip command pip install pygame, with the pip command py -m pip install -U pygame --user, I tried to see if pygame worked with the command py -m pygame.examples.aliens and no problems. I tried to unintall it to re-install it but nothing work. My filename isn't 'pygame.py' and i already tried to see any solutions on youtube, openclassroom, stackoverflow, ... I'm using python 3.8.1 by the way.
If someone could help me, this is really annoying and I could not understand why it does that. Thanks you !
Here's my code test :
import pygame
pygame.init()
screen = pygame.display.set_mode((1080, 720))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
if event.type == pygame.KEYDOWN:
print('A key got pressed')
And here's some screenshots :
Ironic though, Pygame does have an init() method. I face the same issue while linting. For some reasons the linter can't recognize init() as a method.
open settings and search for "python.linting.pylintArgs" (no quotes)
then press add item and paste this "--extension-pkg-whitelist=pygame" (no quotes)
it should help for linting
import sys
import pygame
def run_game():
pygame.init()
screen=pygame.display.set_mode((1200,800))
pygame.display.set_caption("Space Battle")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
run_game()
I get this error:
Traceback (most recent call last):
File "C:/Users/Dell/PycharmProjects/test/test.py", line 3, in
import pygame
ModuleNotFoundError: No module named 'pygame'
I have installed pygame and did the import command on the cmd on windows.
How can I solve the problem?
If you have more than one python editor in your pc, make sure you installed pygame into the right python editor
This kind of problem arises because of two resiongs:
If you have more then on python version in your system and you don't set the python environment yet for default python version to use.
Some time editor not response well after installing packages or library. In this case just close the editor and restart it. Most of the time it helps to fetch the library.
I am trying to run pygame on the PyCharm IDE, I have installed the latest version of pygame for python 3.5 and have added it to the project interpreter. I installed pygame from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame and copied it too python35-32/Scripts/. The test program below runs fine in the python shell but I want to run it from PyCharm.
I have been trying to solve this for the past hour and I've hit a wall.
When I run this simple test program to see if pygame is working:
import pygame
pygame.init()
pygame.display.set_mode((800, 800))
I get this error:
Traceback (most recent call last):
File "C:/Users/jerem/PycharmProjects/Games/hello_pygame.py", line 1, in <module>
import pygame
File "C:\Users\jerem\AppData\Roaming\Python\Python35\site-packages\pygame\__init__.py", line 141, in <module>
from pygame.base import *
ImportError: No module named 'pygame.base'
Any help would be greatly appreiciated!
Thanks again,
JC
Follow the instructions provided here. I think its related to the problem you are having on pygame and PyCharm on windows.
How do I download Pygame for Python 3.5.1?
its bcus pycharme has not recognised you're env or working on wrong env
https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html
check this
I have successfully used pygame in the past, but now it won't work.
my source is
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')
while True: # main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
using this in the shell works fine and creates a pygame window, but it will not work when making a module. it says pygame is not a package.
You have two different versions of Python installed (Python(x,y) and the python.org version), and pygame is only installed on one of them. When you run the interpreter through IDLE or the command line for the version that has pygame associated with you, you can import pygame and use it just fine. However, in Windows Explorer, .py files are associated with the other version of Python, that does not have pygame installed, so when you try to run a file by double-clicking on it the wrong Python interpreter starts, and it can't import pygame because it's not installed for that version.
To fix this, all you need to do is re-associate .py files with the correct version of Python. Right-click on a .py file, select Properties, and in the dialog box that comes up find where it says something like "Opens with:" and click the Change button. Browse to C:\Python33 and select python.exe, and you should be all set. Make sure the option "Always use the selected program to open this kind of file" is set, then click OK.
I'm on XP right now, so the process might be slightly different depending on which version of Windows you're running, but it should be fairly similar.