Why is Code Runner using the old 2.7.1 version of Python instead of 3.x on OSX?
Asked Answered
P

8

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.

Planar answered 5/11, 2013 at 19:50 Comment(0)
I
4

CodeRunner->Preferences->Languages->Run Command

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

Inshore answered 23/1, 2014 at 3:10 Comment(1)
in newest VSCode for MAC need to modify the "code-runner.executorMap" setting set python: "python3"Chapbook
E
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"

Embalm answered 26/5, 2018 at 18:46 Comment(0)
D
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.

Discord answered 2/3, 2019 at 9:19 Comment(0)
I
4

CodeRunner->Preferences->Languages->Run Command

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

Inshore answered 23/1, 2014 at 3:10 Comment(1)
in newest VSCode for MAC need to modify the "code-runner.executorMap" setting set python: "python3"Chapbook
A
2

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

python3 $filename;
Axis answered 5/11, 2013 at 19:56 Comment(0)
C
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

Cay answered 28/10, 2018 at 2:23 Comment(4)
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?Dreg
/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 wantCay
Who's talking about changing it?Dreg
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 somethingCay
M
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
Morrill answered 23/12, 2020 at 12:16 Comment(0)
E
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
Easton answered 14/1, 2022 at 8:8 Comment(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.Dreg
D
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.

Dreg answered 23/7, 2023 at 1:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.