Repository navigation
Python extension unittest discovery can't import cupy #18845
Copy link
Copy link
Closed
Labels
area-testingbugIssue identified by VS Code Team member as probable bugIssue identified by VS Code Team member as probable bugregressionBug didn't exist in a previous releaseBug didn't exist in a previous releaseverifiedVerification succeededVerification succeeded
Milestone
Description
Issue Type: Bug
Behaviour
Expected vs. Actual
Expected: Tests to be discovered correctly using unittest
Actual: ModuleNotFoundError: No module named 'cupy' in Python output on test discovery, no tests discovered.
Commenting out import cupy allows tests to be discovered with import numpy and import skimage.draw in my actual use case.
Steps to reproduce:
- Create an Anaconda environment with
cupy - Select env from (1) as interpreter in VSCode
- Create a file
src.pywithimport cupy - Create a file
test_src.pywithfrom src import * - Add the following to project settings.json:
"python.testing.unittestArgs": ["-v", "-s", ".", "-p", "test_*.py"],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
- Test discovery should fail at this point with a
ModuleNotFoundError: No module named 'cupy', including traceback.
Running test discovery at the command line works as expected using
python -m unittest discover -v -s . -p test_*.py
Commenting out import cupy allows discovery to proceed.
What's really interesting about this is I don't need any actual tests or to use import unittest to reproduce the issue.
Diagnostic data
- Python version (& distribution if applicable, e.g. Anaconda): 3.9.7
- Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Conda
- Value of the
python.languageServersetting: Pylance
User Settings
defaultLS: {"defaultLSType":"Pylance"}
downloadLanguageServer: true
envFile: "<placeholder>"
venvPath: "<placeholder>"
venvFolders: "<placeholder>"
condaPath: "<placeholder>"
pipenvPath: "<placeholder>"
poetryPath: "<placeholder>"
languageServer: "Pylance"
linting
• enabled: true
• cwd: "<placeholder>"
• Flake8Args: "<placeholder>"
• flake8Enabled: false
• flake8Path: "<placeholder>"
• lintOnSave: true
• banditArgs: "<placeholder>"
• banditEnabled: false
• banditPath: "<placeholder>"
• mypyArgs: "<placeholder>"
• mypyEnabled: false
• mypyPath: "<placeholder>"
• pycodestyleArgs: "<placeholder>"
• pycodestyleEnabled: false
• pycodestylePath: "<placeholder>"
• prospectorArgs: "<placeholder>"
• prospectorEnabled: false
• prospectorPath: "<placeholder>"
• pydocstyleArgs: "<placeholder>"
• pydocstyleEnabled: false
• pydocstylePath: "<placeholder>"
• pylamaArgs: "<placeholder>"
• pylamaEnabled: false
• pylamaPath: "<placeholder>"
• pylintArgs: "<placeholder>"
• pylintPath: "<placeholder>"
sortImports
• args: "<placeholder>"
• path: "<placeholder>"
formatting
• autopep8Args: "<placeholder>"
• autopep8Path: "<placeholder>"
• provider: "black"
• blackArgs: "<placeholder>"
• blackPath: "<placeholder>"
• yapfArgs: "<placeholder>"
• yapfPath: "<placeholder>"
testing
• cwd: "<placeholder>"
• debugPort: 3000
• nosetestArgs: "<placeholder>"
• nosetestsEnabled: undefined
• nosetestPath: "<placeholder>"
• promptToConfigure: true
• pytestArgs: "<placeholder>"
• pytestEnabled: false
• pytestPath: "<placeholder>"
• unittestArgs: "<placeholder>"
• unittestEnabled: true
• autoTestDiscoverOnSaveEnabled: true
terminal
• activateEnvironment: true
• executeInFileDir: "<placeholder>"
• launchArgs: "<placeholder>"
experiments
• enabled: false
• optInto: []
• optOutFrom: []
tensorBoard
• logDirectory: "<placeholder>"
Extension version: 2022.4.0
VS Code version: Code 1.66.0 (e18005f0f1b33c29e81d732535d8c0e47cafb0b5, 2022-03-30T05:50:14.623Z)
OS version: Windows_NT x64 10.0.19041
Restricted Mode: No
System Info
| Item | Value |
|---|---|
| CPUs | Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz (12 x 2592) |
| GPU Status | 2d_canvas: enabled canvas_oop_rasterization: disabled_off direct_rendering_display_compositor: disabled_off_ok gpu_compositing: enabled multiple_raster_threads: enabled_on oop_rasterization: enabled opengl: enabled_on rasterization: enabled raw_draw: disabled_off_ok skia_renderer: enabled_on video_decode: enabled video_encode: enabled vulkan: disabled_off webgl: enabled webgl2: enabled |
| Load (avg) | undefined |
| Memory (System) | 15.71GB (4.04GB free) |
| Process Argv | --folder-uri file:///d%3A/repos/uabrc.github.io --crash-reporter-id c8e5394e-cde4-4439-b1c1-1599e605633a |
| Screen Reader | no |
| VM | 0% |
A/B Experiments
vsliv368cf:30146710
vsreu685:30147344
python383cf:30185419
vspor879:30202332
vspor708:30202333
vspor363:30204092
vswsl492:30256859
vstes627:30244334
pythonvspyl392:30443607
pythontb:30283811
pythonvspyt551cf:30345471
pythonptprofiler:30281270
vshan820:30294714
vstes263:30335439
vscorecescf:30445987
pythondataviewer:30285071
vscod805cf:30301675
pythonvspyt200:30340761
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
vsaa593:30376534
vsc1dst:30438360
pythonvs932:30410667
wslgetstarted:30449410
vsclayoutctrt:30451275
pythonvsnew555:30457759
vscscmwlcmc:30438804
vscgsvidc:30447482
cppdebug:30465021
pynewfile477:30463512
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area-testingbugIssue identified by VS Code Team member as probable bugIssue identified by VS Code Team member as probable bugregressionBug didn't exist in a previous releaseBug didn't exist in a previous releaseverifiedVerification succeededVerification succeeded
Activity
karthiknadig commentedon Apr 1, 2022
William Warriner (@wwarriner) Can you try doing this?
code .That should work better in this scenario.
wwarriner commentedon Apr 1, 2022
Hi Karthik Nadig (@karthiknadig) I appreciate the response. I tried that just now and I get the same error.
I should add that this is new behavior for an existing workspace. I have been working fine with the same workspace since at least as far back as November 2021. The last time I worked with it, before today, was about 2-3 weeks ago, and test discovery worked as expected. I made no changes since then, and it was no longer working today.
IsaacMulberry commentedon Apr 4, 2022
I believe this is the same or very similar to an issue my team are having. After updating the Python VSCode extension tests are no longer discovered in the test explorer and the error shown is
ModuleNotFoundError: No module named 'dash'. We are also using Anaconda.The tests fine running
python -m unittestin the VSCode terminal.This issue only started since updating the extension (to v2022.4.0) and downgrading the extension from v2022.4.0 to v2022.2.1924087327 solves the issue - all tests are discovered. ie changing nothing else and only swapping extension versions "fixes" and reintroduces the error.
I believe our setup is the same as described by William Warriner (@wwarriner) so just mentioning what I've managed to look at so far.
Navigating to the repo in a conda terminal and running
code .to open VSCode does not solve the issue eitherThanks for the help.
karrtikr commentedon Apr 4, 2022
Hi everyone, can you provide the following logs? Preferably for both for the extension versions: one which works and the latest version.
Output for
Pythonin theOutputpanel (View→Output, change the drop-down the upper-right of theOutputpanel toPython)Output for
Python testin theOutputpanel (View→Output, change the drop-down the upper-right of theOutputpanel toPython test)It lists the commands we run for test discovery.
EliahKagan commentedon Apr 4, 2022
I believe I am experiencing the same issue. On my system, since version 2022.4.0 of the extension, test discovery always fails in projects that use conda environments when any module from that environment is needed. This happens both in projects that use unittest and those that use pytest. Projects that use pytest always fail with
ModuleNotFoundError: No module named 'pytest'. So it appears that test discovery is not using the project environments. Runningcode .from the project directory after activating the conda environment doesn't help and the resulting logs are the same (except timestamps).This is with VS Code 1.66.0 on Windows 10.0.19044. My conda version is 4.12.0; it is provided by miniforge. It has Python 3.9.12 in its base environment, but my projects' conda environments all use Python 3.10.4. The workaround of downgrading the extension to version 2022.2.1924087327, as IsaacMulberry did, also works for me. The project for which I've included logs below is configured to use Pytest.
.vscode/settings.json{ "python.testing.pytestArgs": [ "." ], "python.testing.unittestEnabled": false, "python.testing.pytestEnabled": true }Here are the logs from
Pythonin theOutputpanel for both versions of the extension:v2022.2.1924087327 (working)
v2022.4.0 (broken)
With both versions of the extension, all the log lines up to and including
Starting Pylance language server.appear when I start VS Code (I am starting it by runningcode .from the project directory in PowerShell with the environment activated.)The subsequent lines are appended to logs when I click Testing (the beaker icon) in the activity bar (which I expect, since that triggers test discovery).
I find that nothing is logged in
Output>Python Test Logwhen test discovery fails (or otherwise before tests run), so I haven't included that. (I don't have anything exactly calledPython testin the drop-down menu in the Output panel;Python Test Logis the closest.)I hope this may be helpful. Please let me know if I should give any further information. Thanks!
wwarriner commentedon Apr 4, 2022
Hi Kartik Raj (@karrtikr), happy to provide that info for the non-working version. This is in the context of the minimal example I started the issue with.
Python output:
There is no Python Test Log output. Presumably because no tests have been run, though I can't say for sure here.
To get info for the working version I would need to know how to roll back to an older extension version. How can I do this?
And not to derail things, maybe it is related, but I'm also seeing the issue with
numpyandmklwhen debugging tests that myself and others have seen in the past. Similar to the issue we're discussing, thenumpytest debug issue was working three weeks ago and is not now. The text for that is below.5 remaining items
IsaacMulberry commentedon Apr 5, 2022
I can confirm that changing
python.condaPathfromC:\Users\IsaacMulberry\anaconda3\Scripts\conda-script.pytoC:\Users\IsaacMulberry\anaconda3\Scripts\conda.exedoes solve the issue and all tests are discovered.Thanks Kartik Raj (@karrtikr)
wwarriner commentedon Apr 5, 2022
I can confirm also that changing
python.condaPathappropriately allows test discovery to work as expected.karrtikr commentedon Apr 6, 2022
Thanks everyone, https://github.com/microsoft/vscode-python/suites/5947251954/artifacts/203903588 contains a fix for the issue where everything should ideally work without setting
python.condaPath. You can useExtension: Install from VSIXcommand to install it. Feel free to give it a try and let me know if it's still a issue.EliahKagan commentedon Apr 6, 2022
This works for me. After deliberately bringing back the problem by clearing the value of
python.condaPathin my user settings, I installed v2022.5.2099471260-dev obtained by that link, and the problem went away again (withpython.condaPathstill no longer specified).Thanks!