humble
humble

Reputation: 2188

VS Code not running a Python file in the conda environment mentioned

I have both anaconda and VS Code installed in my system. When I start writing a script, VS Code gives me an option to chose the interpreter. I choose my particular conda environment. This works fine. However when I try to run the script using Ctrl+Alt+N or using the play button on the top right (using the extension Code Runner), the terminal that opens in the VS Code doesn't recognize that I am now working in a conda environment.

As a result, it gives an error that the module is not installed (because the module is available only in the conda environment).

When I try to manually activate the conda environment from the VS Code terminal, it doesn't work.

What is the workaround? I am using Windows.

Update: If I use cmd as the default integrated terminal in VS Code in place of PowerShell, then the command activate NAME_OF_ANACONDA_ENV works. And then, I can manually run the script. However, running the script using Ctrl+Alt+N still doesn't work; it doesn't recognize that I am in a conda env.

Upvotes: 3

Views: 4368

Answers (2)

heyobi
heyobi

Reputation: 81

After a few tries i figured out how to do it. So, this works for me.

  1. vs code settings search: code-runner.executorMap

  2. Choose User and Edit in settings.json

  3. find: "python": "python -u"

  4. change: "python": "$pythonPath -u $fullFileName"

if you encounter tempCodeRunnerFile or selection problem:

  1. vs code settings search: code-runner.ignoreSelection
  2. tick the box

Upvotes: 7

quest
quest

Reputation: 3936

On windows 10: Ctrl+Shift+P and selecting the environment gets the job done. After setting the environment, I execute by pressing Crtl+F5 to execute without debugging and F5 to debug.

Upvotes: -1

Related Questions