eklavyaisabird
eklavyaisabird

Reputation: 35

Import "pygame" could not be resolved Pylance (reportMissingImports) error in VSCode on mac

import pygame

#initialise
pygame.init()

screen = pygame.display.set_mode((800, 600))

running = True
while running == True:
    for event in pygame.event.get():
        if event.type == pygame.quit:
            running = False

This code actually runs, but the error does not go away. I am following a pygame tutorial that uses pycharm. Is the issue the program I am using?

I'm a beginner and can't figure this out at all.

Upvotes: 3

Views: 16875

Answers (7)

Swatilekha Roy
Swatilekha Roy

Reputation: 1

We need to check if the editor's current Python version matches the correct directory path of the Python imported version. In order to do so,

  • First, we need to check the Python version shown on the bottom panel of the editor and click on it.

  • After clicking, we will get the above popup showing all the Python versions currently installed in the local system. Step 1 (Matching Version)

  • Click the correct directory path (matching the one in which Pygame is currently installed)

  • You may also edit the default import path from the Python settings.json file (search "Python" inside VS Code "Settings") and add the correct directory path in the "python.defaultInterpreterPath" field.

Step 2 (Editing Settings)

Step 3 (Adding Correct Directory Path to settings.json)

This solution worked for me!

Upvotes: 0

Renzo Zagni
Renzo Zagni

Reputation: 121

Ensure the python interpreter you are using contains the pygame libraries

enter image description here

Upvotes: 0

tunne
tunne

Reputation: 41

I have experienced the same problem on VSCode even though the package was already there using pip install pygame and I had to add the library folder in Pylance manually, I copied the library path and pasted it to Pylance extra path setting. It solved my problem. Pylance extra path setting

Upvotes: 2

Heitor Miguel
Heitor Miguel

Reputation: 31

After a long time trying to solve the problem, I had to manually add the library folder in Pylance.enter image description here

It may be that the location of your libraries is in a different place, but after the Python folder the location doesn't change (unless you changed it yourself), in my case it still didn't work, if yours doesn't work either, put: C:\python\python310\lib\site-packages\pygame If you installed without making any changes your python folder is in c:\Users\Your user\AppData\Roaming\Python...

Upvotes: 3

Deepak
Deepak

Reputation: 490

@eklavyaisabird - I have faced same issue on VSCode but I had added ./sources on Pylance extra path setting, it fixed the error

enter image description here

Upvotes: 1

Steven-MSFT
Steven-MSFT

Reputation: 8431

If you are using the global environment, you need to reopen the VSCode after you install the package you want to import.

enter image description here

If you are using the virtual environment, Plycance Language Server can detect the packages automatically after you installed them.

enter image description here

Upvotes: 3

eklavyaisabird
eklavyaisabird

Reputation: 35

After adding it to the environment using pycharm, it somehow fixed the issue in VSCode as well. Weird. I wonder how I would do the same thing with only vscode.

Upvotes: 0

Related Questions