VS Code Code Runner doesn't work with virtualenvs

slinden picture slinden · Jun 21, 2018 · Viewed 10k times · Source

I can't get Code Runner extension to work with virtualenvs. Whenever I try to run code that imports a library that is installed only in the virtualenv and not in the global Python installation I get an import error. Running the exact same code in terminal works.

I am on Windows 10 and I have Python 3.6.5 installed.

The precise error I am getting is:

ModuleNotFoundError: No module named 'bs4'

Answer

Adarsh Patel picture Adarsh Patel · Jan 18, 2019

I also faced same issue.

enter image description here solution which i found best is just add this line to your user or workspace settings(whichever is suits your projects):

"code-runner.executorMap": {
    "python": "C:\\Users\\adarsh_patel\\VisualCode\\env\\Scripts\\activate.bat && python -u",
}

you have to enter your virtualenv path or you could use.

"code-runner.executorMap": {"python":"$pythonPath $fullFileName"}

enter image description here

enter image description here

hope this helps you.