How can I change the default virtual environment command in Visual Studio Code? - python

I have setup a virtual environment in my project folder using Command Palette: Python: Create Environment: .venv
So whenever I open vs code in that folder it attempts to start the environment using .venv/Scripts/Activate.ps1 file which fails as running scripts is disabled for Powershell. So I want to switch to command prompt as default terminal and execute .venv/Scripts/Activate.bat by default instead.
I am able to switch default terminal to cmd but vs code still tries to run the .venv/Scripts/Activate.ps1 instead of .venv/Scripts/Activate.bat. How do I switch this default command from
& <file-path>/.venv/Scripts/Activate.ps1
to
<file-path>/.venv/Scripts/Activate.bat

When you choose a virtual environment interpreter, vscode will automatically activate the environment every time you build a new terminal. This is controlled by the following settings, and the default value is true.
"python.terminal.activateEnvironment": true,
The shell command that activates the virtual environment is based on your terminal automatic change:
PowerShell is & e:/workspace/py12/.venv/Scripts/Activate.ps1
CMD is e:/workspace/py12/.venv/Scripts/activate.bat
As for you every time you build a new terminal, you will create a new PowerShell or CMD, which is controlled by the following settings
"terminal.integrated.defaultProfile.windows": "PowerShell",

Related

Is it necessary to run activate command after activating virtual environment using pipenv shell command?

As far as I know, for activating the virtual environment it's enought to just run pipenv shell
But in my case after running this command the parentheses at the beginning of the command prompt doesn't show up. Even after selecting the proper interpreter (the one inside venv folder)
To show the parentheses I should run activate command afterward.
So my question is: Is it important to show the project name in parentheses at the terminal prompt? Should I run the activate command after pipenv shell command every time?
I will appreciate if you describe the meaning of those parentheses. Tnx.
PS: I am using vscode in windows 10.
You do not need to activate the environment manually. When you select the python interpreter and open the terminal, vscode will activate it automatically for you.
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 Terminal command. In the latter case, VS Code automatically activated the selected environment.
You can read document for more details.

How to autoload venv/bin/activate in vscode on mac

I have django project folder with venv environment.
when opening vscode it has terminal opened in vscode.
Is there a way that I don't have to venv/bin/activate all the time when opening the project folder?
Edit (credit to #XJOJIX) from the comment in this answer. This will active the virtual environment without having to close or open terminals. A Python file still needs to be selected to load the Python extension.
Add this parameter in VS Code to "launch.json" or ".code-workspace"
"settings": {
"python.terminal.activateEnvInCurrentTerminal": true
}
Previous Answer:
To have a VS Code terminal automatically activate a virtual environment when first launching VS Code:
Close the terminal before exiting VS Code.
Open VS Code. Ensure a Python file is selected to direct VS Code to load the Python extension.
Wait for the Python extension to finishing loading (very bottom
left of VS Code terminal).
Open a new terminal after Python extension has loaded. The venv will automatically activate.
The trick is to open the terminal only after the Python extension has loaded.
If the directory of the terminal contains a virtual environment, VS Code will also automatically activate a virtual environment if a new terminal is opened. As before, a Python file must be selected and the Python extension must be fully loaded.
If you are on a mac OS, the simplest thing is to make ENV for your python projects.
Follow these simple Command in your Vs Code Terminal and you get your ENV activated :
Python3 -m venv env
source env/bin/activate
(The env in the first line is your env name so you can type any name)
Use the command palette to trigger the "Python: select interpreter" command. It should allow you to pick your virtual environment.
The article Using Python environments in VS Code migth be of interest to you.
If you use window machine, it might locate at
env/Scripts/activate
You can run above in your vscode terminal to activate your venv

How to activate virtual environment in Vscode when running scripts are disabled in system?

I created a virtual environment in vscode in a folder called server by typing:
python -m venv env
And I opened the server folder, select interpreter Python 3.8.1 64-bit('env':venv)
then I got following error:
I can't find any solution to this and I am stuck for hours.
It seems that it is going to activate the environment through a powershell script. And running such scripts is turned off by default. Also, usually a virtual environment is activated through cmd and .bat script. You could either turn on running powershell script or make VS Code activate an environment through cmd and .bat file.
The first way - using cmd instead of Powershell
I just checked it in my PC and VS Code doesn't use Powershell at all. It activate an environment with cmd instead of Powershell. Probably it is worth to check VS Code settings, set cmd as a default terminal. It is probably such an option in the main settings.json (you can open it through ctrl+shift+p and type 'open settings (JSON)'): "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",.
The second way - changing Powershell execution policy
In order to change Powershell execution policy you can add "terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"] to your main VS Code settings.
Also you can open a Powershell window as administrator and type the following:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Then respond y to any questions.
An update with regards to using the Command Prompt instead of Powershell in VS Code:
When implementing "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe" in settings.json it gives the notice:
This is deprecated, the new recommended way to configure your default shell is by creating a terminal profile in #terminal.integrated.profiles.windows# and setting its profile name as the default in #terminal.integrated.defaultProfile.windows#. This will currently take priority over the new profiles settings but that will change in the future
After taking a look at the documentation I've found that the correct alternative would be to include a line as shown below.
"terminal.integrated.defaultProfile.windows": "Command Prompt"
Alternatively, you can also use the GUI as shown in the documentation. More advanced settings are also shown there.
Try Using Cmd in vscode and Run this command env(your Virtual Env name)\Scripts\activate

AppInstallerRedirector terminal

I use python 3.7.4 on my system.
I tried creating a virtual environment on VS code for a project in the cmd terminal, but as soon as I enter the command python3 -m venv myEnv the terminal name changes from cmd to AppInstallerPythonRedirector and nothing happens.
I want help figuring out why this is happening and how to fix the issue and create a new environment for my project
The command python3 -m is for macOS/Linux. If you are using Windows, it should be py -3. Steps to create and activate the virtual environment for windows:
Create the virtual environment:
py -3 -m venv .venv
Note: When you create a new virtual environment, you should be prompted by VS Code to set it as the default for your workspace folder. If selected, the environment will automatically be activated when you open a new terminal.
Active the environment:
.venv\scripts\activate
If the activate command generates the message "Activate.ps1 is not digitally signed.
You cannot run this script on the current system.", then you need to temporarily change the PowerShell execution policy to allow scripts to run (see About Execution Policies in the PowerShell documentation):
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
Select your new environment by Ctrl+Shif+P, Select Interpreter command from the Command Palette.
If you are macOS/Linux, just follow:
https://code.visualstudio.com/docs/python/environments#_global-virtual-and-conda-environments

VS Code integrated terminal not starting with virtual environment activated while the right interpreter is being selected

Platform and software versions:
Mac OS Mojave, VS Code 1.38.0, Python extension installed. Created
virtual environment in project directory using command
python3 -m env.
Modified setting in Python extension,
"python.venvPath": "bin", to handle the bin directory where the python for the virtual
environment is stored.
Situation:
When I launch VS Code using code ., and then open a python file in the folder, the interpreter selected is ./bin/python, however the integrated terminal is not set to the right python executable. If I launch a new terminal it sources the virtual environment (which may be due to the Python extension setting "python.terminal.activateEnvironment": true)
Question:
Is there a way to have the integrated terminal also have the virtual environment sourced?
Or is there a better way to have VS Code activate virtual environment created by python3 -m env .?
Thank you.
Edit:
Just reread the VS Code documentation here - https://code.visualstudio.com/docs/python/environments and this time noticed this below. Wondering if there is a way to kill the existing terminal and then launch one upon VS Code launch...
However, 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. Use the Terminal: Create New Integrated Terminal command after VS Code is running.
Two things. One, "python.venvPath" is meant to point at a directory that contains other virtual environments, not the bin/ directory that has a Python interpreter from a virtual environment. (I also don't know what python3 -m env is supposed to do; did you mean python3 -m venv?)
Two, there isn't a way to make VS Code automatically launch and complete the loading of the Python extension before VS Code creates a terminal if you have the terminal frame open at start-up.
At the time of this writting vscode now has
"python.terminal.activateEnvInCurrentTerminal": true,
so I my global settings.json, F1 > preference: Open Settings (JSON)
"python.venvPath": "D:/miniconda3/envs",
"python.terminal.activateEnvInCurrentTerminal": true,
and in my workspace's settings.json, F1 > preference: Open Workspace Settings (JSON)
"python.defaultInterpreterPath": "D:/miniconda3/envs/my-workspace-venv/python.exe"
And it works for me.

Categories