Installing python modules on Ubuntu

88,384

Solution 1

There are two nice ways to install Python packages on Ubuntu (and similar Linux systems):

sudo apt-get install python-pygame

to use the Debian/Ubuntu package manager APT. This only works for packages that are shipped by Ubuntu, unless you change the APT configuration, and in particular there seems to be no PyGame package for Python 3.

The other option is to use PIP, the Python package manager:

sudo apt-get install python3-pip

to install it, then

sudo pip3 install pygame

to fetch the PyGame package from PyPI and install it for Python 3. PIP has some limitations compared to APT, but it does always fetch the latest version of a package instead of the one that the Ubuntu packagers have chosen to ship.

EDIT: to repeat what I said in the comment, pip3 isn't in Ubuntu 12.04 yet. It can still be installed with

sudo apt-get install python3-setuptools
sudo easy_install3 pip
sudo apt-get purge python-pip

After this, pip is the Python 3 version of PIP, instead of pip3. The last command is just for safety; there might be a Python 2 PIP installed as /usr/bin/pip.

Solution 2

Try to install pip.

apt-get install python-pip
pip install pygame

Solution 3

You can use several approaches:

1 - Download the package by yourself. This is what I use the most. If the package follows the specifications, you should be able to install it by moving to its uncompressed folder and typing in the console:

python setup.py build
python setup.py install

2 - Use pip. Pip is pretty straightforward. In the console, you have to type:

pip install package_name

You can obtain pip here https://pypi.python.org/pypi/pip and install it with method 1

One thing to note: if you aren't using a virtualenv, you'll have to add sudo before those commands (not recommended)

Solution 4

It depends on the Ubuntu version and the IDE you are using. Ubuntu 15 and older come with Python 2.7 and Ubuntu 16.04 comes with both Python 2.7 and 3.5. Now based on the IDE you are using there are several ways to do this. Let`s say you only installed Spyder from Ubuntu app store or installed Jupyter. In other words you do not have a distribution like Anaconda or Enthought which install their own Python versions. This is important to pay attention to because once you are trying to install a package/library, you need to know which Python it is being installed to.

Now assuming you just have an IDE that is connected to Ubuntu`s default Python versions, you can use the terminal to install your packages:

For python 2.7 use

pip install libraryname

For python 3.5 use

pip3 install libraryname

Sometimes, for reasons that I don`t know, during the package installation process, Linux blocks access to the Python so try these as well:

sudo apt install python-libraryname

and for Python 3.5

sudo apt install python3-libraryname

These have helped me to install all the libraries that I need.

Now, if you are using a distribution like Aanaconda or Enthought, there is a good chance that the libraries that you are installing are not going to be added to the libraries that those distributions use. In order to install the libraries for these distributions, once you run the distribution, go to the ipython console and write

!pip install libraryname

In case of Enthought, it has it`s own Package Manager where it has most of the libraries you need and you can install them there without using pip or anything else.

Share:
88,384
Infamouslyuseless
Author by

Infamouslyuseless

i'm a 14 year old who enjoys programming, i'm not the best, but i'll try to help, i have a website where i post programs and stuff: http://infamouslyuseless.wix.com/infamouslyuseless

Updated on February 06, 2021

Comments

  • Infamouslyuseless
    Infamouslyuseless about 3 years

    I need to install some modules for python on Ubuntu Linux 12.04. I want pygame and livewires but I'm not sure how to install them.

    I have the py file for livewires, which has been specially edited (from a book I'm reading) and I want to install it but I'm not sure how to, I also want to install pygame.

  • Infamouslyuseless
    Infamouslyuseless over 10 years
    Does it automatically import it into my python file, so i can straight away start using pygame?
  • Fred Foo
    Fred Foo over 10 years
    @Infamouslyuseless: both options install packages into a location where you can import from, if you haven't done crazy things to your Python setup.
  • Infamouslyuseless
    Infamouslyuseless over 10 years
    It doesn't work, it says "E: unable to locate package python3-pip"
  • Infamouslyuseless
    Infamouslyuseless over 10 years
    "E: Invalid operation python3-setuptools" is what i got after i did "sudo apt-get python3-setuptools"...
  • tripleee
    tripleee almost 5 years
  • tripleee
    tripleee almost 5 years
    This should now be clearly marked as obsolete. The distribute installation wrapper now simply installs setuptools instead.