在 python 和 pylint 中添加到 sys.path 的路径 [英] Adding a path to sys.path in python and pylint
问题描述
所以.我知道这个问题似乎已经被问到死了,但似乎没有一个答案能解决我想要做什么.
So. I'm aware that this question seems to have been asked to death, but none of the answers seem to address what I want to do.
我在另一个目录中有一个库,我想将它包含在我运行的一组 other 项目中.我不希望每次运行 python 时都添加该库..
I have a library in another directory that I want to include in a set of other projects that I run. I don't want that library added every time I run python..
所以,我一直在我的python代码中做的是:
So, what I had been doing was this in my python code:
import sys
sys.path.append("/tmp/demo/src/my-lib")
import MyClass
这很好用.但是,现在我发现并喜欢 pylint,它抱怨
and this worked fine. But, now that I've discovered and love pylint, it complains that
E: 7, 0: Unable to import 'MyClass' (import-error)
C: 7, 0: Import "import MyClass" should be placed at the top of the module (wrong-import-position)
我知道我可以使用指令禁用导入错误和错误导入位置(或者只是将其放入 .pylintrc ...)但是,我宁愿不这样做.我想知道将路径添加到 sys.path 的正确"方法,该路径不是全局到所有我的项目,只是添加到使用它的项目子集特定的图书馆.
I know I could just disable import-error and wrong-import-position with a directive (or just by putting it into .pylintrc...) But, I'd rather not. I'd like to know the "right" way of adding a path to sys.path that's not global to all my projects, just to the subset of projects that use that particular library.
这可能吗?
推荐答案
您可以使用 pylint 的init hook"来完成.请参阅此答案:https://stackoverflow.com/a/3065082/4323
You can do it using an "init hook" for pylint. See this answer: https://stackoverflow.com/a/3065082/4323
我们可能不会自动支持此功能.但是现在我们确实支持手动添加路径,尽管以更麻烦的方式,通过``--init-hook="import sys; sys.path.append(...)"
We will probably not going to support this automatically. But right now we do support manually additions to path, although in a more cumbersome way, through ``--init-hook="import sys; sys.path.append(...)"
这篇关于在 python 和 pylint 中添加到 sys.path 的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!