Reputation: 55
When I code on VSCode and don't specify a python version it is set to python2.7.17
I would like to set it by default to python3.6
I tried doing so by adding this at the top of my script : #!/usr/bin/python3.6
But it is not working
#!/usr/bin/python3.6
from flask import Flask, render_template, request, session
from datetime import timedelta
import sys
print(sys.version)
This code outputs :
2.7.17 (default, Apr 15 2020, 17:20:14)
[GCC 7.5.0]
I can it output the version 3.6 ?
My settings.json looks like this :
Even with a specified path the sys.version outputs python2
Upvotes: 4
Views: 10685
Reputation: 401
Open settings and search for Python Default Interpreter Path
.
In your case, you wish to set it to /usr/bin/python3.6
. Though I would suggest using a virtual environment.
{
"python.defaultInterpreterPath": "/usr/bin/python3.6"
}
If you are using virtual environment, to ensure that the terminal is using the associated python environment, check the second option Python > Terminal: Activate Environment
:
In settings.json
it is called "python.terminal.activateEnvironment": true
.
One reason your change did not persist, was because in your screenshot, you opened your workspace's settings.json
.
To make it default, you should edit your user's settings.json
instead.
This you can access with Ctrl+Shift+P
, Preferences: Open Settings (JSON)
.
Detailed instructions can be found in the documentation "Manually specify an interpreter", including using environment variables as the interpreter's path.
Also, "python.pythonPath"
has been deprecated:
2021.6.0 (16 June 2021)
5. Added python.defaultInterpreterPath setting at workspace level when in pythonDeprecatePythonPath experiment. (#16485)
8. Show python.pythonPath deprecation prompt when in pythonDeprecatePythonPath experiment. (#16485)
2020.7.0 (16 July 2020)
9. Prompt users that we have deleted pythonPath from their workspace settings when in Deprecate PythonPath experiment. (#12533)
2020.5.0 (12 May 2020)
6. Do a one-off transfer of existing values for python.pythonPath setting to new Interpreter storage if in DeprecatePythonPath experiment. (#11052)
8. Added prompt asking users to delete python.pythonPath key from their workspace settings when in Deprecate PythonPath experiment. (#11108)
12. Rename string ${config:python.pythonPath} which is used in launch.json to refer to interpreter path set in settings, to ${config:python.interpreterPath}. (#11446)
2020.4.0 (20 April 2020)
13. Added a user setting python.defaultInterpreterPath to set up the default interpreter path when in Deprecate PythonPath experiment. (#11021)
Upvotes: 6
Reputation: 5333
In VSCode's command palette choose the option
Python: Select Interpreter
And choose the interpreter you want to use. This will automatically add the setting correctly to your settings.json
file in your .vscode
folder for your workspace.
This will take care of Python management in VSCode.
Now coming to setting the right python to be used by your os/terminal when trying to run scripts.
Since you are using linux, make sure your python is the right version thats linked
python --version
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
<< verfiy your paths correctlysudo update-alternatives --config python
python --version
again and checkIf on Windows make sure path to Python 3.8.x is set at a higher priority in your Path Environment Variable. Py3 should be above your other paths
Upvotes: 3