如何在Visual Studio终端中自动激活Python虚拟环境?
我正在使用Visual Studio(不是Visual Studio代码)来开发Python应用程序。
有没有一种方法可以使视觉工作室的开发人员PowerShell自动激活虚拟环境,而在Pycharm或VS-Code中可能会有可能?现在,我必须明确运行命令。\ env \ scripts \ activate
在终端中激活虚拟环境。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了设置PowerShell启动参数的关键。
转到Visual Studio 2022 参数 - > 环境 - > 终端。
在“ profiles”窗口 上 powershell for Developers(默认),然后参数并遵循下一步:
-ExecutionPolicy远程设计
一开始。-command”& {command1; command2; ...; commandn}“
andtax and addenv> env \ scripts \ actigns \ activate \ activate
作为命令之一(因此看起来像-command“& {command1; command2; ...; env \ scripts \ activate}”
)。恭喜!现在,Visual Studio每次创建PowerShell控制台时都会激活您的ENV。
Note :
-executionPolicy远程设计
仅在此特定过程中设置ExecutionPolicy(...我们每次创建PowerShell Console时运行),并且不会影响Global,用户或用户或使用任何其他EnvexecutionPolicy
变量,但在此特定过程中变量。因此,设置安全!PS我们需要更改
executionPolicy
才能运行activate
脚本。参考:
阅读有关PowerShell 启动参数和 recutionpolicies 。
I found a key in setting up PowerShell startup arguments.
Go to the Visual Studio 2022 Parameters -> Environment -> Terminal.
In "Profiles" window click on PowerShell for developers (Default), then Arguments and follow next steps:
-ExecutionPolicy RemoteSigned
in the very beginning.-Command "&{ Command1; Command2; ...; CommandN}"
syntax and addenv\Scripts\activate
as one of the commands (so it looks like-Command "&{ Command1; Command2; ...; env\Scripts\activate}"
).Congrats! Now Visual Studio will activate your env every time you create PowerShell console.
NOTE:
-ExecutionPolicy RemoteSigned
setting up ExecutionPolicy only for this particular process (...that we run every time we create PowerShell console) and does NOT affect either global, user or any other envExecutionPolicy
variable but variable in this particular process. So it is safe to set up!P.S. We need to change
ExecutionPolicy
to be able to runactivate
script.References:
Read more about PowerShell startup arguments and ExecutionPolicies.