0

I want to create a venv (virtual environment) using this python command in windows 10

python -m venv "environment name"

and to activate it I have to enter this line in powershell

"environment name"\scripts\activate or "environment name"\scripts\activate.bat

so I created a this function in $profile

function active {
    param (
        $venv_name = "venv"
    )
    
    "$($venv_name)\scripts\activate.bat"
}

but the problem is this function only shows the path and nothing more but I want it to activate

"environment name"\scripts\activate.bat

How can I fix this ?

and it doesn't have to be function

David Tim
  • 23
  • 2

1 Answers1

1

Use the invocation operator &:

function active {
    param (
        $venv_name = "venv"
    )
    
    & "$($venv_name)\scripts\activate.bat"
}

You can also simplify the string slightly, $() is not required for simple variable expansion:

& "${venv_name}\scripts\activate.bat"
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • What's the reason for `${..}`? – Abraham Zinala Apr 24 '21 at 17:57
  • 1
    @AbrahamZinala, have a look here https://stackoverflow.com/a/60329476/11954025 – Daniel Apr 24 '21 at 18:04
  • 1
    @AbrahamZinala At some point you're going to run into a situation where you'll want to use a variable to store a drive letter or similar, best be in the habit of qualifying them with `${...}`, otherwise you'll get nasty surprises :) `$DriveLetter = 'C'; "$DriveLetter:\Windows"` vs `"${DriveLetter}:\Windows"` – Mathias R. Jessen Apr 24 '21 at 18:05
  • Oh man, that's cool. Wasn't aware of that. Thanks to both of you, for the explanation and link. Don't mind me and my comments; just a curious mind at work(: – Abraham Zinala Apr 24 '21 at 18:25