1

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?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    Do you also have a folder named "lexicon" in the same path? Anecdotally, you may find the [builtin `unittest`](https://docs.python.org/3/library/unittest.html#basic-example) and [`coverage.py`](https://coverage.readthedocs.io/) (together) to be cleaner and friendlier than nose, which is [no longer developed](https://nose.readthedocs.io/en/latest/). – ti7 Apr 05 '21 at 14:17
  • Thanks a lot. You are right – Sergio Iliev Apr 05 '21 at 14:27
  • 1
    Does this answer your question? [Python Import Class With Same Name as Directory](https://stackoverflow.com/questions/16245106/python-import-class-with-same-name-as-directory) – ti7 Apr 05 '21 at 14:37

1 Answers1

1

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

ti7
  • 16,375
  • 6
  • 40
  • 68