28

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'
SSteve
  • 10,550
  • 5
  • 46
  • 72
slinden
  • 953
  • 1
  • 11
  • 24
  • I use wing ide, but it's probably the same principle. You need to replicate the relevant environment variables, path, python path. Otherwise you are not in the virtualenv, because that's what it is. Any ide will probably allow you to do that. – Kenny Ostrom Jun 22 '18 at 12:49
  • You should activate your virtual environment and then type 'pip install library_name '. Now when you run code it should work fine. – Natsfan Jun 23 '18 at 19:10
  • 2
    @jmh The problem is not that I don't have the packages installed in the correct virtualenv. I am able to run my code in terminal but not in "output" with Code Runner, becuase somehow Code Runner doesn't run the code within the virtualenv that I have set up. – slinden Jun 25 '18 at 07:40
  • 1
    @KennyOstrom When I change the active virtualenv within VS Code, the following line in the workspace settings gets changed: "python.pythonPath": "C:\\Users\\User\\envs\\Sandbox\\Scripts\\python.exe". In this case "Sandbox" is my active virtualenv. The virtualenv activates correctly, but it is just the Code Runner extension that does not recognize it. – slinden Jun 25 '18 at 07:47

8 Answers8

23

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.

Adarsh Patel
  • 562
  • 3
  • 16
  • 2
    I just gave up on using the Code Runner extension... Now I just run my code in the terminal. First I activate the virtualenv and then I just type code . to open VS Code in the project directory. This seems to be the best way for me at the moment. Then I run the code by pressing CTRL + K + A. – slinden Jan 29 '19 at 10:52
  • In my experience the extension has never run successfully in `vents` of any kind including `conda`, `pyenv-virtualenv`. Shame. – Kambiz Jun 17 '22 at 18:27
  • Scratch that!!! @daniel-ayk provides working syntax for `pipenv` virtuals – Kambiz Jun 17 '22 at 18:38
17

A possible solution would be to set the "code-runner.runInTerminal": true in the VS Code settings, which is false by default. Doing so, Code Runner, will run the code in the shell that is configured using the "terminal.integrated.shell.windows" setting.

After that, run your script with Code Runner. This now opens a new terminal, where the python environment you have selected using VS Code's Python: Select Interpreter will be activated automatically, before executing the code. (If the environment is not activated automatically, you can do this also manually, just make sure you do it in the terminal session that was opened by Code Runner.)

nkaenzig
  • 684
  • 7
  • 14
13

First approach:

First, I suggest to set executorMap like this:

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

by setting this, every time you change your Python interpreter version in VS Code, code-runner will use the same version to execute your code.

Second approach:

Another method I used before was to use a Shebang code in the first line like this:

#! .\venv\scripts\python.exe

code-runner is compatible with the Shebang command and it will execute your code with the version of Python that you have mentioned in the first line.

Elly
  • 49
  • 1
  • 1
  • 7
ghassemi.ehsan
  • 131
  • 1
  • 5
5

If you watch this video, you can see the solution at 44.55 min if you are a mac user.

You have to define your $pythonPath. However, you don't have to define $fullFineName. It is already done for you if Code Runner installed

Add this to user setting:

"python.pythonPath": "/Users/danielaaa/miniconda3/envs/tf/bin/python",

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

tomerpacific
  • 4,704
  • 13
  • 34
  • 52
Daniel Ayk
  • 83
  • 1
  • 6
3

I added shebang line at the beginning of the file pointing at my venv interpreter location eg.

#!/Users/username/Desktop/venv/bin/python

Code runner seems to work just fine.

Andrzej
  • 31
  • 1
1

I activated CodeRunner and ran into all the same problems mentioned above.

I then proceeded to pip install requests the module that in my case was present in the venv but not globally, even though it was already present and should have been working in theory. Lo and behold, it now works fine.

I guess the takeaway is that CodeRunner doesn't pick up midstream if you install it post-create of the venv.

clickfrank
  • 11
  • 4
0

add this to your user or workspace settings

"code-runner.executorMap": {
        "python": "source $workspaceRoot/venv/bin/activate && python3 $fullFileName",
    },
"code-runner.runInTerminal": true
Max
  • 1
  • 1
0

Appending the workspace directory to PYTHONPATH before running the script worked for me:

  "code-runner.executorMap": {
    "python": "export PYTHONPATH=\"$PYTHONPATH:$workspaceRoot\";python -u $fullFileName",
  }
Elliot Waite
  • 138
  • 1
  • 8