Python3.6 |-使用__import__-|

时间:2018-08-05 02:35:02

标签: python terminal python-3.6 python-import

我正在创建一个非常基本的程序,该程序可以使用Python3.6模拟终端,其名称为Prosser(起源是“ Prosser”听起来像是“ Processer”,而Prosser是命令处理器)。

命令导入存在一个问题,即所有,这些命令存储在名为“ lib ”的文件夹中在Prosser的根文件夹中及其内部可以包含文件夹和文件,如果文件夹位于 lib 目录中,则可以不再命名为文件夹,其名称现在为 package (但这暂时不适用)。

程序的接口只是写的输入:

Prosser:/home/pedro/documents/projects/prosser/-!

,用户可以在文本前键入命令,就像普通终端一样:

Prosser:/home/pedro/documents/projects/prosser/-! console.write Hello World

假设在“ lib ”文件夹中存在一个名为“ 控制台 ”的文件夹有一个名为“ write.py ”的文件,其代码为:

class exec:
    def main(args):
        print(args)

如您所见,前两行就像一个重要的命令执行结构:“ exec ”类是命令执行的主要类,而def“ main “ strong>”是终端将要读取和执行的主要功能,同时还会传递用户定义的参数,此后该命令将负责捕获任何错误并执行将要创建的操作。

此刻一切正常,但现在出现了 U 伙计们需要的真正帮助,命令导入。

就像我写的那样,用户可以键入任何命令,在上面的示例中,我键入了“ console.write Hello World ”,存在一个名为“ 控制台”的文件夹和一个名为“ write.py ”的文件。关键是可以用“点”来定义软件包,这是:

-! write Hello World

上面我只键入了“ ”,这表明该文件仅位于“ lib ”文件夹中,而没有具有要存储分离的软件包,因此它是 Freedom命令(该命令没有软件包或节点)。< / p>

-! console.write Hello World

现在,我在“ console.write ”上方键入内容,这表明该文件具有要打包或打包并存储的包或节点,这意味着它是一个附加命令(具有包或节点的命令)。

因此,文件与文件包之间用一个点分隔,您放置的点越多,将导航更多的文件夹以找到该文件并继续下一次执行。

代码

最后是代码。我使用 import 语句对此进行了尝试:

import os
import form

curDir = os.path.dirname(os.path.abspath(__file__)) # Returns the current direrctory open

while __name__ == "__main__":
    c = input('Prosser:%s-! ' % (os.getcwd())).split() # Think the user typed "task.kill"
    path = c[0].split('.') # Returns a list like: ["task", "kill"]
    try:
        args = c[1:] # Try get the command arguments
        format = form.formater(args) # Just a text formatation like create a new line with "$n"
    except:
        args = None # If no arguments the args will just turn the None var type
        pass 

     if os.path.exists(curDir + "/lib/" + "/".join(path) + ".py"): # If curDir+/lib/task/kill.py exists
         module = __import__("lib." + '.'.join(path)) # Returns "lib.task.kill"
         module.exec.main(args) # Execute the "exec" class and the def "**main**"
     else:
         pathlast = path[-1] # Get the last item, in this case "kill"
         path.remove(path[-1]) # Remove the last item, "kill"
         print('Error: Unknow command: ' + '.'.join(path) + '. >>' + pathlast + '<<') # Returns an error message like: "Error: Unknow command: task. >>kill<<"
    # Reset the input interface

问题在于,当执行“ module = __import __(“ lib。” +'。'。join(path))”行时,控制台将显示错误:

Traceback (most recent call last):
    File "/home/pedro/documents/projects/prosser/main.py", line 18, in <module>
    module.exec.main(path) # Execute the "exec" class and the def "**main**"
AttributeError: module 'lib' has no attribute 'exec'

我也尝试使用:

module = \_\_import\_\_(curDir + "lib." + '.'.join(path))

但是它得到相同的错误。我认为现在比较轻。我想要有人帮助我还是找一些替代代码。 :)

1 个答案:

答案 0 :(得分:0)

我认为您在这里有错误:

您在这里有不同的路径:

if os.path.exists(curDir + "/lib/" + "/".join(path) + ".py")

这里还有另一个,你没有curDir:

module = __import__("lib." + '.'.join(path)) # Returns "lib.task.kill"

您应该使用os.path.join来构建这样的路径:

module = __import__(os.path.join(curdir, 'lib', path + '.py'))