Python Virtual Environment working in Command Prompt, but not in Powershell - python-3.x

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

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.

How to activate python virtual environment in VS code terminal?

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.

Deactivate a pipenv environment

How can I deactivate my pipenv environment?
With other tools I've been able to do something like source deactivate, but that has no affect here.
Create an environment:
pipenv --three
Activate the environment:
source $(pipenv --venv)/bin/activate
But how to deactivate?
To elaborate on Williams' answer a bit more, the expected workflow is to enter the virtualenv using pipenv shell. When you activate the virtualenv that way, the console output now indicates to use exit:
Spawning environment shell (/bin/zsh). Use 'exit' to leave.
Trying to exit the virtualenv with deactivate will leave pipenv in a confused state because you will still be in that spawned shell instance but not in an activated virtualenv.
Using the shell command exit also works.
This worked for me when using deactivate still left me with the error:
Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated.
No action taken to avoid nested environments.
After using exit I could successfully switch pipenv instances. This could well be a bug and I should probably report it.
UPDATE: See other answers below. As it has been explained, this works for virtualenv, but pipenv works differently.
Just type deactivate on the command line. See the guide here
Just type "exit", it will take you out of shell.
And if you use "deactivate", then probably you will get this error when you again try to enter in the shell.
Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated.
No action taken to avoid nested environments.
So, it is prefer to use "exit" for quick access.
Deactivate from pipenv virtual environment, you can simply run the following command.
deactivate
Please check this topic for answer from contributor perspective:
https://github.com/pypa/pipenv/issues/84#issuecomment-275056943
Spoiler :
pipenv starts a new shell session with the virtualenv pathing instead of changing the pathing in the current shell session. That is why deactivate does not work. you need to exit the shell session. the exit command or CTRL-d will do the trick.
First "deactivate" and then "exit" you will get out of the virtual env. As of now this worked for me.
for the problem caused was due to the previous shell operation in another directory so what i did was exiting my new directory by ctrl+d the exiting the terminal. After that i opened the terminal by alt+t build the directory to the intended directory.then run the pipenv shell command.

customed command not found in a new terminal

everyone.
I had a basic question want to consult, about the environment variable setting.
After closed my one existed terminal which could execute compile(make) and do customed(mksdboot) command, i can't do mksdboot command anymore(I had execute a predefined setting environment variable shell script i.e. $ . ./arndale_envsetup.sh again) in the new terminal.
Cause i am a beginner in Linux, i am not very clearly about the environment variable setting rules.
i had tried to 'su' or 'sudo' to execute mksdboot, but no luck:(
ps. I had another project needs to compile in my PC(i didn't export PATH to .bashrc, only execute export PATH when i open a new terminal every time), may it efforts the original project's environment variable?
thanks.
[UPDATED]
i tried using $source ./arndale_envsetup.sh, relative commands worked finally.
but i still did't figure out the reason between work or not work. >"<
The command
history
will list what your previous commands where.
This might give you a pointer what set the path in the way you needed it.
You could also try to see where you command is via
locate mksdboot

Resources