让pylint识别自定义模块加载器 [英] Having pylint recognize custom module loader

查看:50
本文介绍了让pylint识别自定义模块加载器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义模块加载器,它基本上可以进行一些重定向.我想让 pylint 识别这个自定义加载器.这是我的情况:

I have a custom module loader that basically does some redirection. I would like pylint to recognize this custom loader. This is my situation:

root/
    __init__.py
    new/
        __init__.py
        foo.py
        bar.py
    old/
        __init__.py

我有很多客户端导入 old.foo.我在 old/__init__.py 中编写了一个自定义加载器,将它们重定向到引擎盖下的导入 new.foo .我如何让 pylint 识别这一点?当它 lints import old.foo 时,它抱怨找不到 old.foo.这只是pylint的一个问题.我可以让客户端代码毫无问题地识别自定义加载程序.

I have a lot of clients importing old.foo. I wrote a custom loader in old/__init__.py to redirect these to import new.foo under the hood. How do I get pylint to recognize this? When it lints import old.foo, it complains that it can't find old.foo. This is only a problem with pylint. I can get client code to recognize the custom loader without any issue.

推荐答案

来自 模块文档:

包支持另一种特殊属性,__path__.在执行该文件中的代码之前,它被初始化为包含保存包的 __init__.py 的目录名称的列表.这个变量是可以修改的;这样做会影响以后对包中包含的模块和子包的搜索.

Packages support one more special attribute, __path__. This is initialized to be a list containing the name of the directory holding the package’s __init__.py before the code in that file is executed. This variable can be modified; doing so affects future searches for modules and subpackages contained in the package.

因此,如果我理解正确,您想重定向对 old 的任何引用以重定向到 new,因此您需要做的就是替换 old 文件夹,其中 old.py 包含以下内容:

So if I understand correctly you want to redirect any references to old to redirect to new, so all you would need to do is replace the old folder with old.py that contains this:

__path__ = ["new"]

然后当任何东西试图导入 old.foo 时,它最终会导入 new.foo.

Then when anything tries to import old.foo it will end up importing new.foo.

这篇关于让pylint识别自定义模块加载器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆