powershell: activate python virtualenv using shortcut

Question:

All my virtualenvs are put into a venv folder which is inside the project folder.

If I want to activate the virtualenv when inside the project folder I need to enter:

.venvScriptsActivate.ps1 

Having to do this many times I really find this cumbersome. And I am looking for something quicker. From inside my project folder I’d like to type act or something which then automatically activates the .ps1 script.

But not being familiar with powershell and all the options available I wonder if someone could give me some clues where to start ? (batch script? cmd script? powershell script? shortcut (.lnk) file?)

Thanks !

Asked By: sanders

||

Answers:

What you want to use is called an alias

In Windows you will use it this way :

New-Alias <nom-alias> <commande>

To make it persistant (be able to use it again after closing and reopening the powershell) you need to put the alias in a configuration file.

Answered By: Tim

WORKED_OUT_SOLUTION. credits to answer from Tim.

In your powershell:

# create an alias called "ve" that looks for the relative location of -Value
# and runs it.
New-Alias -Name ve -Value venv/scripts/activate.ps1

# Export the alias to a script.
Export-Alias -Name ve -Path "venv.ps1" -As Script

# Make sure this scripts runs each time you start a powershell session.
Add-Content -Path $Profile -Value (Get-Content venv.ps1)
Answered By: sanders

This function finds the activation script in your project and runs it.

Function venv_activate {
    Get-ChildItem activate.ps1 -Recurse -Depth 2 | %{$_.FullName} | Invoke-Expression
}

enter image description here

Answered By: Marvin Xu
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.