Jay
Jay

Reputation: 233

Installing 3rd party Python modules on an Ubuntu Linux machine?

I'm guessing my question is pretty basic, but after 15-20 minutes on Google and YouTube, I am still a little fuzzy. I am relatively new to both Linux and Python, so I am having some difficulty comprehending the file system tree (coming from Windows).

From what I've found digging around the directories in Ubuntu (which is version 12.04, I believe, which I am running in VBox), I have ID'd the following two directories related to Python:

  1. /usr/local/lib/python2.7 which contains these two subdirectories:

    dist-packages
    site-packages

    both of which do not show anything when I type "ls" to get a list of the files therein, but show ". .." when I type "ls -a".

  2. /usr/lib/python2.7 which has no site-packages directory but does have a dist-packages directory that contains many files and subdirectories.

So if I want to install a 3rd party Python module, like, say, Mechanize, in which one of the above directories (and which subdirectory), am I supposed to install it in?

Furthermore, I am unclear on the steps to take even after I know where to install it; so far, I have the following planned:

  1. Download the tar.gz (or whatever kind of file the module comes in) from whatever site or server has it
  2. Direct the file to be unzipped in the appropriate subdirectory (one of the 2 listed above)
  3. Test to make sure it works via import mechanize in interactive mode.

Lastly, if I want to replace step number 1 above with a terminal command (something like sudo apt-get), what command would that be, i.e., what command via the terminal would equate to clicking on a download link from a browser to download the desired file?

Upvotes: 13

Views: 67231

Answers (5)

Logamou Seknewna Lema
Logamou Seknewna Lema

Reputation: 59

You can use

sudo apt-get install python3-library_name

Replace library_name by any other library (e.g. scipy, pandas, numpy, matplotlib, etc.)

Upvotes: 4

theBuzzyCoder
theBuzzyCoder

Reputation: 2880

To install nay python package in ubuntu, first run sudo apt-get update

Then type "sudo apt-get install python-" and press tab twice repeatedly. press y or yes and it will display all the packages available for python. Then again type sudo apt-get install python-package It will install the package from the internet.

Upvotes: 0

Daniel Nouri
Daniel Nouri

Reputation: 1274

virtualenv is the de facto Python standard for installing third party library cleanly. Read more about it here: http://www.virtualenv.org/

Usage example:

daniel@redhotcar:~/tmp$ virtualenv myenv
New python executable in myenv/bin/python
Installing distribute....................................................................................................................................................................................done.
Installing pip...............done.
daniel@redhotcar:~/tmp$ cd myenv/
daniel@redhotcar:~/tmp/myenv$ bin/pip install mechanize
Downloading/unpacking mechanize
  Downloading mechanize-0.2.5.zip (445Kb): 445Kb downloaded
  Running setup.py egg_info for package mechanize

Installing collected packages: mechanize
  Running setup.py install for mechanize

Successfully installed mechanize
Cleaning up...
daniel@redhotcar:~/tmp/myenv$ bin/python
Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mechanize
>>> mechanize
<module 'mechanize' from '/home/daniel/tmp/myenv/local/lib/python2.7/site-packages/mechanize/__init__.pyc'>
>>> 

On Ubuntu, install virtualenv via apt-get install python-virtualenv

Upvotes: 10

Burhan Khalid
Burhan Khalid

Reputation: 174624

You aren't supposed to manually install anything.

There are three ways to install Python libraries:

  1. Use apt-get, aptitude or similar utilities.
  2. Use easy_install or pip (install pip first, its not available by default)
  3. If you download some .tar.gz file, unzip it and then type sudo python setup.py install

Manually messing with paths and moving files around is the first step to headaches later. Do not do it.

For completeness I should mention the portable, isolated way; that is to create your own virtual environment for Python.

  1. Run sudo apt-get install python-virtualenv
  2. virtualenv myenv (this creates a new virtual environment. You can freely install packages in here without polluting your system-wide Python libraries. It will add (myenv) to your prompt.)
  3. source myenv/bin/activate (this activates your environment; making sure your shell is pointing to the right place for Python)
  4. pip install _____ (replace __ with whatever you want to install)
  5. Once you are done type deactivate to reset your shell and environment to the default system Python.

Upvotes: 11

Joran Beasley
Joran Beasley

Reputation: 113978

use setuptools http://pypi.python.org/pypi/setuptools/ then type

pip install <somePackageName>

or

easy_install <somePackageName>

they will look in the pypi directories (on the interwebs) for the package and will install the correct version for you automagically...

Upvotes: 1

Related Questions