vscode-python: Python Tests fails to load PYTHONPATH
Type: Bug
Behaviour
Expected vs. Actual
I have this in my settings file: “terminal.integrated.inheritEnv”: true This seems to have correctly imported my PYTHONPATH that I set in my .bashrc for the purposes of development. (i.e. code completion, jump to definition etc…) all work as expected. However when I write a test file, the test functionality in VSCode fails. I’m able workaround the failure by manually including the path of my custom module via sys.path.append, however, this should not be necessary.
My workspace:
bug_test
– classA.py
– tests
---- test_A.py
classA:
class A:
pass
test_A:
import sys
import unittest
from bug_test import classA
class Test1(unittest.TestCase):
def test1(self):
self.assertEqual(4,4)
The expected behavior is for the test to pass. The actual behavior is:
ModuleNotFoundError: No module named: bug_test
Workaround: Adding this line in test_A.py : sys.path.append(‘/home/grechkay/yyg_projects’)
fixes the issue and the test passes.
However, this has already been added to the PYTHONPATH in the bashrc file, which is recognized by all of the other VSCode functionality except for the tests.
Steps to reproduce:
- Add: export PYTHONPATH=‘{path_to_module}’:$PYTHONPATH in .bashrc
- structure your workspace as above
- run the tests
Diagnostic data
- Python version (& distribution if applicable, e.g. Anaconda): 3.8.10
- Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Global
- Value of the
python.languageServer
setting: Default
Output for Python
in the Output
panel (View
→Output
, change the drop-down the upper-right of the Output
panel to Python
)
XXX
User Settings
languageServer: "Pylance"
testing
• unittestArgs: "<placeholder>"
• unittestEnabled: true
Extension version: 2023.12.0 VS Code version: Code 1.80.1 (74f6148eb9ea00507ec113ec51c489d6ffb4b771, 2023-07-12T17:22:25.257Z) OS version: Linux x64 5.15.0-76-generic Modes:
System Info
Item | Value |
---|---|
CPUs | Intel® Core™ i7-10700K CPU @ 3.80GHz (16 x 3800) |
GPU Status | 2d_canvas: enabled canvas_oop_rasterization: disabled_off direct_rendering_display_compositor: disabled_off_ok gpu_compositing: enabled multiple_raster_threads: enabled_on opengl: enabled_on rasterization: enabled raw_draw: disabled_off_ok video_decode: enabled video_encode: disabled_software vulkan: disabled_off webgl: enabled webgl2: enabled webgpu: disabled_off |
Load (avg) | 0, 1, 1 |
Memory (System) | 31.01GB (11.41GB free) |
Process Argv | . --crash-reporter-id cbbfbe56-bd2c-4171-b7c8-8bf66b35fe73 |
Screen Reader | no |
VM | 0% |
DESKTOP_SESSION | ubuntu |
XDG_CURRENT_DESKTOP | Unity |
XDG_SESSION_DESKTOP | ubuntu |
XDG_SESSION_TYPE | x11 |
A/B Experiments
vsliv368cf:30146710
vsreu685:30147344
python383cf:30185419
vspor879:30202332
vspor708:30202333
vspor363:30204092
vslsvsres303:30308271
vserr242:30382549
pythontb:30283811
vsjup518:30340749
pythonptprofiler:30281270
vshan820:30294714
vstes263cf:30335440
vscod805:30301674
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
vsaa593:30376534
pythonvs932:30410667
py29gd2263cf:30789497
vsclangdc:30486549
c4g48928:30535728
dsvsc012cf:30540253
pynewext54:30695312
azure-dev_surveyone:30548225
282f8724:30602487
pyind779:30671433
f6dab269:30613381
pythonsymbol12:30671437
2i9eh265:30646982
showlangstatbar:30737416
vsctsb:30748421
pythonms35:30701012
03d35959:30757346
pythonfmttext:30731395
pythoncmvfstrcf:30756944
fixshowwlkth:30771522
hideindicator:30785051
pythongtdpath:30769146
i26e3531:30780429
pythonnosm12tcf:30779713
pythonidxpt:30784022
pythonnoceb:30776495
h7j2d465:30786200
dsvsc013:30789517
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 4
- Comments: 22 (8 by maintainers)
found the issue- seems we are not referencing the user-defined env file as part of the environment when configuring the sub-process for the test run. I will add it now but this will require a PR. In the meantime hopefully, your workaround can suffice and I will close this issue when the fix is merged. Thanks
Using the
v2023.19.12961014
pre-release of the extension it works for me, using that simple test case and without settingtool.pytest.ini_options
. Thanks!@eleanorjboyd I still see this issue if I don’t opt out of the new test adapter experiment, and we keep having to opt out team members if they’ve been included. Is there any more info I can provide? https://github.com/microsoft/vscode-python/issues/21642#issuecomment-1659654444
Another case here. I described my situation extensively in https://github.com/microsoft/vscode-python/discussions/21872.
Had the same issue and adding
"python.experiments.optOutFrom": ["pythonTestAdapter"]
fixed the issue. It did not logExperiment 'pythonTestAdapter'
is active in logs.