3

I am a beginner and I want to work with the pygame module. I have already used pip install to install pygame and it says so on my command prompt. On my command prompt it says:

Requirement already satisfied: pygame in c:\users\35192\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages (2.1.2)

However, when I try to import pygame in vscode, the module won't import and it says: "ModuleNotFoundError: No module named 'pygame'"

Does anyone know how to fix this?

António Rebelo
  • 186
  • 1
  • 13
  • 2
    Please read [Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the error trace. – Rabbid76 Apr 04 '22 at 19:41
  • 1
    You probably have more than one version of python installed, and `pip` is using a different one than vscode is. Try installing it with `python -m pip install pygame`. If the version of python on your path is the same one vscode uses, that will solve it. – baileythegreen Apr 04 '22 at 19:43
  • 1
    @baileythegreen I do have more than one version of python installed. I have tried that command and I still get the same error unfortunately :(. How can I run the same version of python everywhere? – António Rebelo Apr 04 '22 at 19:46
  • 1
    *Everywhere* is probably not a question I can answer, as I don't know your computer setup. However, you can run `which python` from the command line, and that will tell you where the copy it's running is located. I assume vscode has a way to run shell commands inside it, so you can do the same there, or look in the settings (I don't know where) to see where it is getting python from. Changing the location in vscode to what you get from the command line would do it—*but there are reasons why that might be a bad idea*, like if one is python2 and the other python3, and you need it like that. – baileythegreen Apr 04 '22 at 19:50
  • 1
    @AntónioRebelo, Maybe see if vscode has a way to install packages; or, if you can run shell commands within it, run `python -m pip install pygame` there. – baileythegreen Apr 04 '22 at 19:51

2 Answers2

3

I solved the problem. I basically just used Ctrl+Shift+P to open up the command palette. From there, I opened "Python: Select interpreter" and then I just changed the python interpreter to be the global one (which I assume is the one used by the command prompt)

António Rebelo
  • 186
  • 1
  • 13
0

As seen here: https://stackoverflow.com/questions/15185827/can-pip-be-used-with-python-tools-in-visual-studio#:~:text=Go%20to%20Tools%20-%3E%20Python%20Tools%20-%3E%20Python,enter%20your%20module%20and%20double%20click%20to%20install.

You have to go to Tools -> Python Tools -> Python Environments in vs code. Then click on your python installation, then pip, and install pygame.

Or you can find where your vs code python installation is, and run this: "directory to vs code python.exe" -m pip install pygame

If you would like to use one python installation everywhere, you can change your vs code interpreter/environment to the python you already have installed on your system. As seen here: https://code.visualstudio.com/docs/languages/python

EshanT201
  • 39
  • 8
  • 1
    I can't find 'tools', where is it? – António Rebelo Apr 04 '22 at 19:55
  • 1
    I don't quite get what you mean, could you explain further please (the last edit) – António Rebelo Apr 04 '22 at 19:57
  • For example, if your python.exe file for vs code (python interpreter/app) is in: C:\Program Files\Python310. Load up a terminal in this directory (Use CD command for windows), and then do: python.exe -m pip install pygame. If your interpreter has a different name then change python.exe to your interpreters file name. Edit: You should take a look at this video https://www.youtube.com/watch?v=W--_EOzdTHk – EshanT201 Apr 04 '22 at 20:02
  • Are you on windows? What is your OS? – EshanT201 Apr 04 '22 at 21:08
  • I am on windows. How do I know which installation is in my vs code? – António Rebelo Apr 05 '22 at 09:55