Natsfan
Natsfan

Reputation: 4847

How do I get VScode to run python 2.7.13 instead of python 3.6

I'm trying to use VScode to run some scripts using python 2.7.13 but it always seems to use python 3. First I set up 2 virtual environments. One for python 2 and one for python 3. this doesn't seem to effect VSCode. It always seems to use python 3. I know its using python 3 because I put the following 2 lines in my code:

aa=10
print aa

but I always get an error message associated with the print statement. I know if I use parenthesis in the print statement all works fine.

I also tried using the "Python: Select Interpreter" from the command palette in VSCode. This doesn't work either.

I am running on a Mac with High Sierra. I have the latest version of VScode, 1.23.

What am doing wrong? How do I get VScode to use python 2.

Upvotes: 8

Views: 43588

Answers (4)

Steven-Yan
Steven-Yan

Reputation: 3

if you download Code Runner in extensions the default run command is python and you may modify it by opening the setting and searching for code-runner.executorMap and hit Edit in settings.json you may see following

"python": "python",

by changing the right-hand side you can easily switch to python2 or pyhton3

Upvotes: 0

Kayla
Kayla

Reputation: 341

  1. enter cmd+shft+p
  2. Python: Select Interpreter

  3. Enter your version

Upvotes: 20

Stefano
Stefano

Reputation: 61

I know there is already an accepted answer but wasn't helpful for me, so i'll add mine:

  1. Install both python2 and python3 with their installer, and remember to tick "add python to the path" during the installation
  2. As pointed out here from Bruno, go the the installation folder of python 2, and copy and paste "python.exe" and rename the copy into "python2.exe", do the same things for the python3 installation folder, copy and paste "python.exe" and rename it into "python3.exe"
  3. Into Visual studio code now you can run python 3 program with "python3 name_of_the_file.py", or python 2 program with "python2 name_of_the_file.py"

Otherwise try to follow this article didn't worked for me, but i din't read it carefully and probably did some mistakes

Upvotes: 3

Evan Hsu
Evan Hsu

Reputation: 36

firstly, in vscode's terminal:

suyichengdeMacBook-Air:alltest suyicheng$ python -V
Python 2.7.10
suyichengdeMacBook-Air:alltest suyicheng$ python3 -V
Python 3.6.2

In my vscode, I can use python or python3, maybe in your computer ,it will be python2.7 or others.

then, setting, search 'python.pythonPath'.

if I want to use python3.X,I set

"python.pythonPath": "python3"

then:

File "/Users/suyicheng/bs/alltest/test.py", line 2
print 22
       ^
SyntaxError: Missing parentheses in call to 'print'

And when I set :

"python.pythonPath": "python",

It works~

Hope my poor English can help you~

Upvotes: 2

Related Questions