【问题标题】:VSCODE PYTHON - ModuleNotFoundError: No module named 'src'VSCODE PYTHON - ModuleNotFoundError:没有名为“src”的模块
【发布时间】:2022-07-07 21:40:06
【问题描述】:

我在“cmd”中使用 pytest 进行了单元测试,它工作正常,但是当我尝试导入我的主文件时出现导入错误。这是我的文件树结构:

MATCHING_CC/
- pytest.ini
- __init__.py               (empty file)
- conftest.py               (empty file)
- src/
    - __init__.py           (empty file)
    - conftest.py           (empty file)
    - matching_ingr_zing.py
    - services/
        - __init__.py       (empty file)
        - ElasticSearch.py
        - ingredient_extractor_for_search.py
        - metrics.py
        - stop_word_matching.py
- models/        (folder to save trained models)
- tests/
    - __init__.py                (empty file)
    - test_services/
        - __init__.py            (empty file)
        - test_elastic_search.py
        - test_ingredient_extractor_for_search.py
        - test_metrics.py
    - test_src/
        - __init__.py                (empty file)
        - test_matching_ingr_zingr.py

这里是问题的图像: error

如果有人可以帮助我并解释我的错误是什么,我将非常感激。

【问题讨论】:

标签: python visual-studio-code python-import importerror src


【解决方案1】:

这也许可以解决您的问题:

import sys
  
# adding src to the system path
sys.path.insert(0, '/home/USERNAME/PATH/TO/src')
  
from src import something

【讨论】:

    【解决方案2】:

    你需要从你的父目录MATCHING_CC_TEST而不是src运行测试:

    【讨论】:

      猜你喜欢
      • 2021-03-09
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 2020-11-20
      • 2021-06-03
      • 2019-09-14
      • 1970-01-01
      相关资源
      最近更新 更多