0

I'm trying to debug a Python unit test inside Visual Studio Code, but am getting an error while importing dependencies at the top of the file:

import unittest
from scipy.io import loadmat  # exception thrown here

I get a lengthy exception message:

Exception has occurred: ImportError       (note: full exception trace is shown but execution is paused at: <module>)`
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
...

This happens only when debugging. Any ideas why? How can I fix this?

Shahar
  • 71
  • 9
  • Are you using any virtualenvs/multiple Python versions, is your terminal/vscode running in the right one? – mikulatomas Jan 28 '22 at 10:09
  • Yep, I'm using an anaconda virtual environment and the terminal is set to it. Do I need to configure this in some other places as well for debugging sessions? – Shahar Jan 28 '22 at 10:11
  • Possibly related https://stackoverflow.com/questions/43700354/conda-and-visual-studio-code-debugging – mikulatomas Jan 28 '22 at 10:12
  • Thanks @mikulatomas, of all the ideas suggested on that SO, the solution that worked was to open a cmd window, do conda activate, navigate to my project folder and start code . from that folder. I'm uneasy about accepting this hack as a solution. I feel there should be a more permanent way of doing this. – Shahar Jan 28 '22 at 10:38

1 Answers1

0

Seems like the way around is to open Anaconda Powershell and type:

cd <my project folder>
conda activate <myenv>
code .

This starts VSCode with the correct configuration. It's an ugly hack, but allows me to run and debug my unit tests.

Shahar
  • 71
  • 9