VS代码中的Pylance使用导入报告未定义的变量*

2024-09-29 07:21:06 发布

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

使用VSCode v1.61.0 我有一个python包,一个名为“tfmodules”的文件夹,其中包含整个lotta模块,我在init.py中执行以下操作:

from os.path import dirname, basename, isfile, join
import glob
modules = glob.glob(join(dirname(__file__), "*.py"))
__all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]

在我的代码中,我这样导入它:

from tfmodules import *

然后我用如下方式调用模块:

def write(aci_topology_model, provider_source, provider_version):
    with open('fabric-bringup.tf', 'w') as f:
        x = fabricbringup.tf_write_fabric_bringup(aci_topology_model, provider_source, provider_version)
        f.write(x)

fabricbringup是软件包中的一个模块。 代码运行得很好,但是Pylance抛出了36个reportUndefinedVariable实例,每个调用的模块一个实例

具体错误(来自其中一个模块)为:

"fabricbringup" is not defined Pylance(reportUndefinedVariable)

我已经为vs代码中的venv选择了合适的解释器,并尝试将其添加到settings.json文件中:

"python.analysis.extraPaths": ["tfmodules", "/home/aj/projects/python/dcnet-aci-lld-convert/tfmodules"]

但我还是从派伦斯那里得到了错误。我做错了什么?谢谢


Tags: 模块frompyimportinitproviderglobwrite
1条回答
网友
1楼 · 发布于 2024-09-29 07:21:06

考虑到您不会在运行时添加任何扩展名,那么创建一个直接生成对“\uuuu init\uuuu.py”的所有引用的文件怎么样

if __name__ == "__main__":
    from os.path import dirname, basename, isfile, join
    import glob
    modules = glob.glob(join(dirname(__file__), "*.py"))
    print(join(dirname(__file__), "*.py"))
    __all__ = ["\t\"" +basename(f)[:-3]+"\",\n" for f in modules if isfile(f) and not f.endswith('__init__.py') and not f.endswith('__add__manager.py')]

    f = open(join(dirname(__file__), "__init__.py"), "a")
    f.write("__all__ = [\n")
    f.writelines(__all__)
    f.write("]\n")
    f.close()

结果将如下所示:

#(previous contents of __init__.py)

__all__ = [
    "file1",
    "file2",
]

相关问题 更多 >