python中使用import*导入部分模块_关于python:Python3.6 |-使用__import__-|从列表中导入模块。...

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

我遇到的命令导入问题是,所有命令都存储在Prosser根文件夹中的" lib"文件夹中,并且如果其中一个文件夹位于" lib"中,则其中可以包含文件夹和文件。" dir,它不能再命名为文件夹,它的名称现在是package(但这暂时不重要)。

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

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

用户可以在文本前键入命令,例如普通终端:

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

假设在" lib"文件夹中存在一个名为" console"的文件夹,并且在其中有一个名为" write.py"的文件,其代码如下:

class exec:

def main(args):

print(args)

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

目前,一切正常,但是现在有了U伙我真正需要的帮助,即命令导入。

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

-! write Hello World

在上面,我只键入" write",这表示该文件仅位于" lib"文件夹中,它没有要存储和打包的包,因此它是一个Freedom命令(一个没有包的命令)或节点)。

-! console.write Hello World

现在,我在" console.write"上方键入内容,这表示该文件具有要存储和分隔的包或节点,这意味着它是Tied命令(具有包或节点的命令)。

这样,文件与文件包之间就会用一个点分隔开,您放置的点越多,将浏览更多的文件夹以找到该文件,然后继续执行下一个执行。

最后的代码。我使用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 + '<>kill<

# Reset the input interface

问题是,当执行"模块= __import __(" lib。" +'。'。join(path))"行时,控制台将显示错误:

Traceback (most recent call last):

File"/home/pedro/documents/projects/prosser/main.py", line 18, in

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))

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

您是否将工作目录添加到系统路径?

我认为您在这里有错误:

您在这里有不同的路径:

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'))

"如果os.path.exists(curDir +" / lib /" +" /".join(path)+" .py")"返回" / home / pedro / documents / projects / Prosser / lib / console / writepy"为。 所以它不是不正确的。

@JooPedro模块= __import __(os.path.join(lib,path + .py))然后呢?

没有用,可以在写之前进行测试吗? 它更有帮助。 我已经读过__import __()内部称为fromlist的类型:__import __(...,fromlist =" ...")

Thx寻求帮助,但是我解决了代码,我只使用了模块" importlib"并使用了命令:" mod = importlib.import_module(lib。+ ..join(path))"。:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值