我正在嘗試將 VSCode 中的測驗擴展與 Python 擴展一起使用。我使用 pytest 作為我的測驗庫。我的檔案夾結構如下所示:
PACKAGENAME/
├─ PACKAGENAME/
│ ├─ __init__.py
│ ├─ main.py
├─ tests/
│ ├─ test_main.py
├─ requirements.txt
在test_main.py我試圖匯入包代碼的檔案中,為了測驗它:
from PACKAGENAME import *
從命令列,在根目錄中PACKAGENAME,我可以使用python -m pytest運行測驗正常的命令。沒有找到模塊沒有問題。但是,當我嘗試使用 VSCode 測驗選項卡時,發現了測驗,但出現以下錯誤:
=================================== ERRORS ====================================
_____________________ ERROR collecting tests/test_main.py _____________________
ImportError while importing test module 'd:\PATH\TO\PACKAGENAME\tests\test_main.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
C:\Users\USER\anaconda3\envs\uni\lib\importlib\__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests\test_main.py:1: in <module>
from PACKAGENAME import *
E ModuleNotFoundError: No module named 'PACKAGENAME'
=========================== short test summary info ===========================
有沒有辦法讓這個作業而不必使用命令列?
uj5u.com熱心網友回復:
我建議你像這樣嘗試:
- 確保您的 VS Code 作業區設定為(根目錄)的父目錄
PACKAGENAME __init__.py在tests目錄中添加一個空 檔案- 在
test_main.py,替換from PACKAGENAME import *為from PACKAGENAME.main import *
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/316782.html
