Setting up a Python Environment with MongoDB

In order to be able to connect to MongoDB with Python, you need to install the PyMongo driver package. In Python, the best practice is to create what is known as a “virtual environment” in which to install your packages. This isolates them cleanly from any “system” packages you have installed and yields the added bonus of not requiring root privileges to install additional Python packages. The tool to create a “virtual environment” is called virtualenv.

There are two approaches to installing the virtualenv tool on your system—manually and via your system package management tool. Most modern UNIX-like systems will have the virtualenv tool in their package repositories. For example, on Mac OS X with Mac Ports, you can run sudo port install py27-virtualenv to install virtualenv for Python 2.7. On Ubuntu you can run sudo apt-get install python-virtualenv. Refer to the documentation for your OS to learn how to install it on your specific platform.

In case you are unable or simply don’t want to use your system’s package manager, you can always install it yourself, by hand. In order to manually install it, you must have the Python setuptools package. You may already have setuptools on your system. You can test this by running python -c import setuptools on the command line. If nothing is printed and you are simply returned to the prompt, you don’t need to do anything. If an ImportError is raised, you need to install setuptools.

To manually install setuptools, first download the file http://peak.telecommunity.com/dist/ez_setup.py

Then run python ez_setup.py as root.

For Windows, first download and install the latest Python 2.7.x package from http://www.python.org. Once you have installed Python, download and install the Windows setuptools installer package from http://pypi.python.org/pypi/setuptools/. After installing Python 2.7 and setuptools, you will have the easy_install tool available on your machine in the Python scripts directory—default is C:\Python27\Scripts\.

Once you have setuptools installed on your system, run easy_install virtualenv as root.

Now that you have the “virtualenv” tool available on your machine, you can create your first virtual Python environment. You can do this by executing the command virtualenv --no-site-packages myenv. You do not need—and indeed should not want—to run this command with root privileges. This will create a virtual environment in the directory “myenv”. The --no-site-packages option to the “virtualenv” utility instructs it to create a clean Python environment, isolated from any existing packages installed in the system.

You are now ready to install the PyMongo driver.

With the “myenv” directory as your working directory (i.e. after “cd myenv”), simply execute bin/easy_install pymongo. This will install the latest stable version of PyMongo into your virtual Python environment. To verify that this worked successfully, execute the command bin/python -c import pymongo, making sure that the “myenv” directory is still your working directory, as with the previous command.

Assuming Python did not raise an ImportError, you now have a Python virtualenv with the PyMongo driver correctly installed and are ready to connect to MongoDB and start issuing queries!

Get MongoDB and Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.