0

I'm building a python app, and I'm trying to get my index.py file to import my guestbookDOA.py file. When I run the index file, I get an import error saying:

line 3, No module named guestbookDAO

Here is my index.py file:

import bottle
import pymongo
import guestbookDAO

#This is the default route, our index page.  Here we need to read the documents from MongoDB.
@bottle.route('/')
def guestbook_index():
    mynames_list = guestbook.find_names()
    return bottle.template('index', dict(mynames = mynames_list))

#We will post new entries to this route so we can insert them into MongoDB
@bottle.route('/newguest', method='POST')
def insert_newguest():
    name = bottle.request.forms.get("name")
    email = bottle.request.forms.get("email")
    guestbook.insert_name(name,email)
    bottle.redirect('/')


#This is to setup the connection

#First, setup a connection string. My server is running on this computer so localhost is OK
connection_string = "mongodb://localhost"
#Next, let PyMongo know about the MongoDB connection we want to use.  PyMongo will manage the connection pool
connection = pymongo.MongoClient(connection_string)
#Now we want to set a context to the names database we created using the mongo interactive shell
database = connection.names
#Finally, let out data access object class we built which acts as our data layer know about this
guestbook = guestbookDAO.GuestbookDAO(database)

bottle.debug(True)
bottle.run(host='localhost', port=8082) 

And here is my guestbookDAO.py file:

import string

class GuestbookDAO(object):

#Initialize our DAO class with the database and set the MongoDB collection we want to use
    def __init__(self, database):
        self.db = database
        self.mynames = database.mynames

#This function will handle the finding of names
    def find_names(self):
        l = []
        for each_name in self.mynames.find():
            l.append({'name':each_name['name'], 'email':each_name['email']})

        return l

#This function will handle the insertion of names
    def insert_name(self,newname,newemail):
        newname = {'name':newname,'email':newemail}
        self.mynames.insert(newname)
  • 2
    What's important is the location of `guestbookDAO.py`. Is it in the same directory as `index.py`, or is it located in a directory listed in `sys.path`? – mhawke Jul 21 '15 at 02:16
  • Need more details about your question. Maybe this is helpful. http://stackoverflow.com/questions/9439480/from-import-vs-import – BAE Jul 21 '15 at 02:19
  • It's in the same directory. – Kristine Rooks Jul 21 '15 at 02:19
  • Since it's in the same directory: are you running Python 2 or Python 3? Python 3 doesn't allow implicit relative imports anymore, and this type of import will fail in Python 3. –  Jul 21 '15 at 04:22
  • It's probably Python 3. How do you do imports in Python 3? – Kristine Rooks Jul 21 '15 at 15:02

0 Answers0