How to activate python virtual environment in VS code terminal? - python-3.x

I have created a python virtual environment using VS code editor.
a virtual environment is created successfully but I am not able to activate it.
when I am trying to activate my virtual env
I have also tried with "activate.bat"

I was trying to activate virtual env command in PowerShell which is by default selected in VS Code, that why it was giving me an error.
we can select different terminal(PowerShell, Command Prompt, Git bash) in VS code. so I have just selected cmd(Command Prompt) and now it is working.

You are using Powershell in your terminal (as denoted by PS) in the begining of every line in the terminal. You can switch to cmd and it will work. To do this, check the above asnwer by Sandeep Bhatt. If the option is not available, follow these steps,
Open commands search (use Ctrl+Shift+P or from menu View->Command Palette...) while terminal is open
In command box that appears, type "Terminal: Select Default Shell" and select it.
All the available commands which are in path will be listed. Select command prompt here.
Once this is done, you can change into cmd by clicking the plus icon shown here

You need to change the shell like here in the picture
change it to git bash or terminal of windows, and use the command "source FolderNameOfVirtualenviremenrts/Scripts/activate" (this command is for git-bash terminal)

Assuming that you're using powershell and your python virtual environment is in the 'venv' folder then you'll need to source the Activate.ps1 script by using the following command
. .\venv\Scripts\activate.ps1
The dot (.) is the equivalent of "source" command in powershell.
After sourcing Activate.ps1 the virtual environment commands activate and deactivate will work.
With newer versions of virtual environments (https://github.com/microsoft/vscode-python/issues/6931) you can also activate the environment as in the following example:
& .\venv\Scripts\Activate.ps1

This should work now if you are in powershell:
.\Venv/Scripts/Activate.ps1

It's unclear what you're using to create the virtual environment, and I presume that you're simply accessing the terminal from VSCode.
Try source activate <env_name> to start your environment.

Related

Setting up and activating virtual environment

So, I've been trying to activate my virtual environment in VScode so I could open my Django manage.py and run the server, but for some reason it doesn't seem to activate in my bash terminal. And after running it, I get a "did you forget to activate your virtual environment?". What am I missing here?
In the env folder, there should be an "activate.bat" script, that you can execute to activate your venv.
I think you should open bash in the existing directory and run Scripts\activate
I'd also like to confirm which of the virtual environment you are making use of.

VS Code cannot activate virtual environment with env\Scripts\activate.bat

I've been looking at all sorts of sources and I cannot make it happen that a virtual environment is created. I have serious problems with the Terminal. Whereas in the other sources by just typing the commands as instructed like \env\Scripts\activate.bat works perfectly, for me I just get an error saying it is not recognized as a name of a cmdlet nor a function nor a script file nor an executable program, see screenshot. It's a nightmare. I also e.g. changed the settings.json as in some suggested methods, but no luck.
enter image description here
In the virtual environment Scripts folder there are 2 activate scripts: activate.bat and activate.ps1.
.bat files aren't recognized in the terminal.
Try running just env\Scripts\activate.

Python Virtual Environment working in Command Prompt, but not in Powershell

I've been trying to get this virtual environment to work with powershell, but I keep getting this error. The virtual environment works in Command Prompt, but not in Powershell, so I've been wondering what I can do to fix that.
There are three (3) "activate" scripts in the venv scripts directory.
PS C:\venv\py39\Scripts> (Get-ChildItem -Filter 'act*').Name
activate
activate.bat
Activate.ps1
These are used in the following shells.
activate # bash
activate.bat # cmd.exe
Activate.ps1 # powershell.exe or pwsh.exe
Use the following for PowerShell.
.\Activate.ps1
The Activate.ps1 script includes a deactivate function. Therefore, no deactivate.ps1 script is needed.
Primary issue is that PowerShell - as you can see on your screenshot - doesn't load commands from the current location by default. Just as it suggests you, try to use .\activate instead of activate.
But that also might not help and lead to other issue connected to the Execution Policies. The command throws some security exceptions after trying to activate the virtual environment. And as it is completly different thing and in order to not duplicate answers, I will redirect you to this thread which should help you with that.
Sometimes I have gotten that error, instead of activate, try .\activate.ps1

How do I continue work on a django project I created with cmd and using sublime text?

How do I continue work on a django project after I switched off my laptop. I'm using sublime text to edit and my command prompt to runserver. I have created a virtual environment and also activate it at the beginning of my work.
First, CD into the directory in your command prompt where your project is.
Second, activate the virtual environment again.
Third, start the dev server.
Fourth, make changes to the code and make cool projects!

Unable to activate virtual environments on Windows 10 with Python 3.7 using cmd

I am having a problem with activating a virtual environment by the name FYP using cmd on Windows 10. I checked the bat file, which is of 1KB size.
See the below commands or image. It should have (FYP) on the start after running the activate.bat file.
C:\Users\Manish>M:
M:\>ENV\FYP\Scripts\activate.bat
M:\>
But using a similar command in PowerShell or VS code will get the environment activated.
Check the below image or command. It is showing (FYP) on the start of the command, and FYP env is now activated.
PS M:\> cd .\ENV\
PS M:\ENV> .\FYP\Scripts\activate
(FYP) PS M:\ENV> cd ..
(FYP) PS M:\> cd .\projects\
Earlier when I had Windows 7, the virtual environment was getting activated on both PowerShell and cmd as well. And I used similar commands in both cmd and PowerShell.
M:\ENV>FYP\Scripts\activate
But with Windows 10, I don't know why it is not getting activated on cmd.
Please help?
Is there any specific reason why the virtual environment is not activating.
The command line needs to know where to start. Add .\ to tell it that start in the current directory.
c:\Program Files (x86)>".\Internet Explorer\iexplore.exe"
So basically, I can easily activate it through Visual Studio Code's terminal(Powershell), using the command
<PATH_TO_VENV>\Scripts\activate
<PATH_TO_VENV>: where you have created the virtual environment.

Resources