Pylint与VSCode和Python在导入方面存在不一致。

huangapple go评论56阅读模式
英文:

Pylint disagrees with VSCode and python in imports

问题

我找不到正确编码的方法,以便同时让 pylint 代码的执行(在 VSCode 中或从命令行中)都能正常工作。

有一些类似的问题,但似乎都不适用于我的项目结构,其中包含一个名为 src 的目录,该目录下将有多个包。以下是简化后的项目结构:

.
├── README.md
├── src
│   ├── rssita
│   │   ├── __init__.py
│   │   ├── feeds.py
│   │   ├── rssita.py
│   │   └── termcolors.py
│   └── zanotherpackage
│       ├── __init__.py
│       └── anothermodule.py
└── tests
    ├── __init__.py
    └── test_feeds.py

从我理解的情况来看,rssita 是我的包之一(因为有 init.py 文件),其中有一些模块,其中 rssita.py 文件包含以下导入语句:

from feeds import RSS_FEEDS
from termcolors import PC

如上所示的 rssita.py 代码可以在 VSCode 中和从项目根目录的命令行中都正常运行,但同时 pylint(无论是在 VSCode 中还是从命令行中使用 pylint srcpylint src/rssita)都会将这两个导入标记为未找到。

如果我修改代码如下:

from rssita.feeds import RSS_FEEDS
from rssita.termcolors import PC

那么 pylint 将会满意,但代码将不再运行,因为它无法找到这些导入。

那么,对此有什么最清晰的修复方法呢?

英文:

I am not finding the way to properly code so that both pylint and the execution of the code (within VSCode or from the command line) would work.

There are some similar questions but none seems to apply to my project structure with a src directory under which there will be multiple packages. Here's the simplified project structure:

.
├── README.md
├── src
│   ├── rssita
│   │   ├── __init__.py
│   │   ├── feeds.py
│   │   ├── rssita.py
│   │   └── termcolors.py
│   └── zanotherpackage
│       ├── __init__.py
│       └── anothermodule.py
└── tests
    ├── __init__.py
    └── test_feeds.py

From what I understand rssita is one of my my packages (because of the init.py file) with some modules under it amongst which rssita.py file contains the following imports:

from feeds import RSS_FEEDS
from termcolors import PC

The rssita.py code as shown above runs well from both within VSCode and from command line python ( python src/rssita/rssita.py ) from the project root, but at the same time pylint (both from within VSCode and from the command line (pylint src or pylint src/rssita)) flags the two imports as not found.

If I modify the code as follows:

from rssita.feeds import RSS_FEEDS
from rssita.termcolors import PC

pylint will then be happy but the code will not run anymore since it would not find the imports.

What's the cleanest fix for this?

答案1

得分: 1

"在我看来,pylinty是正确的,你的设置/ PYTHONPATH 需要修复:在Python 3中,默认情况下,所有导入都是绝对的,因此

from feeds import RSS_FEEDS
from termcolors import PC

应该查找名为 'feeds' 和 'termcolors' 的顶级包,我认为它们不存在。

python src/rssita/rssita.py

这真的不是正确的调用方式,它将设置一个非常奇怪的 PYTHONPATH,以便运行一个随机脚本。

正确的导入应该是相对于包的

from .feeds import RSS_FEEDS
from .termcolors import PC

此外,如果你打算运行一个包,应该是 一个可运行的包使用 __main__

python -m rssita

或者你可以将子包作为模块运行:

python -m rssita.rssita

因为你正在使用 src 包,你要么需要创建一个 pyproject.toml 以便可以使用可编辑的安装,要么在运行命令之前设置 PYTHONPATH=src。这确保了包在 PYTHONPATH 的顶级可见,从而可以正确导入。虽然我不是 src 布局和可运行包互动的专家,所以可能有更好的解决方案。"

英文:

As far as I'm concerned pylinty is right, your setup / PYTHONPATH is screwed up: in Python 3, all imports are absolute by default, so

from feeds import RSS_FEEDS
from termcolors import PC

should look for top-level packages called feeds and termcolors which I don't think exist.

> python src/rssita/rssita.py

That really ain't the correct invocation, it's going to setup a really weird PYTHONPATH in order to run a random script.

The correct imports should be package-relative:

from .feeds import RSS_FEEDS
from .termcolors import PC

Furthermore if you intend to run a package, that should be either a runnable package using __main__:

python -m rssita

or you should run the sub-package as a module:

python -m rssita.rssita

Because you're using an src-package, you'll either need to create a pyproject.toml so you can use an editable install, or you'll have to PYTHONPATH=src before you run the command. This ensures the packages are visible at the top-level of the PYTHONPATH, and thus correctly importable. Though I'm not a specialist in the interaction of src layouts & runnable packages, so there may be better solutions.

huangapple
  • 本文由 发表于 2023年2月6日 19:11:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定