Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 743
humble Avatar asked Apr 12 '26 12:04

humble


1 Answers

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
like image 63
heyobi Avatar answered Apr 14 '26 01:04

heyobi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!