Search code examples
pythonattributeerrornose

Module * has no attribute * using nosetests


I do the task from "Python Learn Hard Way" book. It is about using tests with nose. I have the function scan_net in lexicon.py file:

def scan_net(sentence):
    direction = ['north', 'south', 'east', 'west']
    verb = ['go', 'kill', 'eat', 'breath']
    stop_words = ['the', 'in', 'off']
    nouns = ['bear', 'princess', 'frog']

    words = sentence.split()
    result = []
    for i in range(len(words)):
        if words[i] in direction:
            result.append(('direction', words[i]))
        elif words[i] in verb:
            result.append(('verb', words[i]))
        elif words[i] in stop_words:
            result.append(('stop_words', words[i]))
        elif words[i] in nouns:
            result.append(('nouns', words[i]))
        #check for number and if it is go out of the loop using continue
        try:
            if(int(words[i])):
                result.append(('number', words[i]))
                continue
        except ValueError:
            pass
        else:
            result.append(('error', words[i]))
    return result

And this is my tests file:

from nose.tools import *
import lexicon

def test_directions():
    result = lexicon.scan_net("north south east")
    assert_equal(result, [('direction', 'north'),
                        ('direction', 'south'),
                        ('direction', 'east')])

After I run nosetests tests\lexicon_tests.py command I get AttributeError:module 'lexicon' has no attribute 'scan_net'.

What is wrong with my import? Why it does not see the function scan_net?


Solution

  • You may have a folder named lexicon in your path which is being preferred and should rename one or the other to make it clearer which should be imported.

    You should still be able to import with from lexicon import scan_net, but generally having different names will make your life easier.

    See Python Import Class With Same Name as Directory