0

I'm reading the book "Python Crash Course" and I'm following along with the project alien invasion. I installed Python and the package Pygame as the book shows it.

When I run my code I get a ModuleNotFoundError:

ModuleNotFoundError: No module named 'pygame'
[Finished in 0.0s with exit code 1]
[cmd: ['python3', '-u', '/Users/antonio_spatuzzi/Documents/python_work/alien_invasion/alien_invasion.py']]
[dir: /Users/antonio_spatuzzi/Documents/python_work/alien_invasion]
[path: /Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]

even though the package has been successfully installed:

``Requirement already satisfied: pygame in ./.local/lib/python3.8/site-packages (2.0.1)```

How can I solve this problem?

3 Answers3

1

It seems that you installed the module for python 3.8 and you try to run it with python 3.9. Try installing the package with

python -m pip install
Mihai
  • 831
  • 6
  • 13
  • Thank you for your quick answer. Unfortunately did not work, but the output is exactly like before. – Antonio Spatuzzi Mar 03 '21 at 08:15
  • Should probably be `python3 -m pip install pygame`. Other than that, the answer is correctly pointing out that your problem is having multiple python version installed. – sloth Mar 03 '21 at 10:54
0

We can see from the output you posted that python3 is being used to run your programs:

[cmd: ['python3', '-u', '/Users/antonio_spatuzzi/Documents/python_work/alien_invasion/alien_invasion.py']]

So as was mentioned in an earlier comment, running python3 -m pip install pygame should install Pygame to the version of Python that you're currently using.

japhyr
  • 1,710
  • 2
  • 18
  • 24
  • I solved the problem by uninstalling all the version of Python I had on my computer and then I installed only the most recent one. This time I did not use Anaconda. – Antonio Spatuzzi Mar 06 '21 at 10:02
-1

For beginners it's not a good practice to have two different versions of python installed. If you are on windows try to uninstall all versions of python. (maybe this helps: How to completely remove Python from a Windows machine?)

Then try installing the newest version of python and pygame. In general, it's good practice to make a virtual environment with different instances of python and the respective packages used for a special project. Maybe have a look into Anaconda

Loewe8
  • 71
  • 9
  • 1
    There are many reasons it's appropriate to have multiple versions of Python, or any language you're working with, installed. The simplest reason is that you're working with several projects, each of which was started in a different version, and they can't all be upgraded to the latest version at the same time. You might say that newer programmers should just have one version installed for simplicity, but to say that "it's never good practice" is just plain wrong. – japhyr Mar 03 '21 at 15:52
  • You are totally right. Changed it. But even later on, you should always use some type of closed container for projects (like Anaconda or Docker). Allows to have multiple versions, packages, etc. But for the scope of this question it would be too much to ask for, don't you think? – Loewe8 Nov 10 '21 at 14:28
  • 1
    For many long-lived projects such as data science projects and web apps, working in isolated environments is critical. These kinds of projects tend to have many dependencies, and will end up requiring different versions of those dependencies than other projects over time. However, many Pygame projects only depend on Pygame, and tend to have few breaking changes when a new version comes out. So for most people it's perfectly reasonable to install Pygame system wide and not bother with a venv. – japhyr Nov 10 '21 at 15:05