68

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)

4

18 回答 18

114

最新版本的 virtualenv支持 PowerShell out-of-the-box

只要确保你运行:

Scripts\activate.ps1

代替

Scripts\activate

后者将执行activate.bat,这在 PowerShell 上不起作用。

于 2012-04-05T14:56:40.970 回答
47

我原来的答案现在已经过时了。现在,只需使用activate.ps1(而不是activate.bat)从 Powershell 环境中激活。


原答案:

是一篇包含 Powershell 脚本的帖子,该脚本允许您运行持久修改其环境变量的批处理文件。该脚本会将任何环境变量更改传播回调用 PowerShell 环境。

于 2009-09-01T23:15:02.750 回答
22

一个快速的解决方法是调用 cmd,然后从 cmd 会话中运行您的 activate.bat。例如:

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>
于 2010-10-13T19:52:47.730 回答
14

在您的虚拟环境文件夹的 Scripts 目录中,有几个激活脚本可以使用,具体取决于您执行命令的位置。如果您尝试从 Windows PowerShell 激活虚拟环境,请尝试使用以下命令:

. .\env\Scripts\activate.ps1

如果您收到有关系统上禁用激活脚本的错误,您首先需要在系统上调用执行策略更改。这需要以管理员身份完成。

去做这个:

1)右键单击PowerShell应用程序并选择以管理员身份运行

2)运行以下命令:Set-ExecutionPolicy Unrestricted

3)重新运行激活命令:. .\env\Scripts\activate.ps1

于 2018-04-21T17:56:31.043 回答
12

运行以下命令:

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force

其次是:

./env/Scripts/activate.ps1

就这样

于 2020-01-19T23:30:50.763 回答
6

试试这个: . .\env\Scripts\activate.ps1 看点和空格

于 2015-04-28T11:41:41.057 回答
4

发生此错误的原因是一项安全措施,未经您的批准不允许在您的系统上执行脚本。您可以通过打开具有管理权限的 powershell(在主菜单中搜索 powershell 并从上下文菜单中选择以管理员身份运行)并输入:

设置执行策略远程签名

更多信息:http: //www.faqforge.com/windows/windows-powershell-running-scripts-is-disabled-on-this-system/

于 2016-01-07T09:21:17.353 回答
3

Windows 用户

在 Powershell 中:

  1. 以管理员身份运行 Powershell
  2. 复制并粘贴此命令:set-executionpolicy remotesigned
  3. 同意消息。

最后,运行

your_virtualenv_name\Scripts\activate.ps1

代替

your_virtualenv_name\Scripts\activate.bat

在 CMD 中运行:

your_virtualenv_name\Scripts\activate.bat
于 2019-02-27T11:28:24.037 回答
1

还有2个建议:

因为削弱安全策略总是有问题的,所以我建议以最小的方式为 Powershell 执行此操作:不是以管理员身份调用 Powershell,而是以想要使用 virtualenv 功能的用户身份调用 Powershell。键入“set-executionpolicy -executionpolicy unrestricted -scope currentuser”。这样,策略仅针对一个用户而不是整台计算机更改。

其次,我建议从 github 下载源“regisf/virtualenvwrapper-powershell”。下载后将 zip 文件解压缩到本地目录并在其中运行文件“Install.ps1”。这将在您的计算机上永久扩展 Powershell 配置文件,从而启用所有“virtalenvwrapper-win”命令,包括“workon”。之后,您将不会注意到 Powershell 和 Commandshell 关于 virtualenv 的行为有任何差异。

于 2020-12-05T23:15:37.773 回答
1

我也有这个问题!终于找到了我们应该在windows中做什么......

好的,请按照以下步骤操作:

1)在windows的搜索栏中输入powershell,然后右键单击它并选择以管理员身份运行

(如果您有问题,请检查

2)在powershell中运行以下命令:Set-ExecutionPolicy Unrestricted

3)重新运行激活命令:.\\env\Scripts\activate.ps1

(只需运行确切的命令!请注意您的环境名称。)

就是这样!:)

于 2019-12-07T23:50:27.927 回答
1

首先,确保您被允许运行脚本。如果不激活它。现在打开 powershell pip install virtualenv (如果你的系统中没有安装 virtualenv) python -m virtualenv myenv (这里“myenv”是你的环境的名称)

#activating 你创建的虚拟环境:myenv/Scripts/Activate.ps1

#deactivating虚拟环境:deactivate

于 2021-06-25T06:03:07.803 回答
0

我写了一个小脚本来激活它。

# Don't forget to change execution policies
# Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser
# More info https://docs.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"

请记住使用 PowerShell 扩展名保存文件.ps1

于 2020-03-05T13:34:31.937 回答
0

没有 windows 10. CMD virtualenv venv/Script/activate.bat

于 2022-01-20T23:06:40.047 回答
0

Windows Power shell 被认为比普通的命令提示 shell 更强大和“涡轮增压”。在您使用 Visual Studio 代码 (VScode) 的实例中,运行 power shell 来启动 Python 虚拟环境可能会引发错误,尤其是在 Windows 10 上,如下所示。

\Activate :文件 C:\users\titus\django1\Scripts\Activate.ps1 无法加载,因为在此系统上禁用了运行脚本。有关详细信息,请参阅 https://go.microsoft.com/fwlink/?LinkID=135170 上的 about_Execution_Policies。在行:1 字符:1

要解决此问题,您可以执行以下操作:

  1. 进入windows设置
  2. 打开更新和安全
  3. 选择“开发者”
  4. 开启开发者模式
  5. 接受所有条款

谢谢你一切顺利

于 2021-04-05T19:41:44.000 回答
0

对于仍在努力使事情顺利进行的任何人,默认情况下,Windows PowerShell 不会从当前位置加载命令。如果您 cd 进入 activate.ps1 的位置,运行activate.ps1可能会返回“术语 'activate' 未被识别为 cmdlet 的名称”。

为了从当前位置运行 activate.ps1 尝试:

.\activate.ps1
于 2021-06-17T10:48:53.243 回答
0

我编写了这个快速的小脚本来处理我的开发服务器的激活和启动。

$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"

}

享受。

于 2017-08-08T15:11:34.160 回答
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
于 2021-07-25T01:39:33.433 回答
0

在 Windows PowerShell 上,我必须从 venv/Scripts 文件夹中键入:

. ./activate
于 2019-10-22T08:07:06.180 回答