WoJ
WoJ

Reputation: 30035

How to configure Visual Studio Code in Windows 10 to disregard shebangs and use a Python interpreter path?

I have a Python project which is intended to run in Linux:

#!/usr/bin/env python3
def hello(a: str):
    print(f"bonjour {a}")

hello("SO")

When I edit this code on Windows with Visual Studio Code and run it, I get

[Running] /usr/bin/env python3 "d:\Seafile\dev\dev-perso\domotiqueNG\services\dispatcher\hello.py"
The system cannot find the path specified.

If Visual Studio Code intends to actually use the shebang, the error is understandable: neither env nor python3 exist.

How should I configure Visual Studio Code so that it does not take the shebang into account and rather use the C:\Python36\python.exe executable?


I found in the settings Python: Python Path and set it to my executable but with the shebang present, the shebang takes priority

I removed the shebang to try that version (this is not the solution, I need to keep the shebang there) and interestingly enough I got:

[Running] python -u "d:\Seafile\dev\dev-perso\domotiqueNG\services\dispatcher\hello.py"
  File "d:\Seafile\dev\dev-perso\domotiqueNG\services\dispatcher\hello.py", line 2
    def hello(a: str):
               ^
SyntaxError: invalid syntax

This is weird as it seems to suggest that the compiler does not recognize Python 3.6 syntax while Python 3.6 is the one which is in the path (there are two other Python 2 executable hidden on the computer, not even in the path).

Visual Studio Code suggests that 3.6 will be used:

enter image description here

So I suspect there is somewhere a setting of the Python executable path which I could tweak.

Upvotes: 5

Views: 1427

Answers (1)

WoJ
WoJ

Reputation: 30035

shebang

The use or not of the shebang can be configured in the settings: search for shebang, and then Code-runner: Respect Shebang

the version of Python

When running the code via CtrlF5 the correct interpreter was used.

When running it via AltCtrlN (Run Code), the wrong one was used.

I checked with

import sys
print(sys.executable)

and for some reason the Platform.io interpreter was shown. I do not know how it ended up under Run Code but disabling Platform.io helped. Both AltCtrlN and CtrlF5 use the correct interpreter now.

I still do not know why both ways of starting the script use different interpreters but at least the problem is solved for now.

Upvotes: 4

Related Questions