Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

python - Working with Anaconda in Visual Studio Code

I am getting a bit confused here, the latest Anaconda Distribution, 2018.12 at time of writing comes with an option to install Microsoft Visual Studio Code, which is great.

When launching VSC and after Python: Select Interpreter and with a fresh install of Anaconda, I can see ~Anaconda3python.exe which I assume is the Anaconda Python Environment, however, when I try to run some commands, I am getting:

PS ~DocumentsPython Scriptsvs> ~/Anaconda3/Scripts/activate
PS ~DocumentsPython Scriptsvs> conda activate base

conda : The term 'conda' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1

Now I know that it might be related to the environment variables but I find it highly odd as during the Anaconda installation, there are specific mentions that it is not required to add the Anaconda path to the environment variables. However after the error, the integrated terminal manages to launch Python and I am able to run code.

Next in line is that I am unable to view any variables in the debugger after running a simple script, as shown in the tutorial here:

msg = "Hello World"
print(msg)

I do expect to see similar results as shown in the link such as the dunder variables, I have also updated my launch.json with stopOnEntry = True following the steps.

I would like to know if it is possible to use Visual Studio Code with Anaconda as a interpreter without registering variables from the original distribution and if I am missing out anything required.

I expected the experience to be more straight forward but also I might be missing something, I am running on Windows 10.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I beat my head on this for far too long... launching VS Code from an Anaconda Prompt both feels clunky and didn't really work for integrated Powershell terminals (the default VS Code integrated terminal on Windows)

I wanted to be able to launch VS Code from any prompt (and usually the Windows menu shortcut) and still interact with Conda.

These two methods both worked for Anaconda 2020.02, VS Code 1.44.0, on Windows 10.

Easy & Quick

Switch VS Code to use cmd.exe as the default integrated terminal shell by:

  • opening the command palette (Control-Shift-P)
  • search for Terminal: Select Default Profile
  • select Command Prompt

Harder / Powershell

  • add the location of conda to your PATH (if you did not add it via the installer). For me on an "All Users" install this is C:ProgramDataAnacondaScripts
  • from an Administrator Powershell prompt change the Powershell Execution Policy to remote signed i.e. Set-ExecutionPolicy RemoteSigned
  • open an Anaconda Prompt and run conda init powershell which will add Conda related startup to a Powershell profile.ps1 somewhere in your user's profile.

When you run VS Code and either debug a .py or launch a Powershell integrated terminal you should now see a message about "Loading personal and system profiles" and the Conda environment being prefixed on the terminal's prompt.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...