ArtOfWarfare
ArtOfWarfare

Reputation: 21486

How do I Activate a Python venv Automatically when starting PowerShell?

When I launch PowerShell, I want my Python venv to be automatically loaded.

I've searched a few times, but I've never found anything on how to do that - maybe it's unusual that I'm using venv and PowerShell but also don't know how to do basic things in PS.

Upvotes: 2

Views: 1184

Answers (1)

ArtOfWarfare
ArtOfWarfare

Reputation: 21486

My issue was probably that I was searching for stuff about venv, when really this is a question about PowerShell.

Start a PowerShell session and run this command to have an empty ps1 file generated which will run whenever you launch PowerShell:

New-item –type file –force $profile

It'll print out the path to where that empty file was created. Go and open it up - you can put something like the following in:

Write-Host "Running    C:\Users\Arty\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"
Write-Host "  activate C:\Users\Arty\arty.venv`n"

. \Users\Arty\arty.venv\Scripts\Activate.ps1

The first two lines are just print statements reminding me of where the script and venv are and why this happens automatically. This will be useful for later when I've forgotten and take the behavior for granted. The last line is the only important one.

Upvotes: 1

Related Questions