Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
RichardCoppin opened this issue Aug 4, 2020 · 5 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug investigating We are looking into the cause of the issue

Comments

@RichardCoppin
Copy link

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

@RichardCoppin RichardCoppin added triage-needed Needs assignment to the proper sub-team bug Issue identified by VS Code Team member as probable bug labels Aug 4, 2020
@karthiknadig karthiknadig self-assigned this Aug 4, 2020
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Aug 4, 2020
@karthiknadig
Copy link
Member

@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

@karthiknadig karthiknadig added investigating We are looking into the cause of the issue and removed triage labels Sep 15, 2020
@ClaasRostock
Copy link

I have the exact same issue. +1

@KartikChugh
Copy link

A very irritating issue

@eegdude
Copy link

eegdude commented Jun 26, 2021

It would be nice to add the option to put conda activation script in launch.json. This would enable using different environments for debugging. Currently you can put activation script in tasks.json, but it doesn't quite work since tasks don't use the same terminal

@karthiknadig
Copy link
Member

Closing this in favor of #11039

With #11039 we don't have to send the activation script to the terminal at all, instead we can create the terminal with right environment variables, so we won't have to run activation script each time.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug investigating We are looking into the cause of the issue
Projects
None yet
Development

No branches or pull requests

5 participants