How to change interpreter in Visual Studio Code? - python

I have installed on my system several Python interpreters, 2.x and 3.x versions. I am trying to prepare my work environment to allow easily switch between code written in both Python version. It is really important to have as much flexible setting in Visual Studio Code (VSC).
The problem is that I have no idea how to set VSC terminal to run code in Python 2.x. Terminal output is needed because it allows to provide user input easily.
I've tried instructions provided on VSC page, like manual interpreter's path indication in folder or workspace setting. I reinstalled Python 2.x to ensure PATH variable has been updated.
When I run code with CodeRunner extension, it always run it in Python 3.x.
Does anyone have similar issue and found how to change Python environment used by this integrated terminal?

All you have to do is press ctrl+shift+p Then will get a search bar kinda thing on top of the screen.
Then type the following command:
> python: select interpreter
You will be provided with options. Select the one you want to use.

At the bottom of the MS Code screen is an info bar that lets you know what line, col, text encoding, etc... It also shows the python interpreter you are accessing.
If you click on the text for the version of python that is running, it will open a list of available interpreters on your system. If 2.7 is in your path, you can select it.

Download and install the python version that you want
https://www.python.org/downloads/
Open the visual studio code
Click on the Bottom left for the version
Select the version that you want the code to be complied

Answers above won't change interpreter in the visual-studio's terminal as explained. You can see for yourself. (I'm on linux)
which python3
# /usr/bin/python3
You can't export a new PATH because it'll ruin your other commands. And neither you can directly change the 'python3' file via root privileges. It doesn't work that way.
Your best option is to reshape your system interpreter according to your needs.

Related

VSCode not selecting the correct python interpreter

I have a script that I need to run using a very specific python interpreter in my hard drive.
I am using vscode as the IDE and trying to select the python interpreter using the command palette.
However, since the concerned interpreter is not present, I manually search it.
However, even after that, it doesn't work as typing python in the terminal then opens the windows store for installing a python IDE. I also tried adding the interpreter's path to the environment variables but the problem still persists.
How do I solve this issue.
Thank You for the help.
The Python version you selected on VS Code only effects the editor. Right click to any file on the sidebar and select Open in Integrated Terminal. If your selected Python version is created with virtualenv it will activated and your python command works.
If not worked and you want a global Python version, you need to add Python path to your system variables to work python command. Check this:
https://geek-university.com/python/add-python-to-the-windows-path/
Btw I don't think your python.exe in FreeCad/bin directory is proper Python version
At the bottom left you can select the Python interpreter.
Here it shows you on -how to do that: Python Interpreter
Reason:
Your system environment path contains this: %USERPROFILE%\AppData\Local\Microsoft\WindowsApps.
Solution:
Open Window Search(Win) to find Manage app execution aliases. To turn off App Installer of python3 like this:
You can refer to this answer for more information.

Visual Studio Code wont find my interpreter

I have used python 3.8.5 before on this computer and one day it just can't find the interpreter. I've tried reinstalling python, reinstalling VS code, and I've tried repairing corrupt files.
In vs code
In top left, Click on view
Click on Command Palette
Click on Python: Select Interpreter
Click on your python version
If python directory does not appear, copy python.exe directory and paste it in the Python: Select Interpreter bar.
First, you need to check whether it's a problem related to 'Python' which installed on your computer. Open the cmd out of VSCode, type in 'python --version' to check the output. If you can't get the proper output, you need to check whether you have added the python path to the 'PATH' system variable.
Second, if everything works well out of VSCode. That means that's the problem of 'Python' extension. You'd better reinstall or downgrade the 'Python' extension.

Python3 seems not to be recognized in VSCode

I had Python version 2.7.17 already installed on my machine. Recently I started coding in Python and I installed VS Code to make things easier. However once I opened VSCode and ran a simple Python Hello World program, VSCode terminal gave a warning saying that
Python 2 support has ended at January 1, 2020
which means the support has already ended and suggested to install the latest Python version. So I heeded the warning and installed Python 3.8.5 on my Windows machine. The installation was completed without any issue. Afterward, when I tried the command python --version on VSCode terminal, it correctly gave the output python 2.7.17. However, when I tried the command python3 --version to check if it was correctly installed, the terminal gave no output.
Since I am a beginner and the support for Python 2 has already ended, I would like to work with Python3 from now on. However, I don't mind Python 2 staying in the machine.
Is this something should I worry about? Or should I uninstall Python 2 completely (When I checked Control Panel both versions are list under currently installed programs)?
According to your description, you can change pythonpath in vscode and still keep python2.
I assume you are using windows system and if not, please let me know. The following is the solution:
open cmd and type: where python. There should be two path, copy it which is about python3.8.5;
press Ctrl+, to open setting.json and add an entry for python.pythonPath manually inside your User Settings:
Now you can press Ctrl+Shift+P and find the option select interpreter. Click it you'll find a interpreter list. After selecting new interpreter, it is necessary to press Ctrl+Shift+` to open a new terminal for further development.
About more detailed information about pythonpath and interpreter you can refer to using python environments in vscode.
The version of Python used depends on your environment, and PATH variable.
I think you need to select the correct Python interpreter. At the bottom of your screen, there should be something that says the Python version number. If you click it, you can get a menu to select the interpreter.
Alternatively, you can hit control-shift-p, type "python interpreter", then click "select python interpreter" option.
Finally, you can alter the default Python interpreter used by changing the User settings in VS Code. Hit control-shift-p, type "open settings", and select Open Settings (JSON). Add a key to the JSON file called "python.pythonPath", so that you have a line that looks something like this:
"python.pythonPath": "full/path/to/the/python/executable/you/want/python.exe",
You might be able to get a hint as to the path to use or available isntallations of python if you type where python or which python (depending on terminal/ OS in use).
Then you can save those settings. Restart VS Code. Hopefully now the correct interpreter is selected by default.

Why doesn't the command Python not do anything in Command Prompt?

The Command Prompt does not do anything when I use the Python command to execute a module. It does not even do anything when I use it at all; it just pretends that I did not do anything, and goes back to the current working directory. Attached is a screen shot for clarity.
Apparently, if you call python from Windows' Command Prompt (at least with my home computer), it will look for any python version installed using Windows Store, so if you just type python, it will open the Windows Store for you to install.
If you have Python installed, try to call full path to executable or try to add the path containing your Python executable (in my case, C:\Python38) to the system environment variable PATH:
Search for "Edit the system environment variables".
Click the Environment variables... button (in the Advanced tab)
Edit PATH and append ;C:\Python27 to the end (substitute your Python version)
Click OK and reopen cmd for change to take effect.
Update.
I simply uninstalled and reinstalled Python. Once I did that, the program works as it should in CMD.
If you have python already installed, you should just be able to type 'python' alone then press enter. It should then give you a space to execute python code. It could be simple fix, but if not it's still worth a shot.
When its the first time you're going to install python on Windows it shows you the option Add to path unchecked, so you just need to check that option and install python, you can uninstall python and installet again with the check, this fix the problem.
I just use the IDLE, it is the easiest way to run python scripts. Just open up IDLE and click on File < Open.. < and select your file. Then make any last minute changes and click on File < Save. Click on Run < Run Module (or F5) to run the python script. Done.
This is great for people that want to use python 2.7 or just don't want to install the python application from the Microsoft store.
Sometimes the order in which the PATH environment variable is set also matters; if the Windows store variable is before the path where Python is installed then the windows store will open when you type python.

Why won't PyCharm see my libraries?

If I do something like "import selenium" (or any other kind of third party library) in a .py file and then run it from the terminal, it works just fine. But if I make a new file in PyCharm CE and do the same thing, it can't find the library / module.
How can I fix this or get it to point in the right location? I use a Macbook Pro.
You need to setup your project in PyCharm to use the Python interpreter that has your libraries:
Go to: file->settings->project->project interpreter
And select the appropriate interpreter from the dropdown. After selecting an interpreter, the window displays a list of libraries installed on that interpreter; this should further help you make the right selection.
I've faced a similar issue on Pop!_OS after installing PyCharm via Flatpak. I think the installation is somehow incomplete, as I've had these issues (among others):
Installer could not create the menu shortcut due to the lack of credentials. Unlike during a typical installation, it wouldn't ask for the password and instead I had to uncheck that option altogether.
Built-in terminal defaulted to sh. Even after changing to bash, it would not read my .bashrc and many commands were missing.
After changing the interpreter into a local virtualenv, it would just default to Python 3.7 (even though the version was actually 3.8) and it didn't see any of my installed libraries.
When I've tried to use a Docker Compose environment, IDE failed to detect Docker Compose installation.
I've eventually uninstalled PyCharm and downloaded it directly from Jetbrains website to make it work correctly.

Categories