Reputation: 33
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'
Upvotes: 0
Views: 1735
Reputation: 142631
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())
Upvotes: 1