Skip to content

Test prototype to implicitly use environment variables for terminal activation #20884

Closed
@karrtikr

Description

@karrtikr

Refs: #11039

Complexity: 5

Create Issue


Requirements

  • Pre release build of the python extension
  • VSCode Insiders
  • Do not use a multiroot workspace
  • Virtual or conda environment

Verification

  • Add following to user settings:
"python.experiments.optInto": ["pythonTerminalEnvVarActivation"]
  • Add following workspace folder settings:
"python.terminal.activateEnvInCurrentTerminal": true,
"python.terminal.activateEnvironment": true
  • Reload window and activate Python extension by opening a Python file.

  • Select a virtual or conda environment using Python: Select Interpreter command.

  • Open terminal, no activation commands should be sent.

  • Make sure terminal is activated using the selected environment. You can run the following script using python <script> in terminal to verify:

    import sys
    import os
    
    print('executable', sys.executable)
    print('sys.path', sys.path)
    print('PATH', os.environ['PATH'])
    Output should look something similar to this for `.venv` created via `python -m venv .venv`, make sure `PATH` begins with the activated environment, and `sys.path` has the appropriate directories as well.

    executable: C:\Users\karraj\OneDrive - Microsoft\Desktop\folders\crap\.venv\Scripts\python.exe
    sys.path: ['', 'C:\\Users\\karraj\\OneDrive - Microsoft\\Desktop\\folders\\crap\\hellozsas', 'c:\\Users\\karraj\\OneDrive - Microsoft\\Desktop\\folders\\crap\\.conda\\python38.zip', 'c:\\Users\\karraj\\OneDrive - Microsoft\\Desktop\\folders\\crap\\.conda\\DLLs', 'c:\\Users\\karraj\\OneDrive - Microsoft\\Desktop\\folders\\crap\\.conda\\lib', 'c:\\Users\\karraj\\OneDrive - Microsoft\\Desktop\\folders\\crap\\.conda', 'C:\\Users\\karraj\\OneDrive - Microsoft\\Desktop\\folders\\crap\\.venv', 'C:\\Users\\karraj\\OneDrive - Microsoft\\Desktop\\folders\\crap\\.venv\\lib\\site-packages']
    PATH: C:\Users\karraj\OneDrive - Microsoft\Desktop\folders\crap\.venv\Scripts;C:\Program Files\Common Files\Microsoft Shared\Microsoft Online Services;C:\Program Files (x86)\Common Files\Microsoft Shared\Microsoft Online Services;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Users\karraj\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\Program Files\Git LFS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\Docker\Docker\resources\bin;C:\Users\karraj\AppData\Local\Programs\Python\Python37\Scripts\;C:\Users\karraj\AppData\Local\Programs\Python\Python37\;C:\Users\karraj\AppData\Local\Programs\Python\Python39\Scripts\;C:\Users\karraj\AppData\Local\Programs\Python\Python39\;C:\Users\karraj\AppData\Local\Programs\Python\Python36\Scripts\;C:\Users\karraj\AppData\Local\Programs\Python\Python36\;C:\Users\karraj\.pyenv\pyenv-win\bin;C:\Users\karraj\.pyenv\pyenv-win\shims;C:\Program Files\Common Files\Microsoft Shared\Microsoft Online Services;C:\Program Files (x86)\Common Files\Microsoft Shared\Microsoft Online Services;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\OpenSSH;C:\Program Files\Git\cmd;C:\Users\karraj\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\Program Files\Docker\Docker\resources\bin;C:\ProgramData\DockerDesktop\version-bin;C:\Users\karraj\AppData\Local\Programs\Python\Python38\Scripts\;C:\Users\karraj\AppData\Roaming\Python\Python38\Scripts;C:\Users\karraj\AppData\Local\Programs\Python\Python38\;C:\Users\karraj\Miniconda3;C:\Users\karraj\Miniconda3\Library\mingw-w64\bin;C:\Users\karraj\Miniconda3\Library\usr\bin;C:\Users\karraj\Miniconda3\Library\bin;C:\Users\karraj\Miniconda3\Scripts;C:\Users\karraj\AppData\Local\Microsoft\WindowsApps;C:\Users\karraj\AppData\Local\Programs\Microsoft VS Code Insiders\bin;c:\Users\karraj\AppData\Roaming\Code - Insiders\User\globalStorage\ms-vscode-remote.remote-containers\cli-bin;C:\Users\karraj\AppData\Local\Microsoft\WindowsApps;C:\Users\karraj\AppData\Local\Programs\Microsoft VS Code\bin;;C:\Users\karraj\.vscode-server-launcher\bin;C:\Program Files\JetBrains\PyCharm Community Edition 2022.3.2\bin;
    

  • The prompt should be visible in most cases (create an issue if it points to the incorrect env):

    If nothing is visible, please add a comment here with the type of shell, OS and the Python logs: Prompt does not show up for terminal activation #20827.

  • Try changing interpreters, terminals should be re-activated with the new env. If a used terminal is currently open, a warning sign should appear to the right of terminal:
    image

  • Try changing shells using Select default profile command, verify terminals are re-activated.

  • Lastly, verify debugging when using "python" option works.

    • Add the following to launch.json:
    {
        "name": "Test Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal",
        "justMyCode": true,
        "python": "${command:python.interpreterPath}"
    }
    
    • Replace ${command:python.interpreterPath} with path to an interpreter. This is similar to sys.executable printed earlier, for eg. C:\Users\karraj\OneDrive - Microsoft\Desktop\folders\crap\.venv\Scripts\python.exe. Call it A.
    • Make sure to select a interpreter via Python: Select Interpreter that is different from the "python" specified earlier, call it B.
    • Debug the earlier python script using the above configuration, make sure the output is activated using interpreter A, and not B.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions