This recipe is about setting up PyMongo, which is the Python driver for MongoDB. In this recipe, we will demonstrate the installation of PyMongo on both the Windows and Linux platforms.
A simple single node is what we will need for the sanity testing of the driver, once the installation is complete. Refer to the Single node installation of MongoDB recipe in Chapter 1, Installing and Starting the MongoDB Server, to learn how to start the server. We will also require an Internet connection to download Python and PyMongo. Once these prerequisites are met, we are ready to begin.
The first step is to install Python on the computer if it is not already there. Visit http://www.python.org/getit/, download the latest version of Python for your platform, and install it. The steps for the installation Python are not covered in this recipe. However, before you proceed to the next section, Python should be available on the host operating system.
Let's do a sanity test of the installation as follows:
python
as follows:C:UsersAmol>python Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo >>>
In this section, we will see how to set up PyMongo on a Linux system. We will install it on a Debian flavor, Ubuntu. We will not use Ubuntu's advanced packaging tool (apt) to install PyMongo for a couple of reasons:
Therefore, we will use pip
, a tool to manage Python packages. This tool uses Python Package Index (PyPI) to retrieve the dependencies; this is the official repository for third-party libraries in Python.
So, our installation is split into sections as follows:
pip
, if it is not already installed on Ubuntu, using apt (it will be done in a different way on non-Debian variants)pip
to install PyMongo; this step is the same, irrespective of the flavor of Linux you are usingLet's start by installing pip
on Ubuntu as follows:
sudo apt-get install python-pip
y
to confirm the installation, and it will download the package.amol@Amol-PC:~$ sudo apt-get install python-pip
$ pip install pymongo
pymongo3
as the package name instead:$ pip install pymongo3
python
:amol@Amol-PC:~$ python Python 2.7.3 (default, Apr 10 2013, 05:46:21) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo >>>
Installation of PyMongo is just a prerequisite for running Python code that can connect to Mongo to perform the operations. The next couple of recipes, Executing query and insert operations using PyMongo and Executing update and delete operations using PyMongo, are all about demonstrating these basic operations in Python programming using PyMongo to connect to the database and execute them.