Adding a path to sys.path in python and pylint

ⅰ亾dé卋堺 提交于 2019-12-06 21:49:07

问题


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.

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..

So, what I had been doing was this in my python code:

import sys
sys.path.append("/tmp/demo/src/my-lib")
import MyClass

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)

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.

Is this possible?


回答1:


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

And this statement from pylint's bug tracker:

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(...)"



来源:https://stackoverflow.com/questions/44732819/adding-a-path-to-sys-path-in-python-and-pylint

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!