简体   繁体   中英

powershell: activate python virtualenv using shortcut

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:

.\venv\Scripts\Activate.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 !

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.

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)

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
}

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM