pythonでMongoDBを操作するpymongo

MongoDBの勉強でさくらVPS上にMongoDBをインストールして
pythonで操作をしようと思いました。
MongoDBをPythonで操作する(PyMongo使用)PythonからMongoDBを使ってみるを参考にしようとしたときに引っかかったのでメモしておきます。

まず、MongoDBをインストールしただけではプログラム上から操作できません。

最初にMongoDBをPythonで操作する(PyMongo使用)を使用しようと思って
ソースをコピーして実行した時に

$ python2.4 mongodb_insert.py
Traceback (most recent call last):
File "mongodb_insert.py", line 5, in ?
import pymongo
ImportError: No module named pymongo

この様にモジュールがないと怒られてしまいました。
pymongoで検索した際にこの2つが上に上がってきたので
使ってみようとしたのですが、モジュールのインストール方法は書かれていませんでした。

なので私が実行した手順を記事にします。

さくらVPSCentOS5.6の64bit版での実装です

MongoDBをPythonで操作する(PyMongo使用)では

# easy_install pymongo
と書かれていましたが、easy_installというコマンドが入っていなかったので
yumでインストールしようお思いました。きっとあるだろうと・・・w

MongoDBは10genレポジトリを利用してインストールしていたので
いろんな種類のパッケージがあるremi,epelレポジトリとともに検索をかけました

#  yum list --enablerepo=remi --enablerepo=epel  --enablerepo=10gen|grep pymongo
pymongo.x86_64 1.9-8.el5 epel
pymongo-gridfs.x86_64 1.9-8.el5 epel

したら上の方がちょうど使用するやつなのでインストールします。

#  yum install --enablerepo=remi --enablerepo=epel  --enablerepo=10gen pymongo
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* epel: ftp.kddilabs.jp
* remi: remi-mirror.dedipower.com
Setting up Install Process
Resolving Dependencies

    • > Running transaction check
      • > Package pymongo.x86_64 0:1.9-8.el5 set to be updated
    • > Processing Dependency: python-bson = 1.9-8.el5 for package: pymongo
    • > Running transaction check
      • > Package python-bson.x86_64 0:1.9-8.el5 set to be updated
    • > Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================
Package Arch Version Repository Size
============================================================================================================================================
Installing:
pymongo x86_64 1.9-8.el5 epel 153 k
Installing for dependencies:
python-bson x86_64 1.9-8.el5 epel 53 k

Transaction Summary
============================================================================================================================================
Install 2 Package(s)
Upgrade 0 Package(s)

Total download size: 206 k
Is this ok [y/N]: y
Downloading Packages:
(1/2): python-bson-1.9-8.el5.x86_64.rpm | 53 kB 00:00
(2/2): pymongo-1.9-8.el5.x86_64.rpm | 153 kB 00:00

                                                                                                                                                                                                                                                                                      • -

Total 588 kB/s | 206 kB 00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : python-bson 1/2
Installing : pymongo 2/2

Installed:
pymongo.x86_64 0:1.9-8.el5

Dependency Installed:
python-bson.x86_64 0:1.9-8.el5

Complete!

これでpymongoのインストールが完了しました。