4

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], vscode 1.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 via python or import torch inside ipython
  • 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 ===========================

Output of running pytorch directly

enter image description here

hdkrgr
  • 1,666
  • 1
  • 12
  • 22

1 Answers1

0

I found the following workaround: The issue does not appear when code is started from the command line with the desired env activated.

hdkrgr
  • 1,666
  • 1
  • 12
  • 22