How to fix my non-working pygame library? - python

Problem is whenever I try to run any python file with import pygame in it, it does not work. It always results in a ModuleNotFoundError: No module named 'pygame', though I do have pygame library installed.
I don't know if this is part of the problem but my pygame package is for some reason in \Lib\site-packages.

Related

PyOpenGL isnt a package

Trying to run a basic pygame based PyOpenGL window and it returns an error.
I have reinstalled them both from https://www.lfd.uci.edu/~gohlke/pythonlibs/
Code:
import pygame as pg
from OpenGL.GL import *
Error:
ModuleNotFoundError: No module named 'OpenGL.GL'; 'OpenGL' is not a package
Tried running it, expected a window with a blue/grey background. recieved an error in VS Code terminal and anger
cgohlke's comment was right, the file I was using was called OpenGL.py changed the name and it worked fine

import error __init__.py how to fix this error

I am kind of an amateur to Pycharm.
Been trying to work with the Turtle package. It is clearly installed in the Pycharm,
but every time I import and run it, I see the below error.
I have been searching the net and all I could see was to check the interpreter and mine is the latest Python (3.10).
It will be really helpful if someone can suggest a remedy to this, this is happening with all the packages in fact.
Thank you!
from turtle import Turtle
ImportError: cannot import name 'Turtle' from 'turtle' (/usr/local/lib/python3.10/site-packages/turtle/__init__.py)

Python, pygame, py2exe

I can't make this .py .exe help me.
i have python34 installed on windows 7 - 32 bits
This is a Snake.py simple game and i can't convert it using py2exe.
i looked in google and stackoverflow and everywhere else but couldn't fix it.
i get this error when trying to convert the script.
The error: http://pastebin.com/uRHpg1B8
And this is my game: http://pastebin.com/R6A89Nhe
Well, based on your error output, it looks like you are missing several packages which are important for pygame to function. Error output duplicated below, for future reference.
8 missing Modules
------------------
? MacOS imported from pygame.macosx
? Numeric imported from pygame
? OpenGL imported from pygame
? Py25Queue imported from pygame.threads
? Queue imported from pygame.threads
? numpy imported from pygame, pygame._numpysndarra
y, pygame._numpysurfarray
? packaging imported from pkg_resources
? readline imported from cmd, code, pdb

`import pygame` shows no errors even when pygame is not installed

I've been installing, uninstalling, and reinstalling pygame because there's a program that I've been trying to run (that uses pygame). However whenever I've installed pygame, the program doesn't work.
Traceback (most recent call last):
File "C:\Users\Ryan\Desktop\en\snake.py", line 28, in <module>
class Segment(pygame.sprite.Sprite):
AttributeError: 'module' object has no attribute 'sprite'
(Pygame is not installed when I receive this error supposedly)
What's really confusing me though is that when I type import pygame in normally in IDLE, I get no errors even when I just uninstalled it.
Windows 8 64 bit
Python 3.4.3
Pygame-1.9.2a0.win32-py3.4.msi (pygame file name)
Python is installed on C Drive (C:\Python34)
Whenever Pygame was installed, it was on the D drive
Have Python directory added to Path
Here's a screenshot of a video tutorial I was following, and it shows there are two installation paths. And here's mine, which only shows one path. I'm not sure if this is a big deal, but I just want to be sure.
Open your python interpreter and import pygame
Then you can print pygame to see exactly what is being loaded, should be something like: <module 'pygame' from '/Library/Python/2.7/site-packages/pygame/__init__.py'>
(repeat the above step until it fails importing pygame),
Close your prompt and install pygame. Try executing it again. Should the problem persist, you can edit some file in the game you are trying to run and add two lines: import sys and print sys.path and check the list of paths to make sure that there is not some other pygame installation somewhere bundled with the game that is being used instead of your installed pygame.
Modification of Josep Valls's answer:
You will need to change sys.path. This can be accomplished with the following code:
import sys
sys.path.append('''directory where pygame is''')
import pygame
The sys.path variable tell Python where to look for modules. By adding a path by append, you are telling Python to look for imported modules there. After that pointer is made, you can import pygame.

Beginner unable to run pygame

I have just begun using pygame and am struggling to get anything running at all, I have reduced my code to these few lines:
import pygame
pygame.init()
size = (700, 500)
screen = pygame.display.set_mode(size)
but any code taken from working projects fails just as easily
Running this through shell causes it it to briefly open before crashing which from research IS normal?
But running it through the Python icon causes the prompt to open and close in an infinitesimal amount of time, I really don't know much about prompts so maybe it needs to be reconfigured or the PATH altered?
From research I've found that I'm meant to use a batch file but this just produces the error:
Attribute Error: 'module' object has no attribute 'display'
I'm sure my problem is straightforward and repetitive but I'm struggling to work out where even to begin in looking for advice myself, any helps appreciated thanks!
pygame.display is a module on its own, so you need to import it:
import pygame
import pygame.display
If you just import pygame, you are importing the pygame package (or to be more exact, the __init__.py file inside that package's folder). And while some package import all their submodules in that file, it is nothing you can assume/expect all packages to do. Therefor it's better to explicitly import the modules inside the package instead of just importing the top-level package.
Another possibility: Is your file called pygame.py by any chance? If yes, either rename it or add from __future__ import absolute_import to the top of your file.
That looks like a major corruption in your Pygame installation. Remove it, if necessary re-download, then re-install and try again.
Also make sure that you did not use any file name like "pygame.py" for your files. It would cause namespace-conflicts. So if you name your file "pygame.py" and then do "import pygame", it's basically your file being imported and I bet, you don't have a "display" class in there...
I used precisely the lines you have posted in your question and they run perfectly (Python 3.4, PyGame 1.9.2 on WinXP).
I just use
import pygame
from pygame import *
Works fine.

Categories