Setting up and activating virtual environment - python-3.x

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.

Related

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

Activating virtual environment environment

please for a while now i've been trying to activate virtual environment for my windows 7 machine; I've run commands like env\Scripts\activate where env is the installed virtual environment folder in the project, but each time i run the command, it gives me an error like: activate is not recognized as an internal or external command, operable program or batch file. Please what could be the problem, what should i do?
Do you have activate.bat inside Scripts folder? If yes, run that. If not, delete everything and run python -m venv env again and let me know what happens. Please note that you may change env to whatever you want.
Here it is (just google it, there are a lot of post explaining that):
https://programwithus.com/learn-to-code/Pip-and-virtualenv-on-Windows/

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.

Windows 7: What PATH to set to when using Conda

I've got anaconda installed and was able to create a Python 3.3 environment. I can switch to it and conda info -e shows that I've switched.
However, I'm confused about what to set my PATH variable to. If I hard code it to the exact env then it works, but I thought that the purpose of conda was to be able to switch easily, as well as update and maintain various environments separately.
Perhaps I misunderstood and there's no way around setting my PATH everytime...
In the Windows cmd shell, use the activate and deactivate commands to change the PATH automatically. For instance, if your environment is called python3, run python3 to "activate" (i.e., add to the PATH) the python3 environment. Use deactivate to remove it.

Resources