简体   繁体   中英

How can I fix the ImportError ("No module named pygame") on executing a python file?

After typing SnakeGame.py I am getting the error:

Traceback (most recent call last): File "SnakeGame.py", line 1, in import pygame ImportError: No module named pygame).

Pls give me a solution on how to solve this, I have installed pygame also.

thanks in advance.

That error means that the python interpreter isn't able to find the pygame module.
Are you using a virtual environment? If you are using an IDE like PyCharm, try to install the pygame module inside the IDE console.

pip install pygame

install pygame in CMD write pip install pygame if you use visual studio code go to market and type pygame then install it ...

enter image description here

visual studio code

I had this same problem a few days ago. My solution was to use a virtual environment. To do this, open a terminal in your folder and use the following commands: 1.) pip install pipenv 2.) pipenv shell 3.) pipenv install pygame

After this, you need to select the correct python interpreter. In VS Code, hit ctrl+shift+p Then click the correct interpreter (should have the name of the folder). Then try to run the file. If that doesn't work, try to run this in the terminal: pipenv run {name of the python file, example: app.py}

Hope that works for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM