[PYTHON] How to use virtualenv with PowerShell

virtualenv supports PowerShell, but it may not be available depending on the PowerShell settings. We have summarized the causes and countermeasures.

Cause of "PS Security Exception" when activating virtualenv

When using virtualenv activate in PowerShell, you may get the following error:

> virtualenv.exe venv
> .\venv\Scripts\activate
.\venv\Scripts\activate :Script execution is disabled on this system, so file C:\Users\****\developme
nt\venv\Scripts\activate.Unable to read ps1. For more information, see about_Execution_Policies」(http://go.microso
ft.com/fwlink/?LinkID=135170)Please refer to.
Location line:One character:1
+ .\venv\Scripts\activate
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          :Security error: (: ) []、PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

This is due to PowerShell's script execution policy. You can check the current execution policy with the Get-ExecutionPolicy command.

> Get-ExecutionPolicy

Restricted means "No script can be executed. Windows PowerShell can only be used in interactive mode." For more information on execution policies, see the official Microsoft documentation (https://technet.microsoft.com/en-us/library/ee176847.aspx).

If it remains Restricted, it cannot be activated. There are the following methods as countermeasures.

Measure 1

Launch PowerShell with administrator privileges and use the Set-ExecutionPolicy command to change the execution policy to RemoteSigned. RemoteSigned means" The downloaded script can only be executed if it is signed by a trusted publisher. "

> Set-ExecutionPolicy RemoteSigned

The dialog "Do you want to change the execution policy?" Appears. Enter "Y" and press Enter to proceed.

In addition, official document of virtualenv says that Set-ExecutionPolicy AllSigned is fine, but [^ #] my In the environment, I got the following error and could not activate.

[^ #]: It says "In order to use the script, you can relax your system's execution policy to AllSigned, meaning all scripts on the system must be digitally signed to be executed. Since the virtualenv activation script is signed by one of the authors (Jannis Leidel) this level of the execution policy suffices. "

.\venv\Scripts\activate :File C:\Users\****\development\venv\Scripts\activate.Unable to read ps1. File
C:\Users\****\development\venv\Scripts\activate.ps1 is not digitally signed. This script is on the current system
Cannot be executed. For more information on script execution and setting execution policies, see about_Execution_Policies」(http://go.micr
osoft.com/fwlink/?LinkID=135170)Please refer to.
Location line:One character:1
+ .\venv\Scripts\activate
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          :Security error: (: ) []、PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

Measure 2

If you don't want to change the settings just for virtualenv, it is recommended to add the -Scope Process option to the command of Countermeasure 1 and set" Change execution policy only for the currently open PowerShell window ". In this case PowerShell does not need administrator privileges.

> Set-ExecutionPolicy RemoteSigned -Scope Process

If you don't want the dialog to appear every time, you can omit it with the -force option.

Try using virtualenv activate after countermeasures

If you activate after implementing the above measures, the error will not occur. You should see the virtualenv directory name on the left side of the screen as shown below.


Reference material

Recommended Posts

How to use virtualenv with PowerShell
How to use virtualenv
How to use virtualenv
[Note] How to use virtualenv
[Python] How to use virtualenv
Python: How to use async with
How to use FTP with Python
How to use ManyToManyField with Django's Admin
How to use OpenVPN with Ubuntu 18.04.3 LTS
How to use Cmder with PyCharm (Windows)
How to use Ass / Alembic with HtoA
How to use Japanese with NLTK plot
How to use jupyter notebook with ABCI
How to use CUT command (with sample)
How to use SQLAlchemy / Connect with aiomysql
How to use JDBC driver with Redash
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
Use pipdeptree with virtualenv
How to use Seaboan
How to use image-match
How to use shogun
How to use Pandas 2
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use GCP trace with open Telemetry
Specify the Python executable to use with virtualenv
How to use tkinter with python in pyenv
How to use Qt Designer
How to use search sorted
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
How to use the generator
[Python] How to use list 1
How to use FastAPI ③ OpenAPI
How to use Python argparse
How to use IPython Notebook
How to update with SQLAlchemy?