10

I am trying to use the newer version of Python but when I type:

import sys
print sys.version_info

I get back:

sys.version_info(major=2, minor=7, micro=1, releaselevel='final', serial=0)

In the terminal when I enter python I get:

Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin

When I enter python3 I get:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 13 2013, 13:52:24) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

As you see, I have installed Python 3.3 but no matter what I do I can't seem to actually use it in Code Runner.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
DGDD
  • 1,370
  • 7
  • 19
  • 36

8 Answers8

19

For the latest version of VS Code, you need to open settings (shift+command+p) and override the python interpreter value under code-runner.executorMap.

vscode settings showing modification of the "python" setting inside "code-runner.executorMap"

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Ardhi
  • 2,855
  • 1
  • 22
  • 31
7

In new versions of the settings.json file, just enter:

"code-runner.

(Note the ") and it should show an auto-complete list (or you can press ctrl+space) and select "code-runner.executorMap". It should show all of the run commands. Change:

"python": "python -u",

to

"python": "python3 -u",

To change your IntelliSense for error handling in python3, open the Command Palette (ctrl+shift+P) and select "Python: Select interpreter" and select python 3.x.x.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Armin Akhlagh
  • 77
  • 1
  • 2
4

CodeRunner->Preferences->Languages->Run Command

edit "python $filename" to "python3 $filename"

mzds
  • 64
  • 2
  • 6
    in newest VSCode for MAC need to modify the "code-runner.executorMap" setting set python: "python3" – d1820 Feb 15 '17 at 01:59
2

Try changing Python's "run command" in the preferences to:

python3 $filename;
Lynn
  • 10,425
  • 43
  • 75
0

Use Command+, (comma) to open the Preferences, and then make sure the Languages of Python3 have this:

enter image description here

BTW, use which python3 to make sure the path is /usr/local/bin/python3 instead of /usr/bin/python3 to ensure safety

Tzu7
  • 73
  • 6
  • Why is `/usr/bin/python3` unsafe? I don't use Mac. Is it because it might be outdated/unpatched? Or is it just that `/usr/local/bin/python3` is the path where Brew installs it or something like that? – wjandrea Jul 23 '23 at 01:24
  • `/usr/bin/python3` comes with the system, if you change it randomly, some system files based on it will make mistakes, `/usr/local/bin/python3` is downloaded by yourself, you can do whatever you want – Tzu7 Aug 22 '23 at 01:44
  • Who's talking about changing it? – wjandrea Aug 22 '23 at 01:45
  • You can do some things more safely with the `/local/` path. It is difficult to guarantee that sometimes you don’t need to reinstall python or something – Tzu7 Aug 23 '23 at 09:10
0

Crate a python file and get current sysinfo

import sys
print(sys.version_info)

Need to change to python3?

  1. open your vscode settings file
    1. mac: command+shift+p
    2. search for openSettingsJson
  2. locate the python attribute in the json object
  3. change the value to python3
  4. validate results by running the aforementioned file
  5. profit
wjandrea
  • 28,235
  • 9
  • 60
  • 81
hpl002
  • 530
  • 4
  • 7
0

Another STUPID but working hack is to set Alias in your Shell.

What Code Runner basically does is python -u filename.py

But if you goto your Shell Config file and add one line at your config file(possibly .bashrc/.zshrc or whatever you use)

i.e

alias python='python3'

now whenever coderunner executes

python -u filename.py

it is actually executing

python3 -u filename.py
b33pl0g1c
  • 1
  • 1
  • This doesn't work for me, though I'm using Linux. It seems to depend on your config, cause if I enable `code-runner.runInTerminal`, then it works (since it's running in a shell). Or, I believe if Bash is your login shell, then it might work depending on your Bash config; I just can't remember how that works exactly. – wjandrea Jul 23 '23 at 01:31
0

The command python refers to Python 2 on MacOS and other systems (probably following PEP 394).

To run your script with Python 3, one option is to add a shebang as the first line in the file, like this:

#!/usr/bin/env python3

Code Runner will use the shebang, following the setting code-runner.respectShebang.

wjandrea
  • 28,235
  • 9
  • 60
  • 81