Pycharm一直给出名称'class'没有定义错误

时间:2016-12-31 00:49:22

标签: python

我试图看到每种可能的解决方案,但由于某些原因,它们都没有奏效。我正在做以艰难的方式学习Python 练习49.我创建了自己的解析器,当我通过pycharm运行它时,当我打开控制台或powershell并尝试导入时,它会产生以下错误:

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

我还试图查看问题是否在脚本中,所以我将他的工作复制到另一个选项卡上,它给了我同样的问题。

这里有一点我的代码:

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]

它不是因为self.scan,它在类中我只是不想发布所有代码而不会弄乱页面。我打开控制台,输入import parser(解析器是此文件的名称),然后输入myarg = Sentence('let us kill the bear')。给我上面的错误。 Samehting,当我按照自己的方式行事时,非常感谢你提前阅读此内容。

我在Windows 10,Python 2.7

这是我的错误

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

做的时候     导入paser     parser.Sentence()

我明白了:

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

做的时候:

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'

1 个答案:

答案 0 :(得分:0)

如果您导入

import parser

然后你必须使用parser.Sentence()

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

如果您导入

from parser import Sentence

然后您可以使用Sentence()

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

Python首先在“当前工作目录”(“cwd”)中查找文件。如果您在不同的文件夹中运行代码,那么它可以使用相同的名称而不是您的文件从库模块导入。

您可以查看导入的文件

 import parser

 print(parser.__file__)

检查当前工作目录

 import os

 print(os.getcwd())