kondratyev-nv / vscode-python-test-adapter

Python Test Adapter for the VS Code Test Explorer
https://marketplace.visualstudio.com/items?itemName=LittleFoxTeam.vscode-python-test-adapter
MIT License
120 stars 27 forks source link

PYTHONPATH not respected - issue with remote containers? #230

Closed fsackur closed 3 years ago

fsackur commented 3 years ago

Hi,

I use PYTHONPATH to set the root of my app. This works fine in the default test view, but Test Explorer isn't respecting it:

Error: Process exited with code 4: ImportError while loading conftest '/mono/stepladder/tests/wham5/conftest.py'.
__init__.py:2: in <module>
    from .fixtures import *
fixtures/__init__.py:2: in <module>
    from .test_doubles import *
fixtures/test_doubles.py:4: in <module>
    from stepladder.modules.wham5.model import Device, WhamData, WhamId, Command, TargetId, Message
E   ModuleNotFoundError: No module named 'stepladder.modules'

    at ChildProcess.<anonymous> (/home/mich8638/.vscode-server/extensions/littlefoxteam.vscode-python-test-adapter-0.6.6/out/src/processRunner.js:26:32)
    at Object.onceWrapper (events.js:422:26)
    at ChildProcess.emit (events.js:315:20)
    at maybeClose (internal/child_process.js:1021:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)

If I set up an env file, set PYTHONPATH in it, and point python.envFile at it, then the tests work.

I can also make the tests work by setting python.testing.cwd to the path of my PYTHONPATH.

These aren't really great workarounds for me, unfortunately.

Potentially relevant: I am developing in a dev container. The extension is installed in the container. But PYTHONPATH is baked into the image, so it's hard to understand how the extension isn't seeing it.

tuky commented 3 years ago

Thank you so much for your workaround with the .env file. The other "fix" of setting python.testing.cwd only fixed discovery, but broke the execution, because it was duplicating my src folder.

tuky commented 3 years ago

Without your workaround I'm seeing

pytest-django could not find a Django project (no manage.py file could be found). You must explicitly add your Django project to the Python path to have it picked up.

although

python -m pytest /code/src --collect-only
python -m pytest /code --collect-only

both are indeed working in my dev container from whatever directory i execute that command. And i do have the python path configured in my dev container:

echo $PYTHONPATH
/code/src:
bergkvist commented 3 years ago

It seems to be caused by a bug here: https://github.com/kondratyev-nv/vscode-python-test-adapter/blob/bbe8f1d2d342b2a74aab762f821a3de5678d9312/src/pytest/pytestTestRunner.ts#L157-L161

environment.PYTHONPATH is undefined for me. Replacing it with process.env.PYTHONPATH makes it work. Not sure if this would break something else though.

bergkvist commented 3 years ago

Assuming you are using littlefoxteam.vscode-python-test-adapter-0.6.7, you can manually patch locally by editing the following file:

// VSCode insiders
vim ~/.vscode-server-insiders/extensions/littlefoxteam.vscode-python-test-adapter-0.6.7/out/src/pytest/pytestTestRunner.js

// VSCode
vim ~/.vscode-server/extensions/littlefoxteam.vscode-python-test-adapter-0.6.7/out/src/pytest/pytestTestRunner.js

And then change updatedPythonPath at line 124:

   124      const updatedPythonPath = [
+               process.env.PYTHONPATH,
   125          config.getCwd(),
   126          environment.PYTHONPATH,
   127          DISCOVERY_OUTPUT_PLUGIN_INFO.PACKAGE_PATH
   128      ].filter(item => item).join(path.delimiter);
tuky commented 3 years ago

Great to see this fixed, thanks a lot!

kondratyev-nv commented 3 years ago

@tuky JFYI I'll still need to publish a new version before this will be fixed. I'll comment on this issue after the new version will become available.

kondratyev-nv commented 3 years ago

@fsackur @tuky @bergkvist I've published a new version 0.6.8, it should be available for update. Please, update and let me know if the issue persists. Thanks to @bergkvist for the fix!

tuky commented 3 years ago

Thank you, i can confirm the issue is fixed for me now.

zhen8838 commented 3 years ago

In my remote container, still has a problem.

I install ffi python lib into /usr/local, and export env in zshrc, but pytest adapter can not load PATH /usr/local/python:/usr/local/lib, Maybe need restart container?

export LD_LIBRARY_PATH="/usr/local/lib:${LD_LIBRARY_PATH}"
export PYTHONPATH="/home/workspace/nncase/tests:/usr/local/python:/usr/local/lib:${PYTHONPATH}"

run python scirpt in terminal : 截屏2021-05-27 下午5 05 38

run python scirpt in pytest adapter collection:

collecting ... 2021-05-27 09:03:10.900024: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/lib:
2021-05-27 09:03:10.900083: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
['/home/workspace/nncase/tests/importer', '/home/workspace/nncase', '/root/.vscode-server/extensions/ms-python.python-2021.5.842923320/pythonFiles/testing_tools', '/root/.vscode-server/extensions/ms-python.python-2021.5.842923320/pythonFiles', '/home/workspace/nncase/tests', '/home/workspace/nncase', '/root/miniconda3/lib/python37.zip', '/root/miniconda3/lib/python3.7', '/root/miniconda3/lib/python3.7/lib-dynload', '/root/miniconda3/lib/python3.7/site-packages']
collected 0 items / 24 errors
.
.
tests/test_util.py:24: in <module>
    import nncase
E   ModuleNotFoundError: No module named 'nncase'