Pycharm 从旧版本项目复制后无法识别项目包

Pycharm not recognizing project packages after copying from old version of project

我正在尝试使用 pytest 为 repo 设置单元测试框架,并且我在一个旧分支中工作。我克隆了新分支并将文件复制到新分支。调用 pytest 时,测试会 运行 正常,但它们引用的是旧项目中的文件(即使它们是作为当前项目中的包导入的,而不是通过直接文件路径导入的)。

这是 line/command 用于 运行 pytest:

(venv) C:\Program Files\Python-3.8.2\pycharm_projects\fnma-erm01-devl-edl-hao-repo_21.3>python -m pytest

为了尝试解决这个问题,我在本地删除了旧分支,现在 none 的测试可以运行,并且它不会将任何项目目录识别为有效模块。

堆栈跟踪:

========================================================================================================================================================================= test session starts =========================================================================================================================================================================
platform win32 -- Python 3.8.2, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: C:\Program Files\Python-3.8.2\pycharm_projects\ERM_EDL_HAO_21.3_Branch, configfile: pytest.ini
plugins: mock-3.5.1
collecting ... sys path ['
C:\Program Files\Python-3.8.2\pycharm_projects\ERM_EDL_HAO_21.3_Branch\SourceCode\application\deploy\app\hao',
'C:\Program Files\Python-3.8.2\pycharm_projects\ERM_EDL_HAO_21.3_Branch',
'C:\Progra~1\Python-3.8.2\pycharm_projects\fnma-erm01-devl-edl-hao-repo\SourceCode\application\deploy\app\hao\lambda',
'C:\Progra~1\Python-3.8.2\pycharm_projects\fnma-erm01-devl-edl-hao-repo\SourceCode\application\deploy\app\hao\lambda\tests',
'C:\Progra~1\Python-3.8.2\pycharm_projects\fnma-erm01-devl-edl-hao-repo\SourceCode\application\deploy\app\hao\lambda\utils',
'C:\Program Files\Python-3.8.2\pycharm_projects\ERM_EDL_HAO_21.3_Branch',
'C:\Program Files\Python-3.8.2\python38.zip',
'C:\Program Files\Python-3.8.2\DLLs',
'C:\Program Files\Python-3.8.2\lib',
'C:\Program Files\Python-3.8.2',
'C:\Program Files\Python-3.8.2\pycharm_projects\ERM_EDL_HAO_21.3_Branch\venv',
'C:\Program Files\Python-3.8.2\pycharm_projects\ERM_EDL_HAO_21.3_Branch\venv\lib\site-packages',
'C:\Users\g3uj2m\AppData\Roaming\Python\Python38\site-packages',
'C:\Users\g3uj2m\AppData\Roaming\Python\Python38\site-packages\win32',
'C:\Users\g3uj2m\AppData\Roaming\Python\Python38\site-packages\win32\lib',
'C:\Users\g3uj2m\AppData\Roaming\Python\Python38\site-packages\Pythonwin',
'C:\Program Files\Python-3.8.2\lib\site-packages']
collected 0 items / 16 errors  
=============================================================================================================================================================================== ERRORS ================================================================================================================================================================================
_______________________________________________________________________________________________________________________ ERROR collecting SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_amtm_upstream_notification.py _______________________________________________________________________________________________________________________
ImportError while importing test module 'C:\Program Files\Python-3.8.2\pycharm_projects\ERM_EDL_HAO_21.3_Branch\SourceCode\application\deploy\app\hao\lambda\tests\handlers\unittest_hao_amtm_upstream_notification.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
..\..\lib\importlib\__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
SourceCode\application\deploy\app\hao\lambda\tests\handlers\unittest_hao_amtm_upstream_notification.py:8: in <module>
    from hao_amtm_upstream_notification import lambda_function
E   ModuleNotFoundError: No module named 'hao_amtm_upstream_notification'

# ... ^ that part more or less repeated 15 more times ...

======================================================================================================================================================================= short test summary info =======================================================================================================================================================================
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_amtm_upstream_notification.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_batchjob_status_check.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_batchjob_submit.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_capture_hedge_status.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_ccfa_upstream_notification.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_dynamodb_status_check.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_glue_function.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_irdb_upstream_notification.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_krd_upstream_notification.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_ln_pool_max.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_s3_data_availability_check.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_stepfn_execution_status.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_swap_upstream_notification.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_trigger_sns_msg.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_verify_upstream_data_availability.py
ERROR SourceCode/application/deploy/app/hao/lambda/tests/handlers/unittest_hao_workflow_trigger.py

文件树:

pytest.ini

SOURCECODE
├───application
│   └───deploy
│       └───app
│           └───hao
│               ├───lambda
│               │   ├───hao_amtm_upstream_notification
│               │   │       lambda_function.py
│               │   │
│               │   ├───hao_batchjob_status_check
│               │   │       lambda_function.py
│               │   │
│               │   ├───hao_batchjob_submit
│               │   │       lambda_function.py
│               │   │
                  ... etc ...
│               │   │
│               │   ├───tests
│               │   │   conftest.py
│               │   │
│               │   │   ├───handlers
│               │   │   │       unittest_hao_amtm_upstream_notification.py
│               │   │   │       unittest_hao_batchjob_status_check.py
│               │   │   │       unittest_hao_batchjob_submit.py
│               │   │   │     ... etc ...

pytest.ini 文件:

# pytest.ini
[pytest]
python_files = unittest_*.py

这是我已经知道的 tried/checked:

  1. 所有必要的文件夹都已标记为“Sources Root”。
  2. 我已将 __init__.py 添加到所有必要的文件夹中。
  3. 我已检查以确保 __init__.py 未被识别为文本文件。
  4. 我 运行 文件下的“使缓存无效/重新启动...”选项。
  5. 我已确保项目的解释器与当前项目和目录保持一致。

它不会让我包含屏幕截图,但项目结构应该不是问题,因为它在旧分支中运行得非常好。 (旧分支结构与新分支结构完全相同,因此从一个复制到另一个时应该没有引用问题)

编辑 1: 我正在使用 pytest.ini 配置文件,其中唯一指定的是用于识别测试文件的自定义前缀。我为新项目创建了一个新的 venv,并且我在 PyCharm 中从内置终端调用 pytest,因此 venv 处于活动状态。

编辑 2: 缩短了堆栈跟踪以便于阅读,并添加了文件树和 pytest.ini 文件的相关部分。

好的,似乎 Pycharm 在复制到新项目后无法检测到源根中的更改,因为除了项目名称之外,路径是相同的。奇怪的是,如果我通过 Pycharm 的内置 pytest 运行ner 创建测试配置和 运行,它就可以工作。也就是说,我无法让控制台使用正确的 PYTHONPATH,但我找到了解决方法。我没有使用 python -m pytest 通过控制台调用 pytest,而是创建了一个强制更新 PYTHONPATH 的脚本。

新命令:

python run_unittests.py

新脚本:

import pytest, os, sys

project_dir = os.path.dirname(os.path.abspath(__file__))
base_test_dir = os.path.join(project_dir, 'SourceCode', 'application', 'deploy', 'app', 'hao', 'lambda')

test_dirs = [
    os.path.join(base_test_dir, 'tests'),
    os.path.join(base_test_dir, 'handlers'),
    os.path.join(base_test_dir, 'utils'),
    base_test_dir
]

for i in test_dirs:
    if i not in sys.path:
        sys.path.append(i)

test_args = [a for a in sys.argv if a != 'run_unittests.py']

pytest.main([base_test_dir]+test_args)

感谢@bad_coder 的帮助。