如何在Visual Studio终端中自动激活Python虚拟环境?

发布于 2025-02-07 01:11:22 字数 194 浏览 1 评论 0 原文

我正在使用Visual Studio(不是Visual Studio代码)来开发Python应用程序。

有没有一种方法可以使视觉工作室的开发人员PowerShell自动激活虚拟环境,而在Pycharm或VS-Code中可能会有可能?现在,我必须明确运行命令。\ env \ scripts \ activate 在终端中激活虚拟环境。

I am using Visual Studio (not Visual Studio Code) for the development of a Python application.

Is there a way to make the Visual Studio's Developer PowerShell auto-activate the virtual environment, as is possible in PyCharm or in VS-Code? Right now, I have to explicitly run the command .\env\Scripts\activate in the Terminal to activate the Virtual Environment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

粉红×色少女 2025-02-14 01:11:22

我找到了设置PowerShell启动参数的关键。

转到Visual Studio 2022 参数 - > 环境 - > 终端
在“ profiles”窗口 powershell for Developers(默认),然后参数并遵循下一步:

  1. 添加 -ExecutionPolicy远程设计一开始。
  2. 查找 -command”& {command1; command2; ...; commandn}“ andtax and add env> env \ scripts \ actigns \ activate \ activate 作为命令之一(因此看起来像 -command“& {command1; command2; ...; env \ scripts \ activate}” )。

恭喜!现在,Visual Studio每次创建PowerShell控制台时都会激活您的ENV。

Note

-executionPolicy远程设计仅在此特定过程中设置ExecutionPolicy(...我们每次创建PowerShell Console时运行),并且不会影响Global,用户或用户或使用任何其他Env executionPolicy 变量,但在此特定过程中变量。因此,设置安全!

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:

  1. Add -ExecutionPolicy RemoteSigned in the very beginning.
  2. Find -Command "&{ Command1; Command2; ...; CommandN}" syntax and add env\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 env ExecutionPolicy 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 run activate script.

References:

Read more about PowerShell startup arguments and ExecutionPolicies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文