Python: 导入无法解析,但代码正常运行

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

Python: Import could not be resolved but code is working

问题

我有以下的Python项目结构,并且在根目录下使用 python -m image_batch_processor.main 运行我的脚本是可以的。同时,在根目录下使用 discovery 进行单元测试也可以正常工作。

.
├── README.md
├── image_batch_processor
│   ├── __init__.py
│   ├── argument_parser.py
│   ├── data_collection
│   │   ├── __init__.py
│   │   └── image_file_finder.py
│   ├── image_batch_processor.py
│   ├── image_processing
│   │   ├── image_modification.py
│   │   ├── preview_label_creator.py
│   │   └── utilities
│   └── main.py
└── tests

然而,在每个文件中,我都收到 "无法解析导入" 的 LSP 错误,例如在 main.py 中的导入:

from image_batch_processor.argument_parser import argument_parser
from image_batch_processor.data_collection.image_file_finder import ImageFileFinder
from image_batch_processor.image_batch_processor import ImageBatchProcessor

但是即使在 pip list 中显示已安装的模块如 numpy 或 cv2,也显示相同的错误。

这不是一个重大问题,因为代码是可以运行的,但我想摆脱这些错误消息。

我查了一下,有时需要在虚拟环境中选择正确的解释器,但是我正在使用 pyenv,我的环境是激活的,也选择了正确的解释器。

英文:

I have the following Python project structure and running my script from the root directory using python -m image_batch_processor.main works. Also testing with the unittest module from the root using discovery works fine

.
├── README.md
├── image_batch_processor
│   ├── __init__.py
│   ├── argument_parser.py
│   ├── data_collection
│   │   ├── __init__.py
│   │   └── image_file_finder.py
│   ├── image_batch_processor.py
│   ├── image_processing
│   │   ├── image_modification.py
│   │   ├── preview_label_creator.py
│   │   └── utilities
│   └── main.py
└── tests

However within every file I get an "import could not be resolved" from the LSP - as for example for imports within main.py:

from image_batch_processor.argument_parser import argument_parser
from image_batch_processor.data_collection.image_file_finder import ImageFileFinder
from image_batch_processor.image_batch_processor import ImageBatchProcessor

But also imports of installed modules like numpy or cv2 show the same error even though they show up in pip list

It's not a major issue since the code is working, but I'd like to get rid of the error messages.

I looked it up and sometimes the right interpreter had to be chosen in the virtual environment, however I'm using pyenv and my environment is active and the correct interpreter is chosen.

答案1

得分: 1

看起来环境无法正常工作或选择不正确。尝试为此项目创建一个虚拟环境。你的项目应该如下所示:

.
├── README.md
├── image_batch_processor
│   ├── __init__.py
│   ├── argument_parser.py
│   ├── data_collection
│   │   ├── __init__.py
│   │   └── image_file_finder.py
│   ├── image_batch_processor.py
│   ├── image_processing
│   │   ├── image_modification.py
│   │   ├── preview_label_creator.py
│   │   └── utilities
│   └── main.py
├── tests
└── venv
英文:

Looks like the env is not working properly or selected acordingly. Try creating a venv for this project. Your project should look like this:

.
├── README.md
├── image_batch_processor
│   ├── __init__.py
│   ├── argument_parser.py
│   ├── data_collection
│   │   ├── __init__.py
│   │   └── image_file_finder.py
│   ├── image_batch_processor.py
│   ├── image_processing
│   │   ├── image_modification.py
│   │   ├── preview_label_creator.py
│   │   └── utilities
│   └── main.py
├── tests
└── venv

huangapple
  • 本文由 发表于 2023年4月13日 15:53:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002963.html
匿名

发表评论

匿名网友

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

确定