AppInstallerRedirector terminal - python

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

Related

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

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",

How can I activate my virtualenv in visual code terminal?

How can I activate my virtual environment in visual code terminal? I've tried using the workspace settings method but the settings file was empty
If you have already created your virtual environment, you can just go to its directory by running the following in the VS Code terminal:
cd /python_env/bin/
and then
source ./activate
or, you can directly run
source ./python_env/bin/activate
from your main project directory.
Please follow the steps below:
Open your project folder in VS Code
Create a new terminal
Use the following command in the terminal to create a new virtual environment
# .venv is your virtual environment name
# You can also use py -3 -m venv .venv
python -m venv .venv
After the virtual environment is generated, use the following command to activate the virtual environment
# .venv is your virtual environment name
.venv\scripts\activate
Normally, when VS Code notices that you have created a new virtual environment, a popup will prompt you to allow it to be selected for the workspace.

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

How do I run a python program in a virtual environment from windows command prompt?

I created a virtual environment for a Python program using PyCharm. I can run the program just fine straight from PyCharm terminal but when I try to run it from windows command prompt I get an error because some modules in the program are only installed in the virtual environment.
So my question is: how do I run a Python program that is in a virtual environment from the command prompt?
$ cd your_project
$ ./activate.bat
$ python helloworld.py
Explaination: virtualenv should create a .bat file in your project root directory. To run it, activate the virtual env first then run the python command.
I think you have to look after switching virtual environment on windows
may be this will help
[https://towardsdatascience.com/manage-your-python-virtual-environment-with-conda-a0d2934d5195][1]

virtualenv not working on windows cygwin

I have cygwin installed on a windows 7 machine. I'm trying to follow the instructions listed here:
https://www.tutorialspoint.com/flask/flask_environment.htm
Created a new project directory
cd into the new directory
Typed virtualenv venv, and it created the new virtual dir successfully
This next step fails to work for me.
I'm trying to activate the virtual env but its not working.
"venv\scripts\activate"
When i type that nothing happens.
USER#PCTEST~/newproj
$ venv/Scripts/activate
USER#PCTEST ~/newproj
$ which python
/cygdrive/c/python27/python
Solution
Run activate script with source or . (dot space) command:
source ./venv/Scripts/activate
# Or
. ./venv/Scripts/activate
Explanation
source runs shell commands in your current shell environment, so venv works as expected. While running script by name starts new isolated shell environment, activates venv there and closes new shell environment returning to original one.

Categories