VS Code Code Runner doesn't work with virtualenvs
Asked Answered
M

8

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'
Manipular answered 21/6, 2018 at 10:59 Comment(4)
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.Adornment
You should activate your virtual environment and then type 'pip install library_name '. Now when you run code it should work fine.Bidden
@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.Manipular
@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.Manipular
U
27

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.

Urea answered 18/1, 2019 at 9:50 Comment(3)
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.Manipular
In my experience the extension has never run successfully in vents of any kind including conda, pyenv-virtualenv. Shame.Princeling
Scratch that!!! @daniel-ayk provides working syntax for pipenv virtuals 💪🏿Princeling
A
22

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.)

Alita answered 25/12, 2018 at 17:36 Comment(3)
Unfortunately, for me, it still tries to run using some default python path, and THEN initializes conda env.Bangtail
This workaround works. (seems like a bug of the extension actually...Malarkey
Looping back on this, I am getting what @Bangtail said too... seems like the best solution would be to add the venv activation command to your settings.json key code-runner.executorMapToponym
V
15

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.

Vivie answered 28/1, 2020 at 18:16 Comment(3)
First approach works!Roborant
Second option assumes user is using virtual environments. This is a bad generalization I think.Princeling
First approach works for me too!Countermark
C
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"}

Catalogue answered 13/10, 2019 at 6:10 Comment(0)
S
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.

Suboxide answered 15/1, 2020 at 9:32 Comment(0)
S
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.

Substantive answered 7/10, 2019 at 22:45 Comment(0)
B
0

add this to your user or workspace settings

"code-runner.executorMap": {
        "python": "source $workspaceRoot/venv/bin/activate && python3 $fullFileName",
    },
"code-runner.runInTerminal": true
Buckley answered 10/10, 2019 at 16:19 Comment(0)
M
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",
  }
Monohydric answered 13/7, 2020 at 0:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.