8

I'm trying to add anaconda prompt to start up instead of powershell to avoid having to add python to env variables.

"terminal.integrated.shellArgs.windows": [
    <args>
]

I tried putting them into single line, splitting them "-Foo Goo" as well as "-Foo","Goo". Each version leads to either error or simply ignoring the "-Command" parameter (the lines simply get pasted, but not executed).

Zerg Overmind
  • 955
  • 2
  • 14
  • 28

5 Answers5

19

First of all, I I'd like to give a hint for everyone that uses PowerShell to use the new one.

So, with the Anaconda ready to go (and it been equal or greater than 4.6 - use conda --version) run in sequence (from base environment in cwd terminal):

conda update conda
conda init

This will update your conda root environment and the init will setup all you need to run it on both cwd and powershell.

After this, you can start any powershell (inside vscode or not) and it will be conda ready.

Look at this article for further information.

Hope it helps!

Bigous
  • 381
  • 3
  • 7
  • Simple and sweet! – Kartikeya Sharma Jun 11 '21 at 21:52
  • This worked! I'm on windows 10, only extra things I had to do was select Command Prompt as my terminal. – Francisco C Aug 21 '21 at 03:13
  • Thanks! This really helped me a lot. – CodingButStillAlive Oct 18 '21 at 13:45
  • I think this "broke" my Powershell. Got a "profile.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170." – Jesse W. Collins Nov 27 '22 at 22:34
  • @JesseW.Collins try to run `Set-ExecutionPolicy -ExecutionPolicy Unrestricted` on your powershel... this issue is because your powershell is not allowed to run scripts. Do it as administrator (open the powershell as admin). PS: This info is in the article. – Bigous Dec 06 '22 at 18:56
  • Thank you!!!! that conda init saved me. I've been trying all day to work and this finally solved for me. – imatiasmb Dec 15 '22 at 18:54
9

Thanks Zerg! Your answer worked for me but I also got a warning message to say that this approach has been depreciated. After some googling I got this working by adding a new terminal profile to settings.json.

    "terminal.integrated.profiles.windows": {
        "PowerShell (Anaconda)": {
          "source": "PowerShell",
          "args": [
            "-ExecutionPolicy"
            , "ByPass" 
            , "-NoExit"
            , "-Command"
            , "& 'C:\\Users\\<username>\\AppData\\Local\\Continuum\\anaconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\Users\\<username>\\AppData\\Local\\Continuum\\anaconda3'"
        ]
        }
    },

Then changing the default profile:

"terminal.integrated.defaultProfile.windows": "PowerShell (Anaconda)",
Michael Tagg
  • 99
  • 1
  • 2
  • Thanks for this answer, I was looking for it. However, I have a warning when changing the default profile on the settings.json. I used the + icon next to the command window with the dropdown. Now powershell (anaconda) appear as an option. I chose it as the default profile. – CaribeGirl Oct 14 '21 at 15:14
  • This did the trick! Thanks =D. As an added note, you can append a `; conda activate your_env_here` to get it to start a prompt with your specific environment. Also, might be handy to have `"python.terminal.activateEnvironment": false` set. – RagingRoosevelt Mar 14 '23 at 18:27
7

I ended up using this (although it has tendency to break).

"terminal.integrated.shellArgs.windows": [
    "-ExecutionPolicy"
    , "ByPass" 
    , "-NoExit"
    , "-Command"
    , "& 'C:\\ProgramData\\Anaconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\ProgramData\\Anaconda3'"
],
Zerg Overmind
  • 955
  • 2
  • 14
  • 28
2

From the VSCode Command Palette (Ctrl+Shift+P), select

Terminal: Select default shell

and then pick PowerShell.

Then from the Command Palette (Ctrl+Shift+P), select

Python: Select Interpreter

and pick one of the conda environments. When you now open a new terminal VSCode starts PowerShell and activates the selected environment. This is exactly what the Anaconda-Prompt does. However, you should not set a PYTHONPATH in the environment in combination with an Anaconda install. Conda activation is all you need. It not only adds the selected interpreter to the PATH, but the required libraries too.

Peter
  • 10,959
  • 2
  • 30
  • 47
2

I just installed PowerShell 7, and since I had anaconda installed before, this seems to add the starting command to the profile.ps1 automatically.

The profile.ps1 in C:\Users\USER\Documents\PowerShell (this is version 7, directory WindowsPowerShell would be the old version 5) contains:

#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
(& "C:\Users\USER\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion

enter image description here

With these automatic settings at the start of PowerShell 7, adding PowerShell 7 as a new terminal type to vsccode solved it.

This is how to add PowerShell 7 to the dropdown menu:

Enter Ctrl+Shift+P, open settings.json for the User, and add

{
    "terminal.integrated.profiles.windows": {
        "PowerShell7": {
          "path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
          "args": ["-NoProfile",
          "-noexit",
          "-file",
          "C:\\Users\\USER\\Documents\\PowerShell\\profile.ps1"]
        }
      },
      "terminal.integrated.defaultProfile.windows": "PowerShell7"
}

Then in the settings.json, press Ctrl+s and restart (!) vscode. You will see PowerShell7 as the new default terminal in the dropdown of terminal types:

enter image description here

questionto42
  • 7,175
  • 4
  • 57
  • 90