101

There seems to be a problem when virtualenv is used in PowerShell.

When I try to activate my environment in PowerShell like...

env/scripts/activate

.. nothing happens. (the shell prompt should have changed as well as the PATH env. variable .)

I guess the problem is that PowerShell spawns a new cmd. process just for running the activate.bat thus rendering the changes activate.bat does to the shell dead after it completes.

Do you have any workarounds for the issue? (I'm sticking with cmd.exe for now)

utku_karatas
  • 6,163
  • 4
  • 40
  • 52

19 Answers19

154

The latest version of virtualenv supports PowerShell out-of-the-box.

Just make sure you run:

Scripts\activate.ps1

instead of

Scripts\activate

The latter will execute activate.bat, which doesn't work on PowerShell.

jsalonen
  • 29,593
  • 15
  • 91
  • 109
  • 70
    You may need to run `set-executionpolicy RemoteSigned` to allow scripts to run (mine was set to `Restricted` before) [docs](http://technet.microsoft.com/en-us/library/ee176961.aspx) – Alex L Dec 22 '12 at 08:29
  • 3
    and simply type `deactivate` to undo – Vadim K May 04 '17 at 13:35
  • Is there a way to change the color of the prompt using this approach? – null_pointer Jun 04 '17 at 03:59
  • null_pointer: The color of the powershell is most easily changed by right-clicking on the navigation bar -> Properties -> Colors. – Niko Föhr Jun 28 '17 at 08:23
  • This actually doesn't work for me even though it appears to work. Open two powershell cli windows. do pip freeze in both. in one of them create a virtualenv and activate. do pip freeze in both. see that the packages did not install into the virtualenv. – Gavin Palmer Jul 13 '18 at 18:50
  • 1
    Note that the script creates an in memory function for deactivation. It's named `deactivate`. – jpmc26 Nov 20 '18 at 21:14
  • 2
    I had to write: `./Activate.ps1` it didn't work otherwise for me. – Quitiweb Sep 09 '19 at 14:06
  • 13
    If you don't have admin privileges, you may need to run `set-executionpolicy RemoteSigned -Scope CurrentUser` instead of the command provided by Alex L – micsthepick Mar 21 '20 at 01:51
55

My original answer is now outdated. Now, just use activate.ps1 (rather than activate.bat) to activate from a Powershell environment.


Original answer:

Here's a post that contains a Powershell script that allows you to run batch files that persistently modify their environment variables. The script propagates any environment variable changes back to the calling PowerShell environment.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
46

Run the this command:

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force

followed by:

./env/Scripts/activate.ps1

That's all

Michael
  • 657
  • 4
  • 29
N.Nonkovic
  • 982
  • 8
  • 8
  • 1
    If you are using `python -m venv venv` to build your virtual environment, then the name of script would be `Activate.ps1`. – Mohammad Saad Apr 06 '22 at 17:51
24

A quick work-around would be to invoke cmd and then run your activate.bat from within the cmd session. For example:

PS C:\my_cool_env\Scripts> cmd
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\my_cool_env\Scripts>activate.bat
(my_cool_env) C:\my_cool_env\Scripts>
elliott
  • 241
  • 2
  • 3
17

Inside of the Scripts directory of your virtual environments folder there are several activation scripts that can be used depending on where you are executing the command. If you are trying to activate your virtual env from the Windows PowerShell, try using the following command:

. .\env\Scripts\activate.ps1

In the event you receive an error about the activation script being disabled on your system, you will first need to invoke an execution policy change on your system. This will need to be done as the administrator.

To do this:

1) Right click on the PowerShell application and select Run as Administrator

2) Run the following command: Set-ExecutionPolicy Unrestricted

3) Rerun the activation command: . .\env\Scripts\activate.ps1

zhijazi
  • 171
  • 1
  • 2
6

try this: . .\env\Scripts\activate.ps1 watch dots and spaces

Yihe
  • 4,094
  • 2
  • 19
  • 21
4

This error happens due to a security measure which won't let scripts be executed on your system without you having approved of it. You can do so by opening up a powershell with administrative rights (search for powershell in the main menu and select Run as administrator from the context menu) and entering:

set-executionpolicy remotesigned

for more: http://www.faqforge.com/windows/windows-powershell-running-scripts-is-disabled-on-this-system/

sanfirat
  • 300
  • 3
  • 6
4

Windows users

In Powershell:

  1. Run Powershell as an administrator
  2. copy and paste this command: set-executionpolicy remotesigned
  3. Agree to the message.

Finally, Run

your_virtualenv_name\Scripts\activate.ps1

Instead of

your_virtualenv_name\Scripts\activate.bat

In CMD Just run:

your_virtualenv_name\Scripts\activate.bat
user 451
  • 462
  • 6
  • 8
3

env\Scripts\Activate.ps1 cannot be loaded because script execution is disabled on this system.

Set-Execution Policy can be used to allow the current user to run scripts as shown below.

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

After that run...

env/Scripts/Activate.ps1
2

On Windows PowerShell, I have to type from the venv/Scripts folder :

. ./activate
Boxtell
  • 251
  • 2
  • 7
2

firstly, make sure you are allowed to run Scripts. If not activate it. Now open the powershell pip install virtualenv (if virtualenv is not installed in your system) python -m virtualenv myenv (here "myenv" is the name of your environment)

#activating the virtual environment you created: myenv/Scripts/Activate.ps1

#deactivating the virtual environment: deactivate

1

I had that problem too! and finally found out what should we do in windows...

ok, follow these steps:

1)Type powershell in search bar of windows then right click on it and select Run as Administrator

(if you have problem in that check this)

2) Run the following command in powershell: Set-ExecutionPolicy Unrestricted

3) Rerun the activation command:.\\env\Scripts\activate.ps1

(just run the exact command! be careful about name of your environment.)

and that's it!:)

1

Just 2 more suggestions:

Because it is always problematic to weaken a security policy i would advice to do it the minimal way for Powershell: invoke Powershell not as administrator but as the user who wants to use the virtualenv functions. Type "set-executionpolicy -executionpolicy unrestricted -scope currentuser". This way the policy is changed only for one user and not for the whole machine.

Secondly I would advice to download from github the sources "regisf/virtualenvwrapper-powershell". After download unpack the zip-file to a local directory and run the file "Install.ps1" inside. This will expand the Powershell profile permanently on your machine and hence enable the use of all "virtalenvwrapper-win" commands including "workon". After that you will not notice any difference in the behaviour of Powershell and the Commandshell concerning virtualenv.

joachum hue
  • 96
  • 1
  • 3
0

I wrote this quick little script to handle my activation and startup of a dev server.

$ep = Get-ExecutionPolicy

if ($ep -eq 'RemoteSigned') {

    $root = "C:\Users\ALeven\OneDrive\!code_projects\!django_projects\"

    $test = Read-Host -Prompt 'Would you like to activate the python environment? y/n'
    if ($test -eq 'y') {

        $activatestr = ($root + "\work_venv\Scripts\Activate.ps1")
        & $activatestr

    }

    $test = Read-Host -Prompt 'Would you like to run the python server? y/n'

    if ($test -eq 'y') {

        $whichserver = Read-Host -Prompt 'Enter the name of the project.'
        $path = ($root + $whichserver)
        $runserverstr = ($path + "\manage.py")
        python.exe $runserverstr runserver

    }

} else {

    Write-host "Execution Policy does not allow this script to run properly"
    Write-host "If you have the proper permissions,"
    Write-Host "Please close powershell,"
    Write-host "then right click the powershell icon and run as administrator"
    Write-host "Once in the powershell environment, execute the following:"
    Write-host "Set-ExecutionPolicy RemoteSigned -Force"

}

Enjoy.

addohm
  • 2,248
  • 3
  • 14
  • 40
0

I wrote a little script to activate it.

# Don't forget to change execution policies
# Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser
# More info https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7

if (Test-Path env:VIRTUAL_ENV) {
    deactivate  
}

$env = .\venv\Scripts\activate.ps1

# use $env to set variables, for instance $env:FLASK_APP = "main.py"

Remember to save file with PowerShell extension .ps1.

0

Windows Power shell is consider powerful and "turbo-charged" than the normal command prompt shell. In an instance you are using visual studio code (VScode), running power shell to launch Python Virtual Environment may throw error especially on windows 10, as shown below.

\Activate : File C:\users\titus\django1\Scripts\Activate.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:1

To solve this, you can do the following:

  1. Go to windows Setting
  2. Open update & Security
  3. Select "For Developers"
  4. Enable the developers mode
  5. accept all the terms

Thank you all the best

joanis
  • 10,635
  • 14
  • 30
  • 40
The Elif
  • 39
  • 1
  • 9
0

For anyone still struggling to get things going, Windows PowerShell does not load commands from the current location by default. If you cd into the location of activate.ps1, running activate.ps1 could return "The term 'activate' is not recognized as the name of a cmdlet".

In order to run activate.ps1 from the current location try:

.\activate.ps1
0
  1. Search Windows Powershell
  2. Right click, select Run as Administrator; cofirm security pop-up if needed
  3. Enter: Set-ExecutionPolicy Unrestricted Enter: A
  4. cd.\env\Scripts\activate
manoj adhikari
  • 311
  • 2
  • 13
-1

No windows 10. CMD virtualenv venv/Script/activate.bat

  • 3
    Do you mean that this does *not* work on Windows 10, or that you haven't tested this on Windows 10? It's a little bit unclear. – Tom Aarsen Jan 21 '22 at 08:55