在使用PowerShell时,使用virtualenv似乎会出现问题。
当我尝试在PowerShell中激活我的环境时,例如...
env/scripts/activate
.. 什么都没有发生。(应该已经改变了 shell 提示符和 PATH 环境变量。)
我猜问题在于 PowerShell 为运行 activate.bat 而产生了一个新的 cmd 进程,因此在 activate.bat 完成后对 shell 所做的更改失效。
你有任何解决办法吗?(目前我还在使用 cmd.exe)
在使用PowerShell时,使用virtualenv似乎会出现问题。
当我尝试在PowerShell中激活我的环境时,例如...
env/scripts/activate
.. 什么都没有发生。(应该已经改变了 shell 提示符和 PATH 环境变量。)
我猜问题在于 PowerShell 为运行 activate.bat 而产生了一个新的 cmd 进程,因此在 activate.bat 完成后对 shell 所做的更改失效。
你有任何解决办法吗?(目前我还在使用 cmd.exe)
我也遇到了这个问题!最终在Windows上找到了解决方法...
好的,请按照以下步骤操作:
1)在Windows搜索栏中输入PowerShell,然后右键单击它并选择以管理员身份运行
(如果您在此过程中遇到问题,请查看this)
2)在PowerShell中运行以下命令:Set-ExecutionPolicy Unrestricted
3)重新运行激活命令:.\\env\Scripts\activate.ps1
(只需运行确切的命令!注意您环境的名称。)
就是这样!:)
只有两个建议:
由于削弱安全策略总是存在问题,我建议仅以最小方式为Powershell设置: 不要以管理员身份运行Powershell,而是作为想要使用虚拟环境功能的用户运行。输入 "set-executionpolicy -executionpolicy unrestricted -scope currentuser"。 这样,该策略仅针对一个用户进行更改,而不是整台机器。
其次,我建议从Github下载源代码 "regisf/virtualenvwrapper-powershell"。下载后将zip文件解压到本地目录,并在其中运行文件"Install.ps1"。这将永久扩展您机器上的Powershell配置文件,从而启用所有"virtalenvwrapper-win"命令,包括"workon"。之后,您将不会注意到Powershell和Commandshell在虚拟环境方面的行为上有任何区别。
Windows Power Shell 被认为比普通命令提示符壳更强大和“涡轮增压”。
如果您正在使用 Visual Studio Code (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 处
要解决此问题,您可以执行以下操作:
谢谢 祝一切顺利
Set-ExecutionPolicy Unrestricted
输入:A
cd.\env\Scripts\activate
对于仍然在努力启动的人,Windows PowerShell默认情况下不会从当前位置加载命令。如果您进入activate.ps1的位置,运行activate.ps1
可能会返回“未将“activate”识别为cmdlet的名称”。
为了从当前位置运行activate.ps1,请尝试:
.\activate.ps1
我写了这个简短的脚本来处理我的开发服务器的激活和启动。
$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"
}
享受吧。
我写了一个小脚本来激活它。
# 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"
记得使用 PowerShell 扩展名 .ps1
保存文件。
没有Windows 10。 CMD virtualenv venv/Script/activate.bat