How to configure VS Code's Integrated Terminal to use the correct Python Interpreter
Asked Answered
G

6

21

TL;DR

This question is NOT asking how to select a Python Interpreter to run, or debug, code with, furthermore; this question is NOT asking anything in regard to Virtual Environment, or how to activate a Virtual Environment, all of that is working. I feel it is important to note, that the question below has been asked previously, but failed to receive a satisfying answer due to confusion over what was being asked. Community Members often think one of the questions, that I mentioned at the beginning of this question, is being asked, here is a question that's a good example of what I just wrote here.



Here is My Question:

How can a user configure their Integrated Terminal to use an Interpreter that the user specifies?

I want to be able to configure my terminal to use the interpreter in my virtual environment — their is an example of my virtual environments interpreter in the Screenshot below.

  1. from the status bar that the VENV interpreter is active, and my code runs fine.

  2. from the Windows Terminal the system interpreter is being used.

VSCode using different interpreters

The problem that is caused by this, is that I can't run pip or other packages like PyInstaller from the Integrated Terminal because its not looking inside of my VENV, and furthermore; I don't want to globally install the packages.

Another problem that occurs is that, parts of my app include version sensitive packages, and I need to control the version of the python interpreter used by the Integrated Terminal.

Greenheart answered 4/11, 2020 at 21:45 Comment(17)
What do you see in the Terminal if you, after starting python, check sys.executable? And what you would like it to be?Blowbyblow
@Mark Kortink -What happens when you reopen VSCode? Have you tried using other virtual environments? Please try to refresh the terminal several times.Knocker
@Jill Cheng - I have rebooted VSCode multiple times and restarted the terminal window multiple times, it doesn't change anything.Greenheart
@np8 - sys.executable returns my system interpreter "'C:\\Users\\mark\\AppData\\Local\\Programs\\Python\\Python37\\python.exe'". I want the terminal window to use the interpreter in my venv, but in general point it to any interpreter I chose, my venv directory is "C:\Users\mark\OneDrive\dev\babyclix\.venv\Scripts". As the screen shot shows, terminal uses 3.7.7 and venv 3.7.0.Greenheart
The "Terminal" in VS Code is just cmd.exe or Powershell (in most cases). In this case, it seems to be cmd.exe. It seems that for some reason there is a path in the PATH environment variable, which contains a folder with wrong python.exe, even after the activation of the venv. Open cmd.exe, check output of echo %PATH%. Then, run C:\Users\mark\OneDrive\dev\babyclix\.venv\Scripts\activate.bat and check echo %PATH% again. What did change? The first folder with python.exe is the one started when you run python.Blowbyblow
Check all the folders that are before C:\Users\mark\OneDrive\dev\babyclix\.venv\Scripts. There you should find the reason for the described behaviour.Blowbyblow
@np8 - The only difference between the path with venv activated is that the venv directory is put at front. The python interpreter is the same further down the path. The terminal does not seem to be searching the venv for a python interpreter.Greenheart
After activating the .venv in cmd.exe, what is output of where python? After activating .venv in VS Code terminal, what is output of where python? Is there any difference?Blowbyblow
One sinple thing that comes to my mind is that it could be that VS code does some magic behind the scenes if you select an interpreter, but does not pass the change to Terminal windows that are already open. After selecting interpreter, open a new Terminal window, and activate the .venv. Does it work then correctly?Blowbyblow
@np8 - After activating venv in cmd.exe I get the venv interpreter 3.7.0, while in VSCode I get the system one. The where python command returns nothing in cmd.exe, and the system interpreters in VSCode. Changing the interpreter to the venv one and restarting VSCode does not change the Terminal interpreter.Greenheart
A workaround for me would be to install into venv from cmd.exe, as when I run my app in VSCode it does use the venv, it is just the terminal that doesn't.Greenheart
Are you sure you activated .venv before where python in cmd.exe? It would mean that it does not find any python.exe and should throw an error if you try to run python.Blowbyblow
What is the VSCode version you are using? I use VS Code with Terminal and virtual environments every day and never ran into any problems. If I could reproduce the problem it would be easier to find a fix.Blowbyblow
@np8 - VSCode 1.50.1. Yes the venv in PowerShell is activated and where python doesn't give an error, it just returns nothing. The command python returns the venv python version. I have used VSCode and Terminal a lot without problem as well. You should be able to reproduce it by installing python3.9 or some version you don't have on your machine, including it in your path, rebooting VSCode, and type python in the Terminal to see what version it is picking up.Greenheart
Let us continue this discussion in chat.Blowbyblow
What about using pyenv to create and manage virtual environments. You can then simply add a .python-version file in your project directory and pyenv would automatically activate the correct environment in your terminal (as soon as it switches into your project directory containing the .python-version file.Saturn
You just need to activate the virtualenv in your terminal session. VSCode does that for you automatically and is the default, its on by default unless you disabled it in your python extension settings. #1) make sure you select your venv interpreter, with smart settings you can have python extension search for venv automatically. #2) enable the option to activate the virtualenvAlecto
U
0

It seems as though another integrated terminal could be configured such that a python environment setup script is executed. This would establish the python environment to the venv that is desired.

Urbanite answered 10/9, 2023 at 19:37 Comment(0)
F
0

VSCode's Integrated Terminal is either cmd.exe or PowerShell; it will simply use the first python.exe (or py.exe) found in the PATH environment variable.

This means that you want to run a script (batch file or PS1 script) upon starting the Terminal to modify the PATH env var.

In order to do that, follow the tips in this SO answer: https://mcmap.net/q/337551/-vscode-how-to-run-a-command-after-each-terminal-open

Flipflop answered 15/12, 2023 at 2:27 Comment(0)
M
0

Assuming you have both interpreters installed, lets say that interpreter 2 is located in C:\User<your_user>\AppData\Local\Programs\Python\Python311

STEPS:

  1. Create a VENV using the interpreter2 path.

    • Choose a directory for you virtual env.
    • Once inside that directory, type:

    C:\Users\<your_user>\AppData\Local\Programs\Python\Python39\python.exe -m venv /path/to/venv

    • This will create a VENV with interpreter2 inside.
  2. Use it with VSCode.

    • Open the integreated terminal inside VSCode.
    • type the command to activate the VENV you created earlier:

    /path/to/venv/Scripts/activate.bat

  3. Once you do that, your VSCode will be running the second interpreter.

PS: Bellow there is a Screenshot of my own set where I had the Default 3.11.8 version running, but I switched to the VENV using 3.11.5 enter image description here

Mutualize answered 8/3, 2024 at 16:2 Comment(0)
K
0

If you want to specify the python version before running the code

Check your initial python version by running -

python --version

Then check the path of the python executable being used by running -

python -c "import os, sys; print(os.path.dirname(sys.executable))"

If the output to the second matches your environment path (for virtualenv, path = "Working Directory\Env Name\Scripts") and the output to the first is also correct, that means you have a terminal using the correct python version from the correct environment. If any of them don't match, then you'll have to make changes.

Assertion1 - correct python version and wrong executable path
Reason1 - environment not created or activated in terminal

Assertion2 - wrong python version and correct path
Reason2 - the environment was not properly created, for myvenv see this answer for conda see this

If you don't want to go into the environment and stuff, the simplest way to select the desired interpreter is to manually enter the python executable path.

For example, I have two python version installed on my System in the Directory - C:\Users\UserName\AppData\Local\Programs\Python

enter image description here If I want to run a python code using python 3.8, I will type

C:\Users\Username\AppData\Local\Programs\Python\Python38\python.exe file_to_run.py

or with python 3.10, it will be -

C:\Users\Username\AppData\Local\Programs\Python\Python310\python.exe file.py

Reminder: If you are using VSCode, it has the nasty habit of automatically activating the existing environment in that directory. Hence, you have to check the executable path by the above method to see if your terminal is using the correct environment or not.

Kirstenkirsteni answered 16/4, 2024 at 16:56 Comment(0)
P
0

Remove all paths related to python from your environment variables then add only the version you wish VS Code to use.

You can edit your Windows PATH by typing environment in the Start Menu then clicking "Edit the system environment variables".

Piety answered 21/4, 2024 at 12:24 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Macready
E
0

If I understand correctly, you want the integrated terminal to use a certain interpreter automatically when opened. You can do that by simply adding these lines to the settings.json file:

"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
"terminal.integrated.shellArgs.windows": ["/K", "C:\\Users\\XYZ\\anaconda3\\Scripts\\activate.bat"],
"python.condaPath": "C:\\Users\\XYZ\\anaconda3\\Scripts\\conda.exe"

Basically in the first line you are saying that you want command prompt as the integrated shell. Then in the second line you add arguments when instantiating the shell, basically you call your virtual environment or conda environment. These two lines should work, if they don't then add the third line, which will instantiate a specific conda virtual environment on starting up.

Eustis answered 10/6, 2024 at 14:15 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.