Vscode-python: VSCode is activating Conda environment after code launcher run, instead of before.

Created on 4 Aug 2020  Â·  2Comments  Â·  Source: microsoft/vscode-python

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 (View→Output, 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

investigating type-bug

Most helpful comment

@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

All 2 comments

@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

I have the _exact_ same issue. +1

Was this page helpful?
0 / 5 - 0 ratings