Installing PyMongo

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.

Getting ready

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.

How to do it…

  1. We will first set up PyMongo on the Windows platform. Visit https://pypi.python.org/pypi/pymongo and download the MS Windows Installer that is appropriate for the version of Python installed. My Python version is 2.7, and hence, this was the version downloaded.
  2. Double-click on the downloaded installer and click on Next, as shown in the following screenshot:
    How to do it…
  3. On clicking Next, if the right version of the Python installation is found, we can go ahead with the installation. With a couple more clicks on the Next button, we should have PyMongo installed.

Let's do a sanity test of the installation as follows:

  1. From the command prompt, start the Python shell by typing in 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.
    
  2. We will then import PyMongo; this should happen without any error. If we don't see any import error, our installation has gone through successfully:
    >>> 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:

  • Ubuntu's default repository might not have the latest release of the driver
  • The apt tool is specific to Debian Linux and its variants

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:

  • Installing pip, if it is not already installed on Ubuntu, using apt (it will be done in a different way on non-Debian variants)
  • Using pip to install PyMongo; this step is the same, irrespective of the flavor of Linux you are using

Let's start by installing pip on Ubuntu as follows:

  1. Execute the following command:
    sudo apt-get install python-pip
    
  2. Type in y to confirm the installation, and it will download the package.
  3. Now, install the package by executing the following command:
    amol@Amol-PC:~$ sudo apt-get install python-pip
    
  4. Once the setup is complete, execute the following command from the shell to install PyMongo:
    $ pip install pymongo
    
  5. This will install PyMongo. My Python version is the 2.7.x release. For the Python 3.x release, use pymongo3 as the package name instead:
    $ pip install pymongo3
    
  6. Once the installation of PyMongo is complete, we will do a quick sanity test. From the command prompt of the operating system, start the Python shell by typing in 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.
    
  7. We will then import PyMongo; this will happen without any error:
    >>> import pymongo
    >>> 
    
  8. We are now done with the installation of the PyMongo setup.

There's more…

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset