PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 21, 2022

[FIXED] How to auto-activate python virtual environment in Visual Studio terminal?

 November 21, 2022     python, python-venv, virtualenv, visual-studio, visual-studio-2022     No comments   

Issue

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.


Solution

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.



Answered By - bc-ru
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing