英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论