Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

module

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?

like image 462
Jay Avatar asked Aug 09 '12 23:08

Jay


People also ask

How do I install a third party module in Python?

The primary way to install third-party modules is to use Python's pip tool. This tool securely downloads and installs Python modules onto your computer from https://pypi.python.org/, the website of the Python Software Foundation. PyPI, or the Python Package Index, is a sort of free app store for Python modules.

Can I use pip install in Ubuntu?

Pip is a helpful command line package manager and installer for Ubuntu. Using various commands, pip allows you to manage Python software packages from the Ubuntu terminal. In this tutorial, you have learned how to install pip on Ubuntu machines running both Python 2 and Python 3.


2 Answers

You can use

sudo apt-get install python3-library_name

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

like image 88
Logamou Seknewna Lema Avatar answered Sep 28 '22 09:09

Logamou Seknewna Lema


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

like image 27
Daniel Nouri Avatar answered Sep 28 '22 09:09

Daniel Nouri