其他模块中的类在启动时会抛出错误 - Python

时间:2018-01-24 15:57:19

标签: python

我有一个非常讨厌的问题,我有3个python文件:我的doctree看起来像这样:

/index.py
/system [dir]
    system/system.py
    system/path.py
    system/__init__.py

文件内容:

index.py:

import system.system

input_str = ""
command = ""

sys = System()
path = sys.Path()

def loop():
    global path, input_str

    input_str = raw_input(path.val + " > ")

    format_str(input_str)


def format_str(input_str):
    global command
    command = ""

    for i in range(len(input_str)):
        if (input_str[i] == " "):
            break
        else:
            command += input_str[i]

    command = command.lower()

    if (command == "exit"):
        exit()
    else:
        interpret()

def interpret():
    global command
    if (command == "cd"):
        path.changeDir()

loop()

system.py:

class System:
    version = "0.1.0b"
    inf = ""

    def __init__(self):
        self.inf = open("../inf.txt").read()

    def inf():
        print version

path.py:

import system

class Path(system.System):
    val = "/"
    path = Path()

    def __init__(startDir):
        global val
        val = startDir

    def getPath():
        print val

基本上,我想做的就是将系统类作为一个名为sys的变量启动,并从System扩展Path类(使它成为一个子类)并将这两个文件放在一个单独的目录中,这样就更整洁了,现在当我导入system.system时,我假设我正在系统包中导入系统模块,但后来我调用了System()并得到了这个错误:

Traceback (most recent call last):
    File "index.py", line 6, in <module>
        sys = System()
NameError: name 'System' is not defined

当我从import语句中删除最后一个.system并导入两个模块时,我得到了同样的错误。无论如何,这是一个很大的混乱,我不知道如何处理它,我开始使用谷歌搜索Python类,似乎我正在做的一切正确,但我得到这些错误,请帮助。

  • 关于雅各布

1 个答案:

答案 0 :(得分:0)

您未在System上正确导入index.py课程。

你应该这样做:

from system.system import System

而不是

import system.system