I'm using vs code for a python project. The project's python source code and test code directories are separate, but their structure parallels one another. I am able to run tests without an issue from both the command line and from within vs code. Despite this, when modifying code in test_file.py
vs code's autocomplete does not see modules in ./src/main/python
and displays import errors "Import module name could not be resolved".
Expectation:
Autocorrect to work from within test directories despite the src & test directory structures paralleling one another.
Note: If I change the test directory structure to no longer parallel the src directory structure, the issue goes away.
Any help with this would be greatly appreciated. I'm happy to provide additional information if needed.
Example Test Code:
test_file.py
from pytest import mark
# This is where we have an error in the code editor despite
# this running fine.
#
# Import module_A.mocule_1.file could not be resolved
from module_A.module_1.file import handler
@mark.unit_test
class Handler_tests:
def test_happy_path(self):
handler(None)
I have an .env at project root that includes
.env:
PYTHONPATH=./src/main/python:./src/test/python
Project Directory Structure:
// note, some non-python code directories/files exist but are omitted
<proj_root>
├──README.md
├──.env
└──src
├── main
│ └── python
│ └── module_A
│ ├── __init__.py
│ └── module_1
│ ├── __init__.py
│ └── file.py
└── test
└── python
├── conftest.py
└── module_A
├── __init__.py
└── module_1
└── test_file.py
Environment Information:
VS Code
Version: 1.63.0-insider (Universal)
Commit: bedf867b5b02c1c800fbaf4d6ce09cefbafa1592
Date: 2021-11-18T05:17:00.890Z (3 wks ago)
Electron: 13.5.2
Chromium: 91.0.4472.164
Node.js: 14.16.0
V8: 9.1.269.39-electron.0
OS: Darwin arm64 21.1.0
Pylance
v2021.12.2-pre.1
CodePudding user response:
PYTHONPATH=src/main/python
And you can verify the PYTHONPATH in the code(sys.path
).