Python virtualenv 不会在 Windows 上激活

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18713086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:33:30  来源:igfitidea点击:

virtualenv won't activate on windows

pythonvirtualenv

提问by SScode

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

基本上我似乎无法激活我创建的 virtualenv 环境。

I'm doing this inside of windows powershell through using

我通过使用在 windows powershell 内部执行此操作

scripts\activate

but 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 admin privileges on my computer?

这可能是因为我的计算机没有管理员权限吗?

采纳答案by Kamiccolo

Moving comment to answers section :)

将评论移至答案部分:)

According to Microsoft Tech Supportit might be a problem with Execution Policy Settings. To fix it, You should try executing Set-ExecutionPolicy Unrestricted -Forcein Your Power Shell.

根据Microsoft 技术支持,这可能是执行策略设置的问题。要修复它,您应该尝试Set-ExecutionPolicy Unrestricted -Force在您的 Power Shell 中执行。

NB: Remember to launch Power Shell as administrator.

注意:请记住以管理员身份启动 Power Shell。

回答by ni8mr

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

我在这里找到的另一个快速解决方案(它仅适用于 Windows Powershell)是这样的:

First run

第一次运行

Scripts\cmd

Then run

然后运行

Scripts\activate.bat

At this position, your Virtualenvis activated. Now if you deactivate it and want to activate it again later in the same session of powershell, you just need to run-

在这个位置,你的Virtualenv被激活。现在,如果您停用它并想稍后在 powershell 的同一会话中再次激活它,您只需要运行 -

Scripts\activate

There is no need to cmdor activate.batcommand later.

后面就不需要cmdactivate.bat命令了。

回答by Adil Siddiqui

Set the ExcutionPolicy for the Process Scope after Yes [y] then type Scripts/activate

在 Yes [y] 之后为 Process Scope 设置 ExcutionPolicy 然后输入 Scripts/activate

enter image description here

在此处输入图片说明

回答by Janarthanan Ramu

in windows you should activate the virtual environment by following command in cmd

在 Windows 中,您应该通过在 cmd 中执行以下命令来激活虚拟环境

E:\your_environment\Scripts>activate.bat

E:\your_environment\Scripts>activate.bat

if the environment is activatedthen it show your environment name enclosed with bracket like this

如果环境被激活,那么它会像这样显示用括号括起来的环境名称

(your_environment)E:\your_environment\Scripts>

(your_environment)E:\your_environment\Scripts>

Also we can ensure by checking with where.exeit will list our active python environment with order of hierarchy

我们也可以通过检查where.exe来确保它会按照层次结构的顺序列出我们的活动 python 环境

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

 E:\your_environment\Scripts\python.exe

 C:\Python27\python.exe

if you need to deactivatethen do

如果你需要停用然后做

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

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

回答by Ole Aldric

This worked for me:

这对我有用:

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

你可以简单地打开一个普通的(你不需要提升访问权限)cmd或powershell会话(我在vscode中使用嵌入式PS终端)并从脚本文件所在的文件夹中键入以下内容,例如.venv\Scripts\Activate.ps1::

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

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

and then you can run the activatecommand after that.

然后你可以在那activate之后运行命令。

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

注意:这将只允许该特定脚本运行,并且只能由您的登录用户运行。

回答by Amit Gupta

To install a virtual environmentin Windows Powershell only but to activate you'll need to run Windows powershellas Administrator

要安装一个虚拟环境,只有在Windows PowerShell中,但激活你需要运行Windows PowerShell的管理员

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

It is installed now to activate it, run powershell in as administrator

现在安装激活它,以管理员身份运行powershell

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

To deactivate the environment

停用环境

  1. .\env\Scripts\deactivate
  1. .\env\Scripts\deactivate

Hope it helps.

希望能帮助到你。

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

如需更多帮助,请访问官方页面 https://pypi.org/project/virtualenv/1.8.2/

回答by ianNg

If you don't want to change your computer's execution policy on windows like I do, you can use windows command prompt instead of windows powershell, and just need to run Scripts\activateto activate your env

如果您不想像我一样在 windows 上更改计算机的执行策略,则可以使用 windows 命令提示符而不是 windows powershell,只需运行 Scripts\activate以激活您的 env

回答by yas

If machine a 64 bit, open the x86instance of PowerShell as Administrator& check (set) the execution policy. try this

如果机器是 64 位,请以管理员身份打开PowerShell的x86实例并检查(设置)执行策略。尝试这个

Set-ExecutionPolicy Unrestricted

or

或者

Set-ExecutionPolicy Unrestricted -Force

回答by Matt N

In powershell use

在 powershell 中使用

Scripts\activate.ps1

instead of activate.bat which doesn't work in powershell anymore.

而不是 activate.bat 不再在 powershell 中工作。

also deactivate by just typing

也只需输入即可停用

deactivate

at the command prompt

在命令提示符下

回答by Ochieng'

Follow these steps to the latter. step 1. Use Windows PowerShell as ADMINISTRATOR. (VERY IMPORTANT) and cd into the project folder. Run

按照以下步骤进行操作。步骤 1. 使用 Windows PowerShell 作为管理员。(非常重要)并 cd 进入项目文件夹。跑

virtual env

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

步骤 2. 如果您有 activate.bat 文件,请检查脚本文件夹

\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

第 3 步。如果它不在这里,请确保您有互联网连接并再次运行

virtual env 

step 4. If the activate.bat file is there in the script folder proceed. step 5. run this on your shell

步骤 4. 如果脚本文件夹中有 activate.bat 文件,请继续。步骤 5. 在你的 shell 上运行它

Set-ExecutionPolicy Unrestricted -Force

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

步骤 6. 要在 Windows 上激活 virtualenv,激活脚本位于 Scripts 文件夹中:

env\Scripts\activate.bat

step 7 check for the (env) at the start of each line this shows you are on the virtual environment

步骤 7 检查每行开头的 (env) 这表明您在虚拟环境中

step 8. To reactivate when yo come back to the project the second time run:

步骤 8. 要在您第二次返回项目时重新激活:

.\env\Scripts\activate