I'm working on a pytorch project using visual studio code and trying to use py.test.
However, when trying to discover (or run) tests using the vs-code extension, I get an error (see log) importing torch. When I run pytest from the terminal, everything works fine.
Question: What is VS-code doing differently compared to me running pytest
myself?
How can I fix this?
Thanks
Config and further info
- Win10, custom
conda
env,python=3.7
,pytest=3.8
,pytorch=0.4.1 [cuda92]
, vscode1.27.2
- When I remove some tests so that all remaining tests do not call any
torch
code, everything works fine (i.e.pytest
config inside vs-code should be correct) - Python path pointing to correct env is set in vs-code Workspace Settings
- VS Code seems to be using the correct conda env and pytest version according to Python Test Log (see below)
- Running
pytest
directly from terminal works fine (see below) - I can successfully call modules using
torch
viapython
orimport torch
insideipython
- Edit 1: I'm aware of https://github.com/pytorch/pytorch/issues/4518 and have tried some of the more recent suggestions in that thread. (However, I don't think that's really my problem, because as said above I can
import torch
just fine.) - Edit 2: I'm getting the same error when running any modules that
import torch
inside vs-code's debugger
Output of Vs-Code Pytest Test Log after failed 'Discover Tests'
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-3.8.0, py-1.6.0, pluggy-0.7.1
rootdir: c:\Users\stefan\dev\bnelearn, inifile: pytest.ini
collected 0 items / 1 errors
=================================== ERRORS ====================================
_____________ ERROR collecting bnelearn/tests/test_mechanisms.py ______________
ImportError while importing test module 'c:\Users\stefan\dev\bnelearn\bnelearn\tests\test_mechanisms.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
bnelearn\tests\test_mechanisms.py:3: in <module>
import torch
..\..\Anaconda3\envs\bnelearn\lib\site-packages\torch\__init__.py:80: in <module>
from torch._C import *
E ImportError: DLL load failed: The specified module could not be found.
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.12 seconds ===========================