I installed pip (pip 1.5.4 from /usr/lib/python2.7/dist-packages (python 2.7)). My python version is 2.7.

I installed pygame by apt-get install python-pygame and got a success message. But when I try to import it in python, getting the following message:

ashish@Ashish-Notebook:~$ python
Python 2.7.11 (default, Jun  6 2016, 07:21:54) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named pygame

Similarly, I installed Beautiful Soup by pip install BeautifulSoup and can't import it anywhere. Moreover when I again try to install, I get this message:

Requirement already satisfied (use --upgrade to upgrade): BeautifulSoup in /usr/local/lib/python2.7/dist-packages
Cleaning up...

Moreover when i get a list of installed packages (by pip list). I can see pygame and beautifulsoup in that list.

  • It is very surprising to me that your Python cannot import pygame that is installed successfully according to you. Can you do which python and python -c 'import sys; print(sys.path)'? – edwinksl Jul 14 '16 at 2:30
  • which python /usr/local/bin/python python -c 'import sys;print(sys.path)' ['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7', '/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old', '/usr/local/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/site-packages'] – A.Gautam Jul 14 '16 at 2:48
  • You installed your own Python? – edwinksl Jul 14 '16 at 2:53
  • Yes.I downloaded source and installed that. – A.Gautam Jul 14 '16 at 2:55

The issue is that when you install things with sudo apt-get install (or sudo pip install), they install to places in /usr, but the python you compiled from source got installed in /usr/local. The python you actually end up running when you type python at the prompt is the one you compiled (based on the output of the python -c 'import sys; print(sys.path)' command), and it can't find the python packages installed in the /usr directory. However, Ubuntu comes with python (both python 2 and python 3) already installed in the correct location, where it will find the installed packages. So a general recommendation is to try not to mix installation methods like this. Ubuntu has many software packages available through the official repositories which obviates the need to do much compiling yourself, and it's generally recommended to install software via apt when possible.

That said, for python packages (like Pygame and Beautiful Soup), things can get a bit more complicated. Mixing pip and apt-get can cause troubles down the road, so one approach to keep things simple is to stick with just the system package manager. If you end up needing to use pip because the package manager doesn't have things you need (or up to date versions), it's better to work within virtual environments and use pip for all the python packages you need within that virtual environment.

To tell your system to find the default system python correctly (python 2, which is the default when you just call python), try rewriting the symlink that routes the python command at a prompt to the original destination:

sudo ln -s /usr/bin/python2.7 /usr/bin/python

Hopefully that should allow it to find the python packages you installed.

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.