VS Code 中的 Pylance 报告带有导入的未定义变量 *
Pylance in VS Code reports undefined variable with import *
使用 VSCode v1.61.0
我有一个 python 包,一个名为“tfmodules”的文件夹,里面有很多模块,在 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"]
但我仍然收到来自 Pylance 的错误。我究竟做错了什么?谢谢!
考虑到您不会在运行时添加任何扩展,如何创建一个文件来直接生成对“__init__.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",
]
使用 VSCode v1.61.0 我有一个 python 包,一个名为“tfmodules”的文件夹,里面有很多模块,在 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"]
但我仍然收到来自 Pylance 的错误。我究竟做错了什么?谢谢!
考虑到您不会在运行时添加任何扩展,如何创建一个文件来直接生成对“__init__.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",
]