0

I have tried to see every possible solution for this but for some reason none of them have worked. I am doing Learn Python the Hard way exercise 49. I made my own parser and it works when I run it via pycharm, when i open the console or powershell and try to import, it gives this error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'parse_sentence' is not defined

I also tried to see if the problem is in the script, so I copied his work down onto another tab and it gave me the same problem.

Here a little bit of my code:

class Sentence:

def __init__(self, type):
    self.type = type


def subjects(self):
    nouns = ('door', 'bear', 'princess', 'cabinet')

    s = self.scan(self.type)
    for i in s:
        for k in i:
            if k in nouns:
                print k

def verbs(self):
    verbs = ('go', 'stop', 'kill', 'eat', 'open')
    s = self.scan(self.type)
    for i in s:
        for k in i:
            if k in verbs:
                print k

def objects(self):
    directions = ('north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back')
    nouns = ('door', 'bear', 'princess', 'cabinet')
    s = self.scan(self.type)
    for i in s:
        for k in i:
            if k in directions or k in nouns:
                print k

def get_tuple(self, word):

    directions = ('north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back')
    verbs = ('go', 'stop', 'kill', 'eat', 'open')
    stop_words = ('the', 'in', 'of', 'from', 'at', 'it')
    nouns = ('door', 'bear', 'princess', 'cabinet')

    lowercased = word.lower()

    if lowercased in directions:
        return ('direction', lowercased)
    elif lowercased in verbs:
        return ('verb', lowercased)
    elif lowercased in stop_words:
        return ('stop_words', lowercased)
    elif lowercased in nouns:
        return ('noun', lowercased)
    elif lowercased.isdigit():
        return ('number', int(lowercased))
    else:
        return ('error', word)


def scan(self, sentence):
    words = sentence.split()
    return [self.get_tuple(word) for word in words]

it is not because of the self.scan, it is in the class I just don't want to post all of the code to not mess the page up. I open console, i type import parser (parser is the name of this file) that works, then it i type myarg = Sentence('let us kill the bear'). gives me the error up above. Samehting when I do his way, thank you so much for reading this in advance.

I am on Windows 10, Python 2.7

Here is my error

import parser
myarg = Sentence('let us kill the bear')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'Sentence' is not defined

when doing import paser parser.Sentence()

I get :

Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Sentence'

when doing :

import parser
myarg = parser.Sentence('let us kill the bear')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Sentence'
15
  • I will add the rest of the code if anyone needs it Commented Dec 31, 2016 at 0:49
  • always in question show full error message (Traceback) There are other usefull information - ie. which line makes problem. I don't see parse_sentence in your code. Commented Dec 31, 2016 at 0:52
  • Hello furas, thank you for replying. I aplogoize I forgot to copy my error. That was the error i got when trying to do Zed Shaw's parser example. here is the full code : Commented Dec 31, 2016 at 0:56
  • import parser myarg = Sentence('let us kill the bear') Traceback (most recent call last): File "<input>", line 1, in <module> NameError: name 'Sentence' is not defined Commented Dec 31, 2016 at 0:56
  • 1
    LPTHW is terrible, by the way. Commented Dec 31, 2016 at 0:57

1 Answer 1

1

If you import

import parser

then you have to use parser.Sentence()

myarg = parser.Sentence('let us kill the bear')

If you import

from parser import Sentence

then you can use Sentence()

myarg = Sentence('let us kill the bear')

Python first looks for file in "current working directory" ("cwd"). If you run code in different folder then it can import from library module with the same name instead of your file.

You can check what file was imported

 import parser

 print(parser.__file__)

To check current working directory

 import os

 print(os.getcwd())
Sign up to request clarification or add additional context in comments.

5 Comments

when trying to do from parser import Sentence, it says can not import name Sentence
do you have class Sentence in file parser.py ?
would you like me to post more?
Okay i just did it
Also @Omar, you don't have to call us sir. You welcome to just call us by our usernames. eg. furas instead of sir or leaf instead of sir :)

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.