No module named 'pygame' - python

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.

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.

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!"

Getting an error for a module that I definitely imported

import pyinputplus as pyip
while True:
prompt='Want to know how to keep an idiot busy for hours?\n'
response=pyip.inputYesNo(prompt)
if response=='no':
break
print('Thank you. Have a nice day.')
When I run my above code , I get this error:
Traceback (most recent call last):
File "c:\users\XXXXXX\mu_code\idiot.py", line 1, in <module>
import pyinputplus as pyip
File "c:\users\XXXXXX\mu_code\pyinputplus\__init__.py", line 15, in <module>
import pysimplevalidate as pysv # type: ignore
ModuleNotFoundError: No module named 'pysimplevalidate'
I cannot figure it out. The module is definitely installed. I've even moved it from the folder it was originally installed in to the mu folder where the py file is saved. Any help would be appreciated.
The ModuleError says that you do not have pysimplevalidate installed.
Using the same python executable as you are using to run your script (idiot.py), run
python -m pip install pysimplevalidate
or, even more bullet-proof:
<path_to_python.exe> -m pip install pysimplevalidate
If you are not sure what python executable the script is using, you can check it with
# put this on top of your script
import sys
print(sys.executable) # will print C:\path\to\python.exe

Python third party Module global import

i'm currently into learning a bit of python and i want to import the paperclip third party module into my python file.
Yes, i already installed the pyperclip module with
pip install pyperclip.
if i create a file on my desktop, i get an error which says
Traceback (most recent call last):
File "test.py", line 1, in <module>
import pyperclip
ImportError: No module named pyperclip
However if i put the test.py in my python folder, it runs.
The question now is, is there a way to make all my installed modules available on a global scope ? I just want to have my file e.g. on my Desktop and run it without having import issues.
Thank you.
Greetings
Edit: I'm working on a Mac, maybe this leads to the problem
Found the problem.
The pip installautomatically used pip3.5 install
whereas python test.pydidn't use python3.5 test.py
Thank you #Bakurìu
Is there a way i can define python3.5as python?
You can do this :
pip3.5 install paperclip
pyperclip is install but not paperclip

Pygame attribute, init()

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.

Categories

Resources