'How can I get VSCode to correctly activate conda when running pytest?

When using Anaconda and VSCode on Windows 10, the VSCode debugger correctly activates the environment when I run/debug. But when I use the test module, it fails.

Here's an example setup:

I create an environment using the command: conda create -n sqlite_test python=3.7.3

My folder is setup like so:

./src/
  sql.py
  test_sql.py

I open a new VSCode window, and open the src folder.

The source code for sql.py is:

import sqlite3
import os

def do_sql():
    db_path = os.path.join(os.environ['TEMP'], 'test.db')
    conn = sqlite3.connect(db_path)

    print("SQL code ran successfully")
    return True


do_sql()

The source code for test_sql.py is:

import pytest
from sql import do_sql

def test_do_sql():
    assert do_sql()

In VSCode, I select the python interpreter in my sqlite_test Conda env.

If I just run sql.py, I get the no errors and the print statement prints to the console.

If I run the tests from pytest using VSCode (VSCode installs pytest into the sqlite_test environment using Pip), then I get the following error:

________________________ ERROR collecting test_sql.py _________________________
ImportError while importing test module 'c:\Users\UserName\Documents\src\tmp\sqllite\test_sql.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test_sql.py:2: in <module>
    from sql import do_sql
sql.py:1: in <module>
    import sqlite3
..\..\..\..\Anaconda3\envs\sqllite\lib\sqlite3\__init__.py:23: in <module>
    from sqlite3.dbapi2 import *
..\..\..\..\Anaconda3\envs\sqllite\lib\sqlite3\dbapi2.py:27: in <module>
    from _sqlite3 import *
E   ImportError: DLL load failed: The specified module could not be found.
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!

If I run pytest from the command line (with my conda environment activated), I get a successful test passed:

>pytest
================================================= test session starts =================================================
platform win32 -- Python 3.7.3, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\UserName\Documents\src\tmp\sqllite
collected 1 item

test_sql.py .                                                                                                    [100%]

============================================== 1 passed in 0.04 seconds ===============================================


Solution 1:[1]

Turns out this is a known bug: https://github.com/microsoft/vscode-python/issues/4300

Solution 2:[2]

Follow the issue on GitHub: https://github.com/microsoft/vscode-python/issues/10668

My work around for VS Code using remote WSL2 was to bootstrap the test execution to ensure the VS Code instance was launched from a properly activated conda environment:

.vscode/test-wrapper.sh

#!/bin/bash
echo "PyTest Wrapper Starting Bootstrap"

current_env=`conda info | grep "active environment :" | awk '{print $4}'`
if [ "${current_env}" == "pyspark" ]; then
    pytest "$@"
else
    echo "PySpark is not the current environment. Launch vscode from an activated environment."
    echo "Track this issue here: https://github.com/microsoft/vscode-python/issues/10668"
    echo "Using a native terminal: "
    echo "  conda activate pyspark"
    echo "  code"
    echo 
fi

Relevant settings: .vscode/settings.json

{
  "python.testing.unittestEnabled": false,
  "python.testing.nosetestsEnabled": false,
  "python.testing.pytestEnabled": true,
  "python.testing.pytestPath": ".vscode/test-wrapper.sh",
  "python.terminal.activateEnvironment": true,
  "python.pythonPath": "${env:HOME}/anaconda3/envs/pyspark/bin/python",
  "python.linting.pylintPath": "/${env:HOME}/anaconda3/bin/pylint",
  "python.condaPath": "${env:HOME}/anaconda3/condabin/conda"
}

For people not aware, after you set this up and reload the window make sure to select the correct interpreter, in my case its called pyspark: cntrl + shift + p Python: Select Interpreter ~/anaconda3/envs/pyspark/bin/python

If you suspect you do not have the right conda environment loaded you can use the test-wrapper.sh to debug the issue with conda env list

wrong env selected because code was not launched from a native terminal session using the desired environment:

conda env list
# conda environments:
#
base                  *  /home/name/anaconda3
pyspark                  /home/name/anaconda3/envs/pyspark

Solution 3:[3]

Update: Not sure why this was downvoted, but I can no longer reproduce the bug so this solution may be moot.


For those of you on Linux, I found a possible solution:

In the project directory, create .vscode/conda-pytest.sh:

#!/usr/bin/env bash
. /path/to/miniconda3/etc/profile.d/conda.sh
conda activate my_env && pytest "$@"

and then, in the project's .vscode/settings.json:

{
    "python.testing.pytestPath": "/path/to/project/.vscode/conda-pytest.sh"
}

Remember to set conda-pytest.sh as executable.

For Windows, I'm sure an equivalent batch/powershell script could be written in place of conda-pytest.sh.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ThwompWamp
Solution 2 steven87vt
Solution 3