简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM