小能豆

Working with Anaconda in Visual Studio Code

py

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 ~Anaconda3\python.exe which I assume is the Anaconda Python Environment, however, when I try to run some commands, I am getting:

PS ~\Documents\Python Scripts\vs> ~/Anaconda3/Scripts/activate
PS ~\Documents\Python Scripts\vs> 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.


阅读 76

收藏
2023-11-29

共1个答案

小能豆

It seems like there might be an issue with the activation of the Conda environment in your Visual Studio Code terminal. Here are some steps to troubleshoot and potentially resolve the problem:

1. Verify Anaconda Installation:

Make sure Anaconda is correctly installed, and the Anaconda binaries directory is in your system’s PATH. You can check this by opening a new Command Prompt window and trying the following commands:

conda --version
python --version

If the commands are not recognized, it means Anaconda is not properly added to the system PATH.

2. Restart Visual Studio Code:

After verifying the installation, close and reopen Visual Studio Code. This ensures that any changes made to the system PATH are recognized by the Visual Studio Code instance.

3. Activate Conda Environment in Terminal:

Open the integrated terminal in Visual Studio Code, and manually activate the base Conda environment using the following commands:

conda activate base

Make sure the prompt changes to (base) indicating that the base environment is activated. If this doesn’t work, it might be related to how Visual Studio Code handles the integrated terminal.

4. Configure Visual Studio Code Python Interpreter:

Open the Command Palette (Ctrl + Shift + P) and select the “Python: Select Interpreter” command. Choose the Anaconda interpreter (~Anaconda3\python.exe) from the list.

5. Debug Configuration:

Ensure that your launch.json file is configured correctly. It should look similar to the following:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "stopOnEntry": true,
      "pythonPath": "${config:python.pythonPath}"
    }
  ]
}

The "stopOnEntry": true configuration should stop at the entry point of your script.

6. Variable Inspection:

After running your script in debug mode, check the “Variables” pane in the “Run and Debug” view. You should see local variables, including the msg variable.

Additional Notes:

  • If you’re still facing issues, consider uninstalling and reinstalling the Python extension for Visual Studio Code.
  • Ensure that no conflicting Python extensions are installed.

If the problem persists, please provide additional details about any error messages you encounter.

2023-11-29