question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

VSCode is activating Conda environment after code launcher run, instead of before.

See original GitHub issue

Environment data

Version: 1.47.3 (system setup) Commit: 91899dcef7b8110878ea59626991a18c8a6a1b3e Date: 2020-07-23T13:12:49.994Z Electron: 7.3.2 Chrome: 78.0.3904.130 Node.js: 12.8.1 V8: 7.8.279.23-electron.0 OS: Windows_NT x64 10.0.19041 Python Extension: v2020.7.96456 Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.3 (Anaconda) Type of virtual environment used (N/A | venv | virtualenv | conda | …): (base) Relevant/affected Python packages and their versions: NumPy version 1.18.5

PATH: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\libnvvp;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files\Git\cmd;C:\Program Files\NVIDIA Corporation\Nsight Compute 2020.1.1\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\Microsoft VS Code\bin;%HOME%\AppData\Local\Microsoft\WindowsApps;

settings.json

{
    "python.condaPath": "c:/ProgramData/Anaconda3/Scripts/conda.exe",
    "python.pythonPath": "c:/ProgramData/Anaconda3/python.exe",    
    "python.defaultInterpreterPath": "c:/ProgramData/Anaconda3/python.exe",
    "terminal.integrated.shell.windows": "C:/Program Files/Git/bin/bash.exe",
    "terminal.integrated.shellArgs.windows": [
        "-l"
    ],
    "python.terminal.activateEnvInCurrentTerminal": true,
}

launch.json

{
    "version": "0.2.0",
    "configurations": [        
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "internalConsole"
        }
    ]
}

~/.bashrc

. C:/ProgramData/Anaconda3/etc/profile.d/conda.sh

Example Code: numpytest.py

import numpy

print('Hello World')

Expected behaviour

I would expect the conda environment to be activated before the code is debugged (or executed). however this seems to be happening in reverse.

  1. Activates Conda Environment $ c:/ProgramData/Anaconda3/Scripts/activate $ conda activate c:/ProgramData/Anaconda3

  2. Executes launcher: env c:/ProgramData/Anaconda3/python.exe c:\\Users\\...\\.vscode\\extensions\\ms-python.python-2020.7.96456\\pythonFiles\\lib\\python\\debugpy\\launcher 51465 -- c:\\Users\\...\\Documents\\numpytest.py

Actual behaviour

  1. Executes launcher: env c:/ProgramData/Anaconda3/python.exe c:\\Users\\...\\.vscode\\extensions\\ms-python.python-2020.7.96456\\pythonFiles\\lib\\python\\debugpy\\launcher 51465 -- c:\\Users\\...\\Documents\\numpytest.py

  2. Numpy throws ImportError

C:\ProgramData\Anaconda3\lib\site-packages\numpy\__init__.py:140: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
  1. Activates Conda Environment $ c:/ProgramData/Anaconda3/Scripts/activate $ conda activate c:/ProgramData/Anaconda3

  2. Manually executing the script from the command line at this point succeeds. $ python c:\\Users\\...\\Documents\\numpytest.py Hello World

Steps to reproduce:

  1. Select Default Shell
  2. Configure settings.json as above.
  3. Close all open terminals - if CMD has the /K argument this error doesn’t always show.
  4. Debug the above script (I also experience this error when running without debug)

I’m observing this behaviour both using Git-Bash as well as CMD as default shell in VSCode, but I do not see this same import error in Spyder, or in a terminal (anaconda cmd or Git-Bash). I also only see this issue when I try to import numpy modules (or any derivatives eg. pandas). The code also runs (without the error) in an integrated terminal within VScode.

Note: I do not have any other versions of Python installed.

All of the settings are straight forward the only one I’ve amended was the "terminal.integrated.shellArgs.windows" setting it to ["-l"] so that the bash terminal actually imports the ~/.bashrc, so that python can run in the shell.

I can get this to work by activating conda within a terminal, then manually executing vscode from within the same terminal, but this is less than ideal.

TL;DR:

Everything is pointing to the fact that if the conda environment was just activated before the script is called everything would just work.

Logs

This is the screenshot of the error in the UI. image

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

> pyenv root
> pyenv root
> pyenv root
> c:/ProgramData/Anaconda3/python.exe c:\Users\...\.vscode\extensions\ms-python.python-2020.7.96456\pythonFiles\pyvsc-run-isolated.py pylint --disable=all --enable=F,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,unused-wildcard-import,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode,E0001,E0011,E0012,E0100,E0101,E0102,E0103,E0104,E0105,E0107,E0108,E0110,E0111,E0112,E0113,E0114,E0115,E0116,E0117,E0118,E0202,E0203,E0211,E0213,E0236,E0237,E0238,E0239,E0240,E0241,E0301,E0302,E0303,E0401,E0402,E0601,E0602,E0603,E0604,E0611,E0632,E0633,E0701,E0702,E0703,E0704,E0710,E0711,E0712,E1003,E1101,E1102,E1111,E1120,E1121,E1123,E1124,E1125,E1126,E1127,E1128,E1129,E1130,E1131,E1132,E1133,E1134,E1135,E1136,E1137,E1138,E1139,E1200,E1201,E1205,E1206,E1300,E1301,E1302,E1303,E1304,E1305,E1306,E1310,E1700,E1701 --msg-template='{line},{column},{category},{symbol}:{msg}' --reports=n --output-format=text c:\Users\...\Documents\numpytest.py
cwd: c:\Users\...\Documents
> c:/ProgramData/Anaconda3/python.exe c:\Users\...\.vscode\extensions\ms-python.python-2020.7.96456\pythonFiles\pyvsc-run-isolated.py pylint --disable=all --enable=F,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,unused-wildcard-import,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode,E0001,E0011,E0012,E0100,E0101,E0102,E0103,E0104,E0105,E0107,E0108,E0110,E0111,E0112,E0113,E0114,E0115,E0116,E0117,E0118,E0202,E0203,E0211,E0213,E0236,E0237,E0238,E0239,E0240,E0241,E0301,E0302,E0303,E0401,E0402,E0601,E0602,E0603,E0604,E0611,E0632,E0633,E0701,E0702,E0703,E0704,E0710,E0711,E0712,E1003,E1101,E1102,E1111,E1120,E1121,E1123,E1124,E1125,E1126,E1127,E1128,E1129,E1130,E1131,E1132,E1133,E1134,E1135,E1136,E1137,E1138,E1139,E1200,E1201,E1205,E1206,E1300,E1301,E1302,E1303,E1304,E1305,E1306,E1310,E1700,E1701 --msg-template='{line},{column},{category},{symbol}:{msg}' --reports=n --output-format=text c:\Users\...\Documents\numpytest.py
cwd: c:\Users\...\Documents
##########Linting Output - pylint##########

--------------------------------------------------------------------

Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)



> c:/ProgramData/Anaconda3/Scripts/conda.exe info --json
> pyenv root
> pyenv root
> pyenv root

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
ClaasRostockcommented, Oct 20, 2020

I have the exact same issue. +1

8reactions
karthiknadigcommented, Aug 5, 2020

@RichardCoppin This is a known issue. #5559 (which is covered by a broader set of terminal issues #4568). The recommended way, until we address, this is:

  1. Disable activating terminal automatically, "python.terminal.activateEnvironment": false, and exit VSCode
  2. Open command prompt or power shell outside of VSCode.
  3. Navigate to your project or workspace directory.
  4. Activate conda there.
  5. Launch VSCode from the activated conda environment using code . or code project.code-workspace
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I activate my conda environment for VS Code python ...
Running the debugger seems to reset this. See first code block for the message I get. It looks like the terminal isn't in...
Read more >
Efficient Way to Activate Conda in VSCode - Medium
Option 2: Run “conda activate <env-name>” in terminal · CTRL+SHIFT+P open “Preference: Open User Settings (JSON)”. We choose User settings instead of Workspace ......
Read more >
Debugging configurations for Python apps in Visual Studio Code
Note: To change debugging configuration, your code must be stored in a folder. To initialize debug configurations, first select the Run view in...
Read more >
Visual Studio Code Tries To Activate Conda Base ... - ADocLib
VSCode is activating Conda environment after code launcher run, instead of before. Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.3 ( ......
Read more >
Managing Developer Environments with Conda - Interrupt
How to use Conda for managing build, test, and debug environments and ... by the shell, before and after activating a Conda environment....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found