Visual Studio Code overrides python interpreter in integrated terminal - python

When launching an Integrated Terminal for Visual Studio Code for a Python project that relays on its own virtual environment, the path to the python binary rests unmodified. In other words, the output of the integrated terminal is as follows (after launching it):
user#host:~/repos/project$ source /home/user/repos/project/.env/bin/activate
(.env) user#host:~/repos/project$ which python
/usr/bin/python
(.env) user#host:~/repos/project$ which python3
/usr/bin/python3
(.env) user#host:~/repositories/karbon/kuring$ echo $PATH
/home/user/repos/project/.env/bin:/home/user/local/bin:/home/user/.cargo/bin:/home/user/local/bin:/home/user/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
The virtual environment is configured in the project settings as follows:
.vscode/settings.json:
{"python.pythonPath": ".env/bin/python",}
The interpreter has been selected in the command palette to point to the same file path.
It is strange that even running "source .env/bin/activate" in a separate terminal seems to be loading the virtual environment, but the Python path remains unaltered.
Question: shouldn't VS Code be loading the interpreter from the virtual environment instead of the one from the operating system?
Please notice than in a regular BASH terminal outside VS code, I have no problems when it comes to use the virtual environment.

You should ad the reference to your working folder within the settings.json:
.vscode/settings.json:
{"python.pythonPath": "${workspaceFolder}/.env/bin/python",}

Related

VS Code Python extension startup environment

I have recently changed computers and I can't fix this 'issue'.
I'm working with Python now and of course I have installed the official Python extension for VS Code. There are options to activate the Python virtual environment at startup as you know, but it is using the Scripts\Activate.ps1 command to do it and it fails always. It works by using manually the Scripts\activate command.
How can I set the Python extension to use the activate command instead of Activate.ps1?
My default terminal is cmd in Visual Studio Code and I'm on Windows 11.
I do not want to switch to the Powershell terminal if possible. I'm fine the CMD.
You can change the command that VS Code's Python extension uses to activate the virtual environment by modifying the python.pythonPath setting in your VS Code settings.json file. Here's how to do it:
Open VS Code and go to File -> Preferences -> Settings
Search for python.pythonPath
Scroll down to the "Python Path" section and click "Edit in settings.json"
In the settings.json file, add the following:
"python.pythonPath": "~/mypath/virtualenv/Scripts/activate"
Save and restart

Visual Code how can I activate the virtual environment automatically?

Maybe a simple question, but I cannot figure it out. I work within Visual Studio Code on a MacBook. Within a directory I create a virtual environment and have activated that with source .venv/bin/activate scripts and everything works fine.
In the terminal I see (.venv) (base) gaston#MacBook-Pro WebScraping %
When I start visual code another time and open this same directory I do not see the (.venv) so I again give manually the command to activate.
My question is there a way to activate the virtual environment automatically when opening the directory?
VS Code Default Python Interpreter Path
You can do this in a number of ways. If your goal is to have the virtual environment selected immediately at the launch of VS Code, you will have to target that virtual environment as the Python Interpreter in VSC.
https://code.visualstudio.com/docs/python/environments
System Preferences->Settings->Python: Default Interpreter Path
You'll want to point it to your virtualenv Python installation, likely something like:
envFolder/bin/python3
VS Code Launch Configuration Python Interpreter
You can also set a specific launch configuration so that VSCode will still use your system Python instance by default; however, specific launch configurations will have the virtual environment specified.
https://code.visualstudio.com/docs/python/debugging
I did something within settings and now it works.
When I open VSC now it opens the directory and the right environment, just the way I wanted it.
Took me some while to figure what finally did the trick:
I opened settings Command + , typed env in the search box. Scrolled through the results until I saw:
Python > Terminal: Activate Env In Current Terminal
and checked the checkbox. That solved my problem.

Is it possible to change a python path for a virtual environment getting from github?

After a git clone in my Visual Studio Code, I have a virtual environment.
I have runned the activate script, but when I want to run the server with my command :
python manage.py runserver
I have an error about the python path:
*No Python at 'C:\Users\B\AppData\Local\Programs\Python\Python39\python.exe'*
which is obviously the path of the original computer which has created the project.
Is there a way to redirect this path in my virtual environment or to change the python interpreter for this project ? It doesn't change with the command palette/Python:select interpreter.
I changed the file :
.venv/pyvenv.cfg
Inside I modified this variable
home = C:\Python39
it works for me (at least django has started)

Use Visual Studio Code with Miniconda on macOS

On my Mac, I installed Python using Miniconda and I have also installed various Python packages using conda. In the Mac Terminal app, my base conda environment is active and when I run Python it correctly uses the Miniconda version of Python as shown below:
In Visual Studio Code (VSCode), I have selected the Miniconda version of Python using the Python Interpreter setting. The correct version of Python and conda environment is shown at the bottom of the window:
However, when I use the built-in terminal in VSCode it still uses the macOS Python even though it shows the base conda environment is active:
I'm using the Python, One Dark Pro, and Jupyter extensions in VSCode. Also, the $PATH in the VSCode terminal and in the Mac terminal app returns the following:
# VSCode terminal
base ❯ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin:/Users/gavinw/miniconda3/bin:/Users/gavinw/miniconda3/condabin
# Mac terminal app
base ❯ echo $PATH
/Users/gavinw/miniconda3/bin:/Users/gavinw/miniconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin
How can I configure the Visual Studio Code terminal to use the Miniconda version of Python?
After selecting the Python that comes with conda, the Python interpreter will be displayed in the lower left corner of VSCode, and then use the shortcut key Ctrl+Shift+` to open a new VSCode terminal, (or click "Terminal", "New terminal") it will automatically enter the selected environment and activate it automatically.
The terminal uses whatever python environment your configured shell command is set to. It is in no way linked to the environment of VS Code
However, If you attempt to run a Python file, it will open up the terminal with the full path to the specified interpreter
These details are laid out in the docs
The Python extension uses the selected environment for running Python code (using the Python: Run Python File in Terminal command), providing language services (auto-complete, syntax checking, linting, formatting, etc.) when you have a .py file open in the editor, and opening a terminal with the Terminal: Create New Integrated Terminal command. In the latter case, VS Code automatically activated the selected environment.
Also, if using code command from an activated session...
launching VS Code from a shell in which a certain Python environment is activated does not automatically activate that environment in the default Integrated Terminal.
Otherwise, launching the desktop app directly would also not be done from an activated conda environment
You may want to check the value in your settings for "python.terminal.activateEnvironment", but I believe this only applies when you actually run a .py file

Configure Python Path in Visual Studio Code with venv running from bash on Windows

I have a virtual environment set up inside bash for Windows (Ubuntu) and VS Code installed (Windows). The Python project runs well inside the venv in bash but I would like to now debug it through Visual Code.
I'm trying to set the python.pythonPath as follows inside settings.json
{
"python.pythonPath": "C:\\repos\\myrepo\\venv\\bin\\python"
}
The error returned is "Please validate the path ..."
Note that through bash for Windows, the filesystem is accessible through /mnt/c/
As for the auto-detection of Python, it only lists the python.exe installed on the windows filesystem (which is of no use to me).
https://code.visualstudio.com/docs/python/environments explains that doing this should be sufficient but doesn't seem to work.
It doesn't work because WSL isn't officially supported yet by the extension as of version 0.8.0. See https://github.com/Microsoft/vscode-python/issues/67 to up-vote and track the issue for detecting WSL-based virtual environments.

Categories

Resources