Search code examples
pythonpygamemodulenotfounderror

Running python file from command prompt ModuleNotFoundError: No module named 'pygame'


I have this error when running the script from terminal but works from PyCharm

C:\Users\Username\PycharmProjects\Space Invaders>python main.py
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import pygame
ModuleNotFoundError: No module named 'pygame'

This is how my file directory looks: https://i.sstatic.net/s9qB5.png

I am using python 3.8 and pygame 2.0.1

Should I have to install pygame globally for me to run the script from command line? I have the package installed in a virtual environment.


Solution

  • You're trying to execute the script with global python which doesn't have the pygame package installed. So, you have to activate the virtual environment first. To do this, go to venv/Scripts/ and there will be an "activate" file that you need to execute. Once you have done this you can run your script and it should work.

    More info on: https://docs.python.org/3/tutorial/venv.html