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:
/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".
/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:
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?
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.
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.
You can use
sudo apt-get install python3-library_name
Replace library_name
by any other library (e.g. scipy, pandas, numpy, matplotlib, etc.)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With