8

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.

Natsfan
  • 4,093
  • 3
  • 22
  • 29
  • See https://code.visualstudio.com/docs/languages/python#_environments and https://code.visualstudio.com/docs/python/environments. Beyond this, there isn't enough information in your post for us to offer any useful suggestions. – jpmc26 May 10 '18 at 07:40
  • @jpmc26 i don't understand your comment. I plainly ask how do i get VScode to use python2. I explained what i did and that it didn't work. Why confusion. – Natsfan May 10 '18 at 16:35
  • You didn't try many of the things in those links. You mention trying "Python: Select Interpreter," but you don't mention what choices it provided or which ones you picked. You don't provide any diagnostic information, like what's available, whether VS Code even recognizes that the Python 2 install exists, where Python 2 is installed, any values of the variables mentioned in the above links, and so on. This question contains very little detail for diagnosing the problem. For all we can tell, your installation of VS Code might be corrupted somehow; we can't tell from what info is here. – jpmc26 May 10 '18 at 20:01
  • well i disagree. I stated i created 2 virtual environments and that didn't work. i also tried selecting a python2 interpreter from command pallett and that didn't work either. i was out of options after that. – Natsfan May 10 '18 at 21:06

4 Answers4

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

  3. Enter your version
Kayla
  • 321
  • 2
  • 4
3

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

Stefano
  • 61
  • 1
  • 6
2

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~

Evan Hsu
  • 36
  • 4
0

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

  • Code Runner still depends on what's installed on your system. The default setting is `python`, yes, but if that is configured on your system to refer to Python 3, then Code Runner uses Python 3. If you don't have a `python2` or `python3`, then that won't work. It's better to give Code Runner the full path to the correct Python interpreter. – Gino Mempin Sep 24 '22 at 22:55