166

Essentially I cannot seem to activate my virtualenv environment which I create.

I'm doing this inside of Windows PowerShell through using

scripts\activate

but I get an error message:

"cannot be loaded because the execution of scripts is disabled on this system".

Could this be because I don't carry administrator privileges on my computer?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SScode
  • 1,755
  • 3
  • 12
  • 10
  • 28
    According to [Microsoft Tech Support](http://social.technet.microsoft.com/Forums/windowsserver/en-US/964636ad-347e-4b23-8f7a-f36a558115dd/error-file-cannot-be-loaded-because-the-execution-of-scripts-is-disabled-on-this-system), setting Execution Policy to unrestrictred should help. How to do that: `Set-ExecutionPolicy Unrestricted -Force` – Kamiccolo Sep 10 '13 at 07:47
  • 1
    @Kamiccolo AWESOME! You nailed it. Thanks for you help. Great first experience for me on stackoverflow. Thankyou! – SScode Sep 10 '13 at 08:07
  • 1
    Or, if you have git bash (or mingw) installed, you could just activate it there instead in command windows powershell/command prompt – quasoft Sep 21 '15 at 18:36
  • @Kamiccolo this worked for me on Windows 7 running Python 3. Thanks! – Mona Jalal Jul 18 '16 at 17:48
  • @MonaJalal With Python3 you should use built-in `venv` which doesn't have this issue at all. – Franklin Yu Sep 17 '18 at 19:45
  • If you don't mind, running this in the classical shell cmd.exe works without any hazzle. – Wör Du Schnaffzig Feb 22 '23 at 12:31

24 Answers24

309

According to Microsoft Tech Support it might be a problem with Execution Policy Settings. To fix it, you should try executing Set-ExecutionPolicy Unrestricted -Scope Process (as mentioned in the comment section by @wtsiamruk) in your PowerShell window. This would allow running virtualenv in the current PowerShell session.

There is also another approach that is more unsafe, but recommended by MS Tech Support. This approach would be to use Set-ExecutionPolicy Unrestricted -Force (which do unleash powers to screw Your system up). However, before you use this unsafe way, be sure to check what your current ExecutionPolicy setting is by using get-ExecutionPolicy. Then, when you are done, you can revert back to this ExecutionPolicy by using Set-ExecutionPolicy %the value the get-ExecutionPolicy command gave you% -Force.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kamiccolo
  • 7,758
  • 3
  • 34
  • 47
  • 28
    (for new readers) Also make sure you use Scripts\Activate.PS1 – mak Jan 13 '15 at 19:24
  • This worked for me. Wanted to point out that I tried the directions in the embed at this link unsuccessfully: https://virtualenv.pypa.io/en/stable/userguide/#activate-script. I wonder if its just wrong or is there a difference somewhere. – ThatsAMorais May 31 '16 at 17:19
  • 6
    Or Set-ExecutionPolicy Unrestricted -Scope CurrentUser to use a non-administrative PowerShell. – mijiturka Jun 13 '18 at 15:13
  • 11
    most safe aproach is probably to run "Set-ExecutionPolicy Unrestricted -Scope Process", that would allow you to run virtualenv in current powershell session – wtsiamruk Jun 20 '19 at 18:28
  • I really think @wtsiamruk's comment should be selected as the answer. Consindering opening up your Windows for all scripts could be a serious security flaw. – pojda Nov 03 '20 at 10:04
  • updated the original answer. Thanks for the heads up and sorry for taking too long ;) – Kamiccolo Nov 04 '20 at 21:56
  • 1
    Is there any way to do this permanently? Every new powershell I open I have to lookup this command to get started. – Meekohi Feb 10 '21 at 16:09
  • @Meekohi I was also looking for this info. You can use `Set-ExecutionPolicy Unrestricted -Scope CurrentUser` to allow running of all scripts, but it is a security risk... the available options are listed here under the `-Scope` section. https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7.1#parameters – Dan Apr 10 '21 at 11:43
  • 2
    If `Set-ExecutionPolicy Unrestricted -Scope Process` works for you I think you should also run `Set-ExecutionPolicy Default -Scope Process` after activating the environment to change the execution policy back to avoid any potential issues – raresm Jul 01 '21 at 06:30
67

In PowerShell use

Scripts\activate.ps1

instead of activate.bat which doesn't work in PowerShell any more.

Also deactivate it by just typing

deactivate

at the command prompt.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt N
  • 867
  • 7
  • 6
40

On Windows, open Windows PowerShell as Administrator

Enter image description here

Set-ExecutionPolicy Unrestricted -Force

Create a virtual environment:

pip install virtualenv
virtualenv foo
cd .\foo
.\Scripts\activate

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vy Do
  • 46,709
  • 59
  • 215
  • 313
32

Open another PowerShell window as administrator and then type:

set-executionpolicy remotesigned

Then press Y and then Enter.

Now close this PowerShell window and go back to the shell you were working with. This will solve the issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abhishek Chauhan
  • 365
  • 3
  • 11
11

Set the excution policy for the process scope. After Yes [y], type the script/activate:

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adil Siddiqui
  • 111
  • 1
  • 4
  • This seems like the most reasonable and least intrusive answer so far. Could you explain a bit how it works/what it does? – JoelAZ Jun 11 '23 at 05:05
7

Another quick solution I have found here (it is applicable for Windows PowerShell only) is like this:

First run

Scripts\cmd

Then run

Scripts\activate.bat

At this position, your Virtualenv is activated. Now if you deactivate it and want to activate it again later in the same session of PowerShell, you just need to run:

Scripts\activate

There isn't any need to use cmd or activate.bat command later.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ni8mr
  • 1,725
  • 3
  • 31
  • 58
5

Below steps are working:

  1. Set python path like C:\Program Files\Python310\Scripts\ in Environment Variable
  2. Open PowerShell in Admin mode , and execute the below command:
    Set-ExecutionPolicy Unrestricted -Force
  3. Close PowerShell and repoen in Admin mode
  4. Execute the below command:
    venv\scripts\activate
phoenix
  • 7,988
  • 6
  • 39
  • 45
Koustav
  • 556
  • 5
  • 7
1

This worked for me:

You can simply open a normal (you do not need elevated access) cmd or PowerShell session (I use the embedded PowerShell terminal in Visual Studio Code) and type the following from the folder where the script file is, e.g.: .venv\Scripts\Activate.ps1:

powershell.exe -executionpolicy unrestricted -command .\Activate.ps1 -Scope CurrentUser

And then you can run the activate command after that.

Note: This will only allow that specific script to run, and only by your logged-in user.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ole Aldric
  • 725
  • 6
  • 22
1

If you don't want to change your computer's execution policy on Windows like I do, you can use a Windows command prompt instead of Windows PowerShell and just need to run

`Scripts\activate`

to activate your environment.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ianNg
  • 11
  • 1
  • This doesn't solve the OP's problem because that's what the OP was originally trying... – Joel Nov 11 '18 at 17:31
  • @Joel Uhm, cmd and powershell are two different environments. the execution rights that is limited in powershell doesn't get the same restriction when being used in cmd prompts. well at least for this "scripts\activate" – ianNg Nov 11 '18 at 18:51
  • This worked for me. You can also go straight into the cmd environment from Powershell just by typing "cmd". – RobinP Aug 24 '21 at 11:56
1

Follow these steps to the letter.

Step 1. Use Windows PowerShell as ADMINISTRATOR. (VERY IMPORTANT) and cd into the project folder. Run

virtual env

Step 2. Check in the scripts folder if you have your activate.bat file

\env\Scripts\activate.bat # It has to be there.

Step 3. If it is not, here make sure you have an Internet connection and run this again

virtual env

step 4. If the activate.bat file is there in the script folder, proceed.

step 5. Run this in your shell:

Set-ExecutionPolicy Unrestricted -Force

Step 6. To activate virtualenv on Windows, activate script is in the Scripts folder:

env\Scripts\activate.bat

Step 7. Check for the (env) at the start of each line. This shows you are on the virtual environment.

Step 8. To reactivate when you come back to the project the second time, run:

.\\env\Scripts\activate
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ochieng'
  • 129
  • 1
  • 4
1

Delete the directory for that venv you created. And create a new one using the command:

python -m venv myvenv

Then see if the activate command works.

1

Search PowerShell Right click on Windows PowerShell and Run as administrator.

Put below command and hit enter.

Set-ExecutionPolicy Unrestricted -Force

Restart you system and try to activate python virtual environment.

if your virtual environment was created successfully then it's should work.

0

In Windows you should activate the virtual environment by the following command in cmd

E:\your_environment\Scripts> activate.bat

If the environment is activated then it shows your environment name enclosed with a bracket like this:

(your_environment) E:\your_environment\Scripts>

Also we can ensure by checking with where.exe it will list our active Python environment with order of hierarchy

 (your_environment) E:\your_environment\Scripts>where.exe python

 E:\your_environment\Scripts\python.exe

 C:\Python27\python.exe

If you need to deactivate then do:

(your_environment) E:\your_environment\Scripts>deactivate.bat

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Janarthanan Ramu
  • 1,331
  • 16
  • 17
0

To install a virtual environment in Windows PowerShell only, but to activate, you'll need to run Windows PowerShell as an Administrator:

  1. pip install virtualenv
  2. virtualenv %Name of virtual environment%

It is installed now to activate it. Run PowerShell in as an administrator

  1. Set-ExecutionPolicy Unrestricted -Force
  2. .\env\Scripts\activate

To deactivate the environment

  1. .\env\Scripts\deactivate

For more help, do visit the official page: https://pypi.org/project/virtualenv/1.8.2/

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amit Gupta
  • 2,698
  • 4
  • 24
  • 37
0

If the machine 64 bit, open the x86 instance of PowerShell as Administrator and check (set) the execution policy.

Try this

Set-ExecutionPolicy Unrestricted

or

Set-ExecutionPolicy Unrestricted -Force
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yas
  • 1
0

For me on Windows 10 64 bit:

  • Open cmd as an Administrator
  • powershell
  • Type Set-ExecutionPolicy Unrestricted -Force

Voilà, reopen Visual Studio Code and start pythoninnngg.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Based on the answers found here, I created a short tutorial for this with screenshots.

  • Python 3.7
  • Windows 10 64bits
  • PowerShell

I hope it can help: How to setup Python 3 virtual environment on Windows 10

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dami.max
  • 377
  • 3
  • 17
0
C:\path> Set-ExecutionPolicy - Scope CurrentUser

cmdlet Set-ExecutionPolicy at command pipeline position 1 Supply values for the following parameters: ExecutionPolicy: Unrestricted

C:\path> Scripts\activate.ps1

(new env)

C:\path> deactivate

C:\path> 

This worked.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

In Windows PowerShell the ExecutionPolicy is set to 'restricted' by default. This does not allow scripts to be run in PowerShell.

We can only run scripts when the ExecutionPolicy is set to 'RemoteSensing' from 'Restricted' You can follow the video "PowerShell 08 - Changing the execution policy so you can run scripts" to do that!

After the above step, you can directly type "Scrits/activate" (while being in the directory of the virtual environment that you want to activate) to activate the virtual environment that you wish to activate!

Example

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

If you have used pipenv in creating your virtual environment, you should run pipenv shell before executing any command in the Visual Studio Code terminal.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gimnath
  • 824
  • 9
  • 11
0

I ran into same problem, The issue was

The possible reason was venv doesnot run without "Run as administrator" on powercell You can run venv\Scripts\activate on command prompt instead of powercell

0

Create a .bat script:

call .\env\Scripts\deactivate
RMDIR /Q/S env

call python -m pip uninstall pip -y
call python -m ensurepip
call python -m pip install --upgrade pip
call python -m pip install --user virtualenv
call python -m virtualenv env

call .\env\Scripts\activate

# Install pip requirements
call python -m pip install --upgrade pip
call python -m pip install Pillow

# Run main scripts here
call python main.py

call .\env\Scripts\deactivate

Run this script via Command Prompt.

leenremm
  • 1,083
  • 13
  • 19
0

I resolved this issue by executing this command Set-ExecutionPolicy Unrestricted -Force

Vikas Rao
  • 11
  • 1
0

Use windows cmd instead of the bash shell of Pycharm or VS code then it works.

Athif Saheer
  • 4,539
  • 3
  • 9
  • 14