Dunno
Dunno

Reputation: 135

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?

Upvotes: 0

Views: 750

Answers (2)

Daniel George
Daniel George

Reputation: 41

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.

Upvotes: 0

dor mordehcai
dor mordehcai

Reputation: 102

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:

  1. 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)")
  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

Upvotes: 1

Related Questions