Pygame attribute, init() - python

I'm trying to use Pygame with Python 3.3 on my windows 8 laptop. Pygame installed fine and when I import pygame it imports fine as well. Although when I try to execute this small code:
import pygame
pygame.init()
size=[700,500]
screen=pygame.display.set_mode(size)
I get this error:
Traceback (most recent call last):
File "C:\Users\name\documents\python\pygame_example.py", line 3, in <module>
pygame.init()
AttributeError: 'module' object has no attribute 'init'
I used pygame-1.9.2a0-hg_56e0eadfc267.win32-py3.3 to install Pygame. Pygame is installed in this location 'C:\PythonX' and Python 3.3 is installed in this location 'C:\Python33'. I have looked at other people having the same or similar problem and it doesn't seem to solve the error. Have I done anything wrong when installing Pygame? Or does it not support windows 8?

After
import pygame
pygame.init()
I got this error message and programm didnt work:
" AttributeError: module 'pygame' has no attribute 'init' "
because i named the file "pygame.py"...
When i chaned filename to "pygametest.py" everything worked.
Naming the filename exactly like the modulename seems to confuse python...

You have a directory named pygame in your path somewhere.
$ mkdir pygame # empty directory
$ python3.3
>>> import pygame
>>> pygame
<module 'pygame' (namespace)>
>>> pygame.__path__
_NamespacePath(['./pygame'])
Remove or rename this directory, it is masking the actual pygame package.
If you use print(pygame.__path__) it'll tell you where the directory was found; in the above example it was found relative to the current directory (./).

I also named my app pygame.py and i had the same issue but when i changed it, it worked for me .

Try to place the pygame module in .\PythonX\Lib\site-packages\
I use pip to install all modules I need.
Have you downloaded pygame and manually placed it in the PythonX folder ? Maybe the error comes from that.

Related

I can`t import wxpython in anywhere except python console

I installed python 3.8.8 and installed wxpython using pip at terminal
pip install wxpython
and i run simple program
import wx
print(wx.version())
in pycharm and pycharm`s python console, I got
ModuleNotFoundError: No module named 'wx'
in IDLE, I got
Traceback (most recent call last):
File "C:/Users/tasoo/OneDrive/Desktop/wx.py", line 1, in <module>
import wx
File "C:/Users/tasoo/OneDrive/Desktop\wx.py", line 2, in <module>
print(wx.version())
AttributeError: partially initialized module 'wx' has no attribute 'version' (most likely due to a circular import)
in python.exe code works
I want to import wx in pycharm project.
I tried add python in system path but it didn`t work.
You have problem because you saved code in file wx.py and now import wx loads your file wx.py instead of module wx. Rename your file - ie. main.py instead of wx.py
PyCharm may have own Python installed and it may need to install wx in this Python.
Check
import sys
print(sys.executable)
to get full path to Python used by PyCharm and then use this path
/full/path/to/python -m pip install wx
Or search in PyCharm settings (in menu File) and change Python Interpreter.
In PyCharm for every project you may set different Python - if you have installed many versions.

No module named 'pygame'

I'm workin on macOS 10.15.6. I need the Pygame module and I can't seem to import it in a project.
MacBook-Pro:program my_username$ python3 program.py
Traceback (most recent call last):
File "program.py", line 1, in <module>
import pygame
ModuleNotFoundError: No module named 'pygame'
If I try to use import pygame as pygame, I get this:
MacBook-Pro:program my_username$ python3 program.py
Traceback (most recent call last):
File "program.py", line 1, in <module>
import pygame as pygame
ModuleNotFoundError: No module named 'pygame'
When I type pip3 install pygame, I get Requirement already satisfied: pygame in /usr/local/lib/python3.9/site-packages (2.0.1).
I tried to add /usr/local/lib/python3.9/site-packages (2.0.1) to the path using this tutorial.
echo $PATH gives me:
/usr/local/bin:
/Library/Frameworks/Python.framework/Versions/3.8/bin:
/Library/Frameworks/Python.framework/Versions/3.8/bin:
/usr/local/bin:
/Library/Frameworks/Python.framework/Versions/3.8/bin:
/Library/Frameworks/Python.framework/Versions/3.6/bin:
/Library/Frameworks/Python.framework/Versions/3.6/bin:
/usr/local/bin:
/usr/bin:
/bin:
/usr/sbin:
/sbin:
/usr/local/lib/python3.9:
IDLE 3 finds pygame:
>>> import pygame
pygame 2.0.1 (SDL 2.0.14, Python 3.8.6)
Hello from the pygame community. https://www.pygame.org/contribute.html
Can you help me? What should I do to use pygame?
To start, it's highly advised that you check for python virtual environment usage, your project might using venv and the interpreter associated with your project isn't pointing it's PYTHONPATH to the mentioned address.
You can try 2 things:
Use this code at the beginning of your code (Some kind of temporary solution)
import sys
sys.path.insert(0, "/usr/local/lib/python3.9/site-packages (2.0.1)")
Run your python code (assuming you're not using any kind of IDE):
PYTHONPATH=/usr/local/lib/python3.9/site-packages (2.0.1):$PYTHONPATH python3 your-code
As pointed out by the above answer, you may be using a virtual environment. Are you using Pycharm? If so, either create a new project and check use default system interpreter or something on the lines of that.
You could also add modules to the virtual environment by going to File>Preferences (or settings)>Project:(project name)>Python Interpreter, press the + button and search for pygame.

Python and pygame error message "image not found"

I've installed python 3.8.7 on my mid 2009 macbook pro,and can't seem to properly install pygame on it. I've gone into terminal and installed it using the command "pip3 install pygame" which seemed to work fine, but when I try to import it in python it comes up with the error message "image not found". If anyone could help it would be greatly appreciated.
Error stack trace
Traceback (most recent call last): File "/Users/user/Documents/check.py",
line 1, in <module> import pygame File
"/Users/user/Library/Python/3.8/lib/python/site-packages/pygame/__init__.py",
line 81, in <module> from pygame.base import * # pylint: disable=wildcard-
import; lgtm[py/polluting-import] ImportError:
dlopen(/Users/user/Library/Python/3.8/lib/python/site-
packages/pygame/base.cpython-38-darwin.soLibrary not loaded:
/System/Library/Frameworks/Metal.framework/Versions/A/Metal Referenced from:
/Users/user/Library/Python/3.8/lib/python/site-packages/pygame/base.cpython-
38-darwin.so Reason: image not found
as user "ModoUnreal" said in that post
(Error while import pygame)
"A. Check if you have a 64 or 32 bit version pygame and make sure your version of python is the same.
B. Use something like pip install or any other type of method to install pygame.
C. Install a previous version of pygame and python, you might have some more luck with that.
The error is basically saying that a class in pygame called image could not be found. Obviously pygame has not been installed properly so I would suggest using the above methods to install it again.
I hope this helps!"

Python sound modules not working (Circular Import)

After installation of playsound and simpleaudio modules to try simple commands in order to play some audio files I always get the following message.
playsound: ImportError: cannot import name 'playsound' from partially initialized module 'playsound' (most likely due to a circular import) (/Users/joaosoares/Documents/PySound/playsound.py)
simpleaudio:AttributeError: partially initialized module 'simpleaudio' has no attribute 'WaveObject' (most likely due to a circular import)
I am currently using python 3.8.1, I've tried to create a new environment using pipenv and the problem persists. I got it to work the very first time I installed playsound. The problem might came after I had to install AppKit.NSSound as needed to run simplesound. My problem may be obvious and I apologise in advance if my issue isn't clarified I started to use python very recently.
I am following this tutorial
The only way i can reproduce your error is if i create a file called playsound.py
from playsound import playsound
playsound('myfile.wav')
ERROR
Traceback (most recent call last):
File "C:/Users/Chris/PycharmProjects/stackoverflow/stackoverflow.py", line 1, in <module>
from playsound import playsound
File "C:\Users\Chris\PycharmProjects\stackoverflow\playsound.py", line 1, in <module>
from playsound import playsound
ImportError: cannot import name 'playsound' from partially initialized module 'playsound' (most likely due to a circular import) (C:\Users\Chris\PycharmProjects\stackoverflow\playsound.py)
In your question your error specifically says your file is called playsound.py
/Users/joaosoares/Documents/PySound/playsound.py
rename this to something else otherwise it will try to import its self
There is Error in playsound Module So I has solved it for beginners
Hope so it's useful for you
Write This Command in Terminal that is provided in the image
https://i.stack.imgur.com/j6zcN.png
And Then Again Run the Python File and the Error is Solved
Now you can play sound using playsound module
https://i.stack.imgur.com/VQiNN.png
Note : Use double \ in path if you are a windows user else use \ in Mac and also enter Correct Path of song
just uninstall the current version by typing:
pip uninstall playsound
then install the older version by typing:
pip install playsound==1.2.2
just remove that playsound (use another name) and you are done

Python says pygames has no attribute 'init()' but lib is installed correctly

I'm using Python 2.7 with pygame-1.9.2pre.win32-py2.7.‌exe from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame
I've run this script:
import pygame
print 'PyGame Version: %s ' % pygame.__version__
to see if my module was installed correctly and I got:
PyGame Version: 1.9.2pre
As far as I know, it means that pygame is working correctly. In Python IDLE i try to run interactively:
import pygame
pygame.init()
# Set the height and width of the screen
size=[700,500]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
And it works fine. But whenever I try to run this as a script I get:
Traceback (most recent call last):
File "D:\Computing\Workspaces\Python\test\pygame.py", line 5, in <module>
import pygame
File "D:\Computing\Workspaces\Python\test\pygame.py", line 13, in <module>
pygame.init()
AttributeError: 'module' object has no attribute 'init'
On Eclipse or Running in IDLE either. What this could be?
If your script is named pygame.py Python will import it instead of the actual pygame package (when you do import pygame). That might explain your error.
when you named your script pygame.py and tried to run it, python created a file name "pygame.pyc" (=python compiled file) and put it in the directory you saved your pygame.py script.
you have to remove the newley created .pyc file and rename your script to somthing like my_py_game.py instead of pygame.py

Categories

Resources