I already installed pygame for Python3 (on windows)
But now I want to run a source code written with Python2.7
It gives this error when I run the code :
ImportError: No module named pygame
how can i use pygame for python2.7?(how can i fix it?)
thanks
CodePudding user response:
I just did it, it is pretty straight forward. I believe it is similar in any OS since we're talking about python. But I did on Linux, since it is the OS I use most part of the time.
- Have Python 2 installed (On windows you can get the installer here: https://www.python.org/download/releases/2.7/), on Linux (Debian like):
sudo apt-get update
sudo apt-get install python2.7
- Have pip installed (probably on Windows it will be installed alongside with python 2 on the installer, if not you can also follow the directions down bellow).
But on Linux, have your terminal, then:
- Run this to download the pip installer for Python 2.7 (on Windows probably wget will work only on powershell, or paste the url in your browser and you can download it)
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
- Then this to install it:
sudo python2 get-pip.py
- And at last you can effectively install it:
pip2 install pygame
Finally check it importing in python, in the terminal:
$ python2.7
You'll be able to see python 2 open:
Python 2.7.18 (default, Mar 8 2021, 13:02:45)
Then try importing pygame:
import pygame
and you should see:
pygame 2.0.3 (SDL 2.0.16, Python 2.7.18)
Hello from the pygame community. https://www.pygame.org/contribute.html
CodePudding user response:
I solved my problem by using pip for different versions of Python
> py -2 -m pip install pygame
> py -3 -m pip install pygame