9

I am running Ubuntu 13.10. I have python 2.7.5 with anaconda 1.8.0. Whenever I start python and type import pygame it tells me that there is no module named pygame.

How can I install pygame?

  • 1
    Did you try using sudo apt-get install python-pygame? – jobin Jan 4 '14 at 6:36
  • yes,i did try it – saisanjeev Jan 4 '14 at 9:53
  • Were any of the answers helpful? – jobin Feb 13 '14 at 20:12
  • yeah,I did not look at the version no. properly.I typed in 13.04 but it was actually 12.04.I upgraded it and it worked! – saisanjeev Feb 16 '14 at 11:47
18

The reason you are not able to import pygame into your python interpreter is because you are installing it for python2 and starting the python3 interpreter(Ubuntu 13.10 uses python3 by default).

To install pygame for python2(your version is 2.7.5), do this on a terminal(Ctrl+Alt+t):

sudo apt-get install python-pygame

and then start the python2 interpreter

python2

and then try to import pygame as

import pygame

To set python2 as your default interpreter, you can set an alias in your bash_aliases file. To do this, open a terminal and type:

nano ~/.bash_aliases

This may open an empty file, depending upon whether you have set an alias before and then type

alias python='python2'

Do not change symlinks in /usr/bin/python3, since this may break many application in Ubuntu 13.10 which use python3 vociriferously.

  • I use python with anaconda 1.8.0.So whenever I start python,it starts with anaconda – saisanjeev Jan 5 '14 at 9:41
  • got it thanks everyone and special thanks to you Jobin. – saisanjeev Jan 6 '14 at 5:25
  • @saisanjeev: Either accept the answer as it helped you(if it did), else please post the answer that helped you and mark as accepted. – jobin Jan 6 '14 at 8:42
  • Note, to install pygame in python3 you have to build it from the source code. Following the simple instructions here (askubuntu.com/a/406410/327339), plus installing the one extra dependency before building (see my comment below the answer), worked for me in Xubuntu 16.04 LTS with Python 3.5.2. – Gabriel Staples Sep 14 '16 at 19:16
2

I can confirm Jobin's suggestion in his comment:

sudo apt-get install python-pygame

Works flawlessly. Tested on 13.10.

Next time if you want to install a python package you can search whether it is in the repositories by running:

apt-cache search pygame

Then you can see the packages (if they are in the repositories) and their names. The output for apt-cache search pygame is

lightyears - single player real-time strategy game with steampunk sci-fi
psychopy - environment for creating psychology stimuli in Python
pyntor - flexible and componentized presentation program
python-pygame - SDL bindings for games development in Python
python-pyglet - cross-platform windowing and multimedia library
python-soya - high level 3D engine for Python
python-soya-dbg - high level 3D engine for Python - debug extension
python-soya-doc - high level 3D engine for Python
solarwolf - Collect the boxes and don't become mad

In this list you can select the package (here: python-pygame) and run sudo apt-get install the-package-name.

For more debugging of dependencies see Installing pygame with pip.

  • yeah,it's the same thing i get.But,when i type 'python' in the Terminator or terminal,I get this:-'Python 2.7.6 |Anaconda 1.8.0 (32-bit)| (default, Nov 11 2013, 10:47:19) [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2 Type "help", "copyright", "credits" or "license" for more information.'Then after the 3 arrows, I typed :'import pygame' and it showed this error:-'Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named pygame'.Why is this so?Maybe because i use anaconda python? – saisanjeev Jan 4 '14 at 9:59
  • I don't see why that would be the problem. Are you typing 'import pygame' or import pygame (without the '')? – don.joey Jan 4 '14 at 10:03
  • I am typing just 'import pygame' – saisanjeev Jan 5 '14 at 9:47
  • I feel its because i have anaconda 1.8.0 – saisanjeev Jan 5 '14 at 9:48
0

This should install pygame for python 3, as python3-pip is required for pygame.

 sudo apt-get install python3-pip python3-dev  
 sudo pip3 install pygame
0

Install Pygame for Python 3

To install PyGame for Python 3.x on Ubuntu 19.04 and later open the terminal and type:

sudo apt install python3-pygame

To install python3-pygame in Ubuntu 18.10 open the terminal and type:

sudo nano /etc/apt/sources.list  

Add this line to sources.list.

deb http://archive.ubuntu.com/ubuntu/ cosmic-proposed universe # for 19.04 replace cosmic with disco 

Save sources.list with the keyboard combination Ctrl+O and press Enter and exit with Ctrl+X

Update the list of available software and install python3-pygame.

sudo apt update  
sudo apt install python3-pygame  

Install Pygame for Python 2

To install python-pygame in all currently supported versions of Ubuntu open the terminal and type:

sudo apt install python-pygame

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.