You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让VSCode的Python扩展在运行unittest测试用例时识别自定义子模块

Fix ModuleNotFoundError for Nested Submodules in VSCode Unittest (src Layout)

First, let's nail down the root cause: your project uses the standard src layout, but Python can't find the common package because the src directory (the root of your packages) isn't properly added to sys.path. Most of your previous attempts pointed to the wrong directory (like common instead of src), which is why they didn't work.

Here are the reliable fixes, ordered from cleanest to most explicit:

1. Correctly Configure PYTHONPATH with .env

This is the most maintainable solution for VSCode:

  • Update your .env file in the project directory to point to the src folder (not common):
    PYTHONPATH=./src
    
  • Fix the python.envFile path in your settings.json (you had a typo afinad/.env before):
    "python.envFile": "${workspaceFolder}/project/.env"
    
  • Restart VSCode to load the updated environment variables. This ensures sys.path includes src, so import common.nested_module will work as expected.

2. Adjust VSCode Unittest Configuration

If you prefer configuring directly in VSCode instead of using .env, tweak your unittestArgs to specify the top-level package directory with -t:

{
  "python.testing.unittestArgs": [
    "-v",
    "-s",
    "${workspaceFolder}/project/tests",
    "-p",
    "test_*.py",
    "-t",
    "${workspaceFolder}/project/src"
  ],
  "python.testing.cwd": "${workspaceFolder}/project",
  "python.testing.pytestEnabled": false,
  "python.testing.unittestEnabled": true
}

The -t flag tells unittest that src is the root of your package hierarchy, so it can resolve imports like common.nested_module correctly.

3. Explicitly Add src to sys.path in Test Files (Fallback)

If the above methods don't work (e.g., due to environment quirks), add this code at the top of your test_nested_module.py (and other test files):

import os
import sys

# Get the absolute path to the project root (the folder containing src and tests)
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Add the src directory to sys.path
sys.path.insert(0, os.path.join(PROJECT_ROOT, "src"))

This dynamically adds src to the front of sys.path no matter where you run the tests from, ensuring the package is found.

4. Run Tests Correctly in the Terminal

To run tests from the command line without VSCode:

  1. Navigate to the project directory:
    cd /path/to/your/project
    
  2. Either set PYTHONPATH first:
    export PYTHONPATH=./src
    python -m unittest discover -s tests
    
    Or specify the top-level directory directly:
    python -m unittest discover -s tests -t src
    

Why Your Previous Attempts Failed

  • Method 1: You added project/src/common to sys.path, which lets you import nested_module directly but not common.nested_module (since common isn't recognized as a package root).
  • Method 2: Your .env had PYTHONPATH=./common (wrong directory) and the envFile path was a typo.
  • Method 4: You ran tests from the tests directory without setting PYTHONPATH or specifying the top-level directory, so Python couldn't find src-based packages.

内容的提问来源于stack exchange,提问作者marcel h

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 12:59:04