1
collection.py爲什麼我的Python腳本不能識別導入模塊中的類?
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
這些文件在同一目錄下。當我運行main.py
,但是,我得到一個錯誤:
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
從我讀過的東西,如果文件在同一目錄下,我需要做的是使用import collection
。
我錯過了什麼嗎?
看看MongoClient如何導入到collection.py – Daniel