打开另一个python fi时出现名称错误

2024-05-29 04:04:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直试图从我的主python文件中打开一个python文件:

from banner import * 
from hexed import *  # this one is the file I am trying to open

我试图通过将文件视为模块来打开它们 这就是我如何称呼他们我的横幅文件工程完美,但不能说同样的hexed文件:

def options(self):
while True:
    try:
        try:
            main=raw_input(bcolors.B + "PYDRAGON> " + bcolors.E)
            if main == "msf":
            elif main == "crypto":
                hexed() #THIS IS WHERE I AM CALLING ANOTHER FILE
            elif main == "print":
                banner() #THIS ONE WORKS FINE
            else:
                print bcolors.R + "--> check your input <--" + bcolors.E
                time.sleep(1)
        except KeyboardInterrupt:
            print (bcolors.R + bcolors.U  + "\033[1m" + "\nCtrl-C Pressed! Use 'exit' to close the tool!" + bcolors.E)
            time.sleep(0.9)
            sys.exit()
            pass
    except EOFError:
        print (bcolors.R + bcolors.U + "\nUser Requsted An Interrupt ..Exixting.." + bcolors.E)
        time.sleep(0.9)
        sys.exit()
        pass

我的banner函数工作得很好,但是每当我试图调用hexed python文件时,它就会给我这个错误

Traceback (most recent call last):
File "pydragon.py", line 149, in <module>
obj.options()
File "pydragon.py", line 115, in options
hexed()
NameError: global name 'hexed' is not defined

我已经检查了所有空格和制表符,我认为没有任何缩进错误

下面是我的hexed文件的代码

def witch(self):
main = raw_input(bcolors.R + "Crytography> " + bcolors.E)

if main == 1:
    hound = raw_input(R +  bcolors.U + 'String to encode>' + bcolors.E)
    hound = hound.encode('hex','strict');
    print ""+ G +"Encoded: " + hound
elif main == 2:
    hound1 = raw_input(R +  bcolors.U + 'String to decode>' + bcolors.E)
    hound1 = hound.decode('hex','strict');
    print ""+ G +"Decoded String: " + hound1

else:
    print '\033[31m' + bcolors.BL + "GRRRR, what your'e trying to type ?? " + bcolors.E

我希望这些信息能有所帮助 谢谢


Tags: 文件toinputrawtimemainexitsleep
2条回答

函数名是witch(),而不是hexed()。所以:

更改此项:

        elif main == "crypto":
            hexed() #THIS IS WHERE I AM CALLING ANOTHER FILE

对此:

        elif main == "crypto":
            witch() #THIS IS WHERE I AM CALLING ANOTHER FILE

我不知道你真正想做什么,但你显然应该重新阅读有关导入的文档。你知道吗

与您当前的问题相关-执行以下操作时:

from somemodule import *

Python寻找“somemodule.py文件“在sys.path中,将文件作为模块加载,从模块中收集所有顶级公共名称(“top-level”:在模块级定义,“public”:在模块的__all__属性中公开,或者-如果__all__未定义-不以下划线开头),并将它们绑定到当前名称空间(最终重新绑定已定义的名称)。你知道吗

所以在这两行之后:

from banner import * 
from hexed import *

您已经在脚本的全局命名空间中注入了banner.pyhexed.py中定义的所有名称。很明显,您在某个地方定义了一个名为banner()的函数,正是这个函数被调用,而不是六角体.py文件-你不“调用一个文件”,这是没有意义的,你可以调用一个函数,你可以调用一个类,但你不能“调用一个文件”(也不能“调用一个模块”FWIW,模块是不可调用的)。很明显,在你的六角体.py模块,因此NameError。你知道吗

解决方案很清楚:用显式导入替换star导入(无论如何都应该避免,因为它们是维护的噩梦),要么导入模块,要么通过限定名调用函数,即:

# imports the whole "banner" module
import banner
# the first "banner" is the module name, the second is the function
banner.banner()

或者只导入要使用的函数并直接调用它:

# imports the "banner" function from the "banner" module
from banner import banner
# calls the banner function
banner()

哦,当我们这样做的时候:NameError不会发生在“打开”模块的过程中,但是当您尝试使用一个尚未定义的名称(这里是hexed)时。你知道吗

相关问题 更多 >

    热门问题