【问题标题】:Import errors when debugging Python unit tests in VSCode在 VSCode 中调试 Python 单元测试时导入错误
【发布时间】:2022-01-28 10:04:23
【问题描述】:

我正在尝试在 Visual Studio Code 中调试 Python 单元测试,但在文件顶部导入依赖项时出现错误:

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

我收到一条冗长的异常消息:

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.
...

这仅在调试时发生。任何想法为什么?我该如何解决这个问题?

【问题讨论】:

  • 您是否使用任何 virtualenvs/多个 Python 版本,您的终端/vscode 是否在正确的版本中运行?
  • 是的,我正在使用 anaconda 虚拟环境,并且终端已设置为它。我是否还需要在其他一些地方配置它来调试会话?
  • 感谢@mikulatomas,在该 SO 上提出的所有想法中,有效的解决方案是打开一个 cmd 窗口,执行 conda activate,导航到我的项目文件夹并启动代码。从那个文件夹。我对接受这个 hack 作为解决方案感到不安。我觉得应该有一种更永久的方式来做到这一点。

标签: python unit-testing visual-studio-code


【解决方案1】:

似乎解决方法是打开 Anaconda Powershell 并输入:

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

这会以正确的配置启动 VSCode。这是一个丑陋的 hack,但允许我运行和调试我的单元测试。

【讨论】:

    猜你喜欢
    • 2021-03-14
    • 1970-01-01
    • 2017-12-15
    • 2021-04-09
    • 2019-06-21
    • 2019-11-04
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多