在 python 和 pylint 中添加到 sys.path 的路径

编程入门 行业动态 更新时间:2024-10-27 14:26:44
本文介绍了在 python 和 pylint 中添加到 sys.path 的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以.我知道这个问题似乎已经被问到死了,但似乎没有一个答案能解决我想要做什么.

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"来完成.请参阅此答案:stackoverflow/a/3065082/4323

You can do it using an "init hook" for pylint. See this answer: stackoverflow/a/3065082/4323

还有 来自 pylint 的错误跟踪器的声明:

我们可能不会自动支持此功能.但是现在我们确实支持手动添加路径,尽管以更麻烦的方式,通过``--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 的路径

本文发布于:2023-11-23 21:19:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622894.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   pylint   python   path   sys

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!