Vscode-code-runner: Doesn't recognize venv of python

Created on 8 Dec 2017  Â·  9Comments  Â·  Source: formulahendry/vscode-code-runner

Hi.
I noticed that vs-code-runner doesn't seem to recognize venv environment in python while VS Code's terminal does so by default.

My purpose is that code runner will recognize python's venv automatically.

In my case, I created .vscode and venv file on working directory, and more detail is following:

Working Directory:
   |---main.py ...etc
   |
   |--- .vscode
   |       |--- launch.json
   |       |--- settings.json
   |
   |--- venv
          |--- bin
          |--- include
          |--- lib
          |--- lib64
          |--- pip-selfcheck.json
          |--- pyvenv.cfg

I think I need additional setting and tried it again and again with little change,

for example, I noted on settings.json as following:

 "code-runner.executorMap": {
    "python": "source $workspaceRoot/venv/bin/activate && python",
}

However, I couldn't achieve it.
What should I do for it?

The key bind of this program is fascinating, so I'd like to solve this problem indeed.

question

Most helpful comment

Too old... but maybe someone else got stuck in Windows, just add this line to your workspace settings:

"code-runner.executorMap": {"python": "call venv\\scripts\\activate.bat && python"}

All 9 comments

Hi @heruwakame , could you share the screenshot of output after you tirgger 'Run Code'?
And you may want to add $fullFileName to the setting: "python": "source $workspaceRoot/venv/bin/activate && python $fullFileName"

Hi, @formulahendry.

I'm sorry that I've forgotten the vital information to you.

Here it is.

screenshot from 2017-12-10 22-49-26

/bin/sh: 1: source: not found
I thought completely this problem is because luck of setting.
But as long as I searched, this is caused by the shell environment.

I note that I use Ubuntu 16.04 LTS, Xenial, and normally use bash.

Hi, I've just found temporary solution.
This is my memorandum, so this explain might be tedious.
If you feel so, I'm sorry that.
But I'm glad if this is useful for someone else.

Here is the solution.


Directory structure is as following.

Working Directory:
   |---main.py ...etc
   |
   |--- .vscode
   |       |--- launch.json
   |       |--- settings.json
   |
   |--- venv
          |--- bin
          |--- include
          |--- lib
          |--- lib64
          |--- pip-selfcheck.json
          |--- pyvenv.cfg

I wrote the option in .vscode/settings.json:

"code-runner.executorMap": {
        "python": "bash $workspaceRoot/.vscode/python.sh $workspaceRoot $fullFileName"
    }

In my case, I wrote this option in .vscode/settings.json, so this option is valid for only in this project.
You can write this option to validate this setting in whole VS Code, when you write it in proper file.

Then, I put shell script file in $workspaceRoot/.vscode/python.sh:

#!/bin/bash
source $1/venv/bin/activate
python $2

$1 is $workspaceRoot,
$2 is $fullFileName.

You can check where $workspaceRoot and $fullFileName point out, with writing shell script:

#!/bin/bash
echo $1
echo $2

Code runner runs code in /bin/sh environment, so source command was invalid.

This solution also proves that the cause of this problem is shell environment.

Good to know you have found a solution. You could try to run it in the VS Code Integrated Terminal:

{
    "code-runner.runInTerminal": true
}

If your Integrated Terminal is /bin/bash, your previous command should work.

Too old... but maybe someone else got stuck in Windows, just add this line to your workspace settings:

"code-runner.executorMap": {"python": "call venv\\scripts\\activate.bat && python"}

Thank you, this helped a lot. Now just need to figure out how to get the python linter to point to the virtualenv environment as well?

This also works ok:
"code-runner.executorMap": {"python": "$workspaceRoot\\venv\\Scripts\\python.exe $fullFileName"}
Would be great if code-runner could pick up venv detected by python extension, or get it from .vscode/settings.json from python.pythonPath value.

272 This actually did the trick for me, provided the interpreter is chosen through Select Interpreter

I had the same error. I simply installed python 2.7 and pip and then it worked.
sudo apt install python2.7 python-pip

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eegod picture eegod  Â·  5Comments

sstorey-nephila picture sstorey-nephila  Â·  5Comments

manikantag picture manikantag  Â·  4Comments

seiferthan picture seiferthan  Â·  4Comments

mjaniec2013 picture mjaniec2013  Â·  5Comments