简体   繁体   English

名称''未定义

[英]name ' ' is not defined

I have a function which is stored in builtins. 我有一个内置在内置函数中。 This is used to load python modules with relative paths from the projects base directory. 这用于从项目基本目录加载具有相对路径的python模块。 The projecs base directory is stored under builtins.absolute 项目的基本目录存储在buildins.absolute下

Function below: 功能如下:

def projectRelativeImport(fileName, projectRelativePath, moduleName = None):
    # if moduleName not set, set it to file name with first letter capatilised
    if moduleName is None:
        moduleName = fileName[:1].capitalize() + fileName[1:]

    # we shouldn't be passing fileName with an extension unless moduleName is set due to previous if. So in those cases we add .py
    if len(fileName) >= 3 and fileName[-3:] != '.py':
        fileName = fileName + '.py'

    dir = os.path.join(builtins.absolute, projectRelativePath)
    full = os.path.join(dir, fileName)

    sys.path.append(dir)
    imp.load_source(moduleName, full)
    sys.path.remove(dir)

On one of my other files I use projectRelativeImport('inputSaveHandler', 'app/util', 'SaveHandler') to import SaveHandler from app/util/inputSaveHandler.py. 在其他文件之一中,我使用projectRelativeImport('inputSaveHandler', 'app/util', 'SaveHandler')从app / util / inputSaveHandler.py导入SaveHandler。 This runs through the project RelativeImport absolutely fine. 这在整个项目RelativeImport中运行都很好。 Correct strings are being used by imp, I've printed to check. imp已使用正确的字符串,我已进行检查。

But a couple of lines after that execution I have a line 但是执行那几行后,我有一行

handler = SaveHandler.ConfHandler()

Which throws NameError: name 'SaveHandler' is not defined 引发NameError: name 'SaveHandler' is not defined


I realise my project relative import function is a bit odd, especially since I have it globally saved using builtins (there's probably a better way but I only started using python over the last two days). 我意识到我的项目相对导入功能有点奇怪,尤其是因为我使用内置函数将其全局保存(可能是一种更好的方法,但我仅在最近两天才开始使用python)。 But I'm just a bit confused as to why the name isn't being recognised. 但是我对为什么不认识这个名字有点困惑。 Do I need to return something from imp due to scope being rubbish as the project relative import function is in a different file? 由于项目相对导入功能位于另一个文件中,我是否由于作用域混乱而需要从imp返回值?

I fixed this by returning from projectRelativeImport() what was passed back from imp.load_source as shown below: 我通过从projectRelativeImport()返回从imp.load_source返回的内容来解决此问题,如下所示:

sys.path.append(dir)
submodule = imp.load_source(moduleName, full)
sys.path.remove(dir)
return submodule

Then when I used the import function the returned value now goes to a variable with the same name as that I gave to the module (all very strange) 然后,当我使用import函数时,返回的值现在转到一个变量,该变量的名称与我给模块的名称相同(都很奇怪)

SaveHandler = projectRelativeImport('inputSaveHandler', 'app/util', 'SaveHandler')

I got to this because it worked no problem from the file projectRelativeImport was defined in but not in any others. 我这么做是因为它在文件projectRelativeImport定义的文件中没有问题,但在其他任何文件中均未定义。 So it was clearly a scope issue to me, so I figured I'd just try returning whatever imp gave me and it worked 因此,这显然对我来说是一个范围问题,因此我认为我会尝试退回给我带来的任何印象,并且有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM