1

collection.py

import sys
import os
import pymongo
from pymongo import MongoClient

class Collection():
    """returns a collection curser from mongodb"""

    client = MongoClient()

    def __init__(self, db, collection_name):
        self.db = db
        self.collection_name = collection_name

    def getCollection(self):
        data_base = getattr(self.client, self.db)
        collObject = getattr(data_base, self.collection_name)
        return collObject

main.py

import sys
import os
import collection

def main():
    pass

if __name__ == '__main__':
    print"Begin Main"

    agents = Collection('hkpr_restore','agents')
    print "agents is" , agents

These files are in the same directory. When I run main.py, however, I get an error:

Begin Main
Traceback (most recent call last):
  File "main.py", line 23, in <module>
    agents = Collection('hkpr_restore','agents')
NameError: name 'Collection' is not defined

From what I've read, if the files are in the same directory, all I need to do is use import collection.

Am I missing something?

1
  • look how MongoClient is imported into collection.py Commented May 3, 2015 at 8:56

1 Answer 1

5

You've only imported collection, not Collection.

Either do from collection import Collection, or use the full qualified name when instantiating: agents = collection.Collection('hkpr_restore','agents').

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.