These are my settings:
User Settings
{
"atomKeymap.promptV3Features": true,
"editor.multiCursorModifier": "ctrlCmd",
"editor.formatOnPaste": true,
"python.pythonPath": "python3",
"command": "python3",
}
Workspace Settings
{
"python.pythonPath": "${workspaceFolder}/env/bin/python3.6",
"git.ignoreLimitWarning": true
}
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "python3",
"isShellCommand": true,
"args": ["${file}"],
"showOutput": "always"
}
If I use the debug console, the version and path is right:
But the output always defaults to "python2.7", no matter what I do.
How can I fix this?
Under the view menu select 'show command pallet'. One of the commands you can then select from the command palette is 'Python: Select Interpreter'. Selecting this option will allow you to choose which version of python to use.
UPD. First, read the update #1 part in the bottom
(Try update#1 first) Looking at your screenshots I see you are using Code Runner extension. And I guess that is the way you are launching your programs.
I don't know how it works internally, but adding this line into Code-Runner extension setting.json file fixed it:
"code-runner.executorMap.python": {...
"python": "python3 -u",
...}
Found it in Code-Runner GitHub repository: https://github.com/formulahendry/vscode-code-runner/issues/366
If you type "python --help", you'll see "-u" flag stands for "unbuffered binary stdout and stderr..." - don't know why it matters here.
Update #1. This became not so convenient further - I started using python's virual environments and the solution above couldn't launch these environments, cause python3 (symlink) is always linking to the same python environment.
The solution here is to use Code-Runner's supported customized parameters, so you should change "python" line in it's settings.json to:
...
"python": "$pythonPath $fullFileName",
...
Tot's answer is what worked for me on windows 10, with a few modifications.
File -> Preferences -> Settings
Type in "python.pythonPath" in the search bar.
Change it to what you normally run python with from the command line. If you have your Path environment variable set, that's just python. If not, it's likely the full path to the executable.
This solution is for Mac and Linux:
To change your Python version from 2.7 to 3 do this:
In Vscode click on file > preferences > settings.
On the right side click on the ... (the three dots) and select (open settings.json)
In the search bar type code-runner.executorMap.
You can only change the settings on the right side.
After the last setting type a comma then
"code-runner.executorMap" and hit enter, this will copy all the settings from the default file.
Look for "python" and change the command next to it to "python3".
Save the changes and you should be good to go.
VS Code's terminal using a different python interpreter than the one you've selected
By default, it doesn't know about your interpreter, and will initialize using the default .bashrc or equivalent in the OS.
I found two relevant settings from an issue in Feb 2021.
Checking the second option Python > Terminal: Activate Environment enables automatic activation of virtual environment:
In settings.json it is called "python.terminal.activateEnvironment": true.
Update (8 Aug 2021):
Today when I opened the terminal from VS Code on Windows, it automatically inserted a line of code & C:/Users/[UserName]/[venv]/Scripts/Activate.ps1 to activate the appropriate environment associated with the selected python interpreter!
It appears the aforementioned settings is now the default behavior.
While there are changes to Terminal behavior in the release notes of July 2021 (version 1.59), I don't see virtual environment activation being explicitly mentioned.
The new behavior is documented here, in "Environments and Terminal windows".
"python.pythonPath" has been deprecated
Therefore most previous answers are outdated.
Use "python.defaultInterpreterPath" instead:
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)
If you wish to set a default python interpreter for all workspaces, open settings with Ctrl+Shift+P, Preferences: Open User Settings and search for Python: Default Interpreter Path. Otherwise, if you want to set it for only the current workspace, use Preferences: Open Workspace Settings instead.
In your case, you wish to set it to ${workspaceFolder}/env/bin/python3.6.
If you edit settings.json directly instead of using the GUI:
{
"python.defaultInterpreterPath": "${workspaceFolder}/env/bin/python3.6"
}
Detailed instructions can be found in the documentation "Manually specify an interpreter", including using environment variables as the interpreter's path.
Several of the answers here explain good approaches, but below are my top 2 recommendations.
Bottom Screen Navigation (ease of access)
I find this the quickest approach; however, it isn't always available for first-time users. If you're already using Python in VS Code, this is usually the easiest way to reach the Python: Select Interpreter menu. On the bottom left of your screen, look for "Python X.X.X". This is the currently detected/configured version of Python for your project, and clicking it brings you to the interpreter menu to change the Python version you're using. At the time of writing, I was using Python 3.9.1 as seen in the snippet below:
Command Palette
As #jmh denoted in his answer, you can also use the 'View' tab to navigate to the Command Palette. In the Command Palette, search for Python: Select Interpreter to bring about the same menu denoted above.
Happy coding!
In VSCode there are two paths of python:
Path that is used when you the python code using green play button up in the top right corner. This path can be set under CTRL+SHIFT+P Python: Select Interpreter.
Path that is used when you type "python" in the terminal, and this is in "Environment Variables" in Windows 10 (Similar locations under Linux and Mac). In Windows 10 you can choose to have several Python versions, usually under C:\Users\YourName\AppData\Local\Programs\Python\Python##. Just make sure you change Environment variables C:\Users\YourName\AppData\Local\Programs\Python\Python## and C:\Users\YourName\AppData\Local\Programs\Python\Python##\Scripts accordingly. This will also affect which pip you use, i.e. a pip that belongs to Python 3.8, or a pip that belongs to Python 3.9. Terminal in VSCode in general pertains to your default terminal I think. So in Windows 10 when you type "python" in CMD Line, it should be the same version as VSCode terminal.
For sanity purposes you should make sure that both "Python: Select Interpreter" and the system environment variables point to the same version of Python.
Bonus goodie in Windows 10. If you don't have environment variable setup, and you type 'python' in VSCode terminal, it'll point to C:\Users\YourName\AppData\Local\Microsoft\WindowsApps\python.exe, which just opens up python link in Windows AppStore 🙄.
Late answer really, if you find difficult to set the python version in VsCode,
If the interpreter didn't show the envname/bin/python or any desired path you want, then go to
VSCODE main page -->file-->preference-->settings
select the ... on the right corner side. You'll see USER SETTINGS, WORKSPACE SETTINGS, YOURAPP_NAME_SETTINGS. click on the your_app_name.
"python.pythonPath":
"/home/Jhon/AllWorksUbuntu/Projects/VX-350/envname/bin/python"
Play on the above to set the correct path. You're good to go!!!
Worked for me (linux user);
Assuming that you have other python versions installed in your system:
Kill the old terminal
Open a new terminal
In the new terminal instead of write "python" to select the interpreter write "python3" or "python3.8"
Looks like put only 'python' will always bring python 2.
Just a preface: VS code was working fine (Using Python 3.x) and seemingly out of the blue it started using Python 2.7. The input() function would not convert the input to a string and that's when I realized what was happening. Typing Python in the terminal window showed 2.7 was running.
For me....
Even though "python.pythonPath" was pointing to a seemingly correct location (C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64), one of my environment variables was pointing to C:\Users\Mike.windows-build-tools\python27.
I deleted the environment variable and reinstalled Python 3.8 from the Microsoft Store and it immediately installed. I got a message from VS Code (it was running) that 3.8 got installed. I clicked Terminal/New Terminal, typed Python and it showed version 3.8. Typed 'Python xxx.py' and the code started working as it had been.
In my case, I checked the python version using
python --version
It showed python 2.x even though my interpreter path was 3.x. So uninstalled python 2.x from my computer through control panel. Then it worked fine for me.
Updating #Isabella answer, using current VSCode 1.65.1 and current python launcher, you can use py -version, for example py -3.8.
Thus, you can make a folder .vscode containing a single file name called settings.json containing
{
"python.defaultInterpreterPath": "py -3.8",
}
In Vscode you can go to preferences > settings, then on the right menu click on the first icon which is JSON. Look for "python.pythonPath" and "python.defaultInterpreterPath" and change the path. To find python3 path, open terminal and execute python3 dummyname. Actually you'll face an error but the point is it will show you the path!
One thing that can also be missed is the shell profile you are using in your terminal where you see the wanted python version vs the profile in your VS Code.
Hold cmd + p and write >Terminal: Select default profile
Select bash or whatever you used to install the python3 version in the terminal at the global level.
Closs the terminal and VS Code and then open it again, this time VS Code will pick the correct version
from the Lower left corner, click on the python, then vscode will navigate you to all python version that installed in your system, and select the right one for you.
Windows: Use py -3.6 --version or to create virtual environment py -3.6 -m venv venv36
Installing Anaconda, then selecting it as the interpreter is an easy way to set Python to the latest version and get the extensions that will make your Jupyter Notebook working.
For Windows Users:
I was recently faced with a similar situation where my newly downloaded python version would not show in the terminal even when I had selected the correct interpreter using 'Python: Select Interpreter'.
Here are the steps I followed that finally made it work:
Go to 'Edit the system variables' in settings.
Under the 'Advanced' tab, click the 'Environment Variables' button.
In the top half of the new window that opens, you will find 'User Variables'.
Find the path of your old python version in there.
Replace it with the path of your new python version.
Now check your python version in the VScode terminal again.
Related
I want to run my Python project on my globally installed Python interpreter.
I don't want to be in the "Mosh Python Standard Library" virtual environment anymore but I can't figure out how to exit it.
This what is shown in my output window in VS Code:
Here is a picture of my VS code terminal, the venv doesn't appear to be active:
Sorry for any confusion in my post, I am new to programming.
You can go to the vs-code's command pallet and type in Python: Selected Interpreter. This will open a window which you can choose which interpreter you want your python files to debug. Or you can click on the bottom left corner's button and it will do the same.
It turns out my default python interpreter in settings.json was pointing to the virtual environment, so even though I selected the global interpreter in the bottom left it would still run on the unwanted venv
I am well aware of the thread How to execute Python code from within Visual Studio Code
But none of them shows how to get the >>> python shell running with the file imported. Cuz I would like to call functions separately.
I understand that I can get the python shell going by simply typling python in the terminal but the functions in the python file have to manually imported everytime.
Is there a way to run files in VSCode like in IDLE?
you can run the file in an interactive mode in VSC code terminal by using the parameter -i :
python -i py_file.py
Install the Python Extension, then press Ctrl+Shift+P (Command+Shift+P) and type Python: Start REPL. Click the command, and you'll get an interactive Python console.
Assuming you have the official Python extension installed, you can search the command list (Ctrl+Shift+P) for "Python interactive" and you'll see several options that allow to do different variations of this:
Not sure if this was figured out or not, but I was having a similar question. How to simply use something like IDLE (the known '>>>' shell prompt) inside VSCodesee w/o chasing down plugins and their settings? I noticed that no answer was marked confirmed and although insightful, not what I was looking for.
I use Mac and while trying different things to address this, I found that by using/copying the path in the Python Launcher settings I got what I was looking for. Then, I validated the equivalent for Windows. Please see below.
Windows:
If you are using Windows, from your Terminal pane/window in VSCode, enter the path to your python.exe, including the executable name. It is located within the "User > AppData" directory.
For example: C:\Users\YOUR_USER_ID\AppData\Local\Programs\Python\Python310\python.exe
Note that AppData is a hidden folder, so may have to change your view settings in Explorer to show Hidden Items.
MAC:
If you are using MAC, from your Terminal pane/window in VSCode, enter the path to your interpreter
For example: /usr/local/bin/python3
Note: You can also do this directly from a CMD, PowerShell or a Terminal(Mac) window without having to launch IDLE, VSCode or any other coding/scripting app.
To build on the answer already provided, you can automatically run in interactive mode by changing your settings:
Go under File>Preferences>Settings. Search for "arguments" in the search bar. Then under "Python › Terminal: Launch Args", click on the link "Edit in settings.json".
Once settings.json is open, add "-i" with the quotation marks. It should look something like that:
{
"workbench.colorTheme": "Default High Contrast",
"terminal.integrated.localEchoEnabled": "off",
"terminal.integrated.shellIntegration.enabled": false,
"python.terminal.launchArgs": [
"-i"
]
}
Now, it will pass the argument -i every time you run.
I don't like the other post.
because it involves renaming python executables.
Here's my discoveries.
Step 1. Go to System Properties. Click on Environment Variables
Step 2. Add new variables, such as PYTHON_27_HOME
PYTHON_27_HOME:%ProgramFiles%\Python27\
PYTHON_36_HOME:%ProgramFiles%\Python36\
PYTHON_HOME:%PYTHON_27_HOME%
In my case, PYTHON_27_HOME(Python 2.7) is pointing to C:\Program Files\Python27\. You can replace this with your own path to python. %PYTHON_HOME% has a default value pointing to %PYTHON_27_HOME%, which is the path for Python 2.7. That's my preference, feel free to adjust accordingly. Be aware that there're 32-bit and 64-bit python. Please use %PROGRAMFILES% for path to C:\Program Files and %PROGRAMFILES(X86)% for path to C:\Program Files (x86).
Step 3. Select PATH and click on Edit. PATH
Step 4. Click on New and add %PYTHON_HOME%. %PYTHON_HOME% will be added to PATH automatically every time you launch a command prompt.
In order to switch between different versions of python in cmd, here's the trick.
Step 5. I created a batch file with
#echo off
:: Switch Python version
DOSKEY python27=SET PATH=%PYTHON_27_HOME%;%PATH%
DOSKEY python36=SET PATH=%PYTHON_36_HOME%;%PATH%
Basically, it disables echo and creates two alias. In batch file any string after :: is the comments. Every time, python27 or python36 is called, it re-exports %PATH% with the new Python path. Save it as profile.bat. You can name it whatever you want.
Step 6.
Search for regedit (Registry Editor). Click on Edit > New > String Value. Give AutoRun as the Value name and %USERPROFILE%\profile.bat as the Value data. Here, please put your actual path value to the profile.bat we just created. So, whenever a command prompt is opened, it automatically loads profile.bat, which creates those two alias in the script.
Step 7. Close any command prompt you're using or just open a new command prompt. This is because your changes will not affect opened cmd window. Environment changes only happens to new CMD.
Step 8. Verify your results here.
If you're using different Java versions, same trick applies, too. Find my javac environment setting here.
I think the easiest way to support various versions of Python, as well as other languages, is the asdf version manager. It allows you to set a version of Python globally, as well as in each project folder. This means that you can set your Python version to dynamically change based upon the folder you're working in.
asdf version manager
I haven't used Windows for almost 20 years, but I've heard that Windows 10 sports an Ubuntu-based subsystem for Linux. I don't know if asdf will work with that, but it is worth a try. Just use the instructions for setting asdf up with bash.
TL;DR: Use a symbolic link to youre current version and put that on your path instead.
Linux has the alternatives command to switch versions globally, but I do this:
Install Python variants under C:\Python\Python38, C:\Python\Python36 etc
Set your PATH to include C:\Python\Current;C:\Python\Current\Scripts
Have a batch file like this to switch versions:
#echo off
echo 1. Python 3.8
echo 2. Python 3.6
set /p ver="Enter Version: "
if [%ver%]==[1] (
SET FOLDER=C:\Python\Python38
) ELSE if [%ver%]==[2] (
SET FOLDER=C:\Python\Python36
) ELSE (
GOTO end
)
if exist C:\Python\Current\nul (
rmdir C:\Python\Current
)
MKLINK /D C:\Python\Current %FOLDER%
:end
The only downside is that MKLINK (and hence the batch file) requires elevated privilages
I am running OS X Lion, Python 2.7, and I am trying to setup Pygame to work with PyDev in Eclipse. I set up PyDev to use a custom-installed Python (not the default one). I forced this install to use 32-bit, which works fine in the Terminal - I can import Pygame, and other modules. However, when I use it in PyDev, it gives me a no matching architecture error. It also appears to be running in 64-bit mode.
The paths to the interpreter are the same.
import sys
print ("%x" % sys.maxsize, sys.maxsize > 2**32)
prints out ('7fffffff', False) while using Terminal, but in Eclipse/PyDev it prints out ('7fffffffffffffff', True)
The two paths (using sys.executable) are:
In Terminal it is:
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
And in Eclipse it is
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
The path to the interpreter I used is: /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
I also tried manually specifying the interpreter in Terminal - Using the above path. It worked.
The default python that comes with the system is /usr/bin/python
I am using a 32-bit version of Eclipse classic as shown by an answer to this question.
Does anyone have any idea what is wrong?
You need to instruct eclipse to use 32-bit python.
Right-click on your project -> properties -> PyDen/Interpreter grammar and select "Click here to configure an interpreter not listed"
After you add your new python binary (e.g. C:\Python27\python.exe), you go back to the interpreter menu in the properties window and you select this interpreter from the corresponding drop-down menu.
I solved this by using a method describe in an answer to another question.
The answer said to go to your plugins/org.python.pydev/pysrc and open interpreterInfo.py. Then you replace all the instances of sys.executable with the path to the interpreter you want.
In my case, this meant changing them to /Library/Frameworks/Python.framework/Version/2.7/bin/python2.7
After that, open up eclipse and create a new interpreter with the same path, and it should work.
Under the Build menu, I can see 'Execute' option, but it is greyed out.
The only option available is 'Set Includes and Arguments'. When I click that both fields are already filled out. What do I have to write there?
I don't need to configure anything in Geany. I just hit F5 and the current module is executed.
Are you sure that your file is recognized as a Python source file? Which version of Geany are you using (I am using the SVN version, which is pretty stable; damn, it's rock solid stable ;-))? I have a slightly more developed configuration for Python compilation in my version, but the commands are the same and it works very well.
I had the same problem and found the following solution:
In the 'Build Options' dialogue, click on '_Execute' at the bottom of the dialogue and the _execute options become editable.
I put the full path name to my Python install in there (C:\Python27\python %F) or (C:\Python32\python %F) and it seems to work just fine.
For some unknown reason a new installed Geany at Ubuntu 19.04 (Disco Dingo) had python "%f" at Build → Set Build Commands → Execute default settings.
I solved a similar issue with python3 "%f" instead.
Also don’t forget to set the “KeyBinding”, i.e., the keyboard shortcut-key (single or combination) you press to activate “Run, Execute, Compile, Save, Find, Print, etc...
To do so;
Menu Edit → Preferences → Keyboard Shortcuts.
Here you can choose/change any Key/s to map to any action you want.
Why? Because some other Linux program might have been overridden, or might have taken the default Geany assigned map keys.
You need to set the path variables in the beginning. Or you need to set the variables in Build → set build commands.
This video can give you good clarity:
Install Geany for Python
Your compile path is looking fine. But in the execution path, give the full path of your python.exe. In my case it is...
C:\python27\python "%f"
Find the Python console path.
Now set the environment variable