Skip to content

Instantly share code, notes, and snippets.

@pushrbx
Last active August 23, 2019 11:08
Show Gist options
  • Save pushrbx/c869107c286c29d26a66e2e59a53e498 to your computer and use it in GitHub Desktop.
Save pushrbx/c869107c286c29d26a66e2e59a53e498 to your computer and use it in GitHub Desktop.
A function for your powershell profile which helps activating the python venv in PyCharm. (Assuming that you use the default folders offered by pycharm on project creation)
function IfTrue($a, $b, $c) { if ($a) { $b } else { $c } }
function Coalesce($a, $b) { if ($a -ne $null) { $a } else { $b } }
New-Alias "??" Coalesce
New-Alias "?:" IfTrue
function py-avenv {
param (
[string]$venvName = $null
)
$intellij_env_vars = get-childitem env: -name -include __INTELLIJ*
if (!($intellij_env_vars -eq $null)) {
$p = get-childitem -path ".." -name -include ".venvs"
if (!($p -eq $null)) {
$project_name = (Get-Item -Path ".\").Name
$p = ?? (get-childitem -path "..\.venvs" -name -include $project_name) $venvName
$project_name = ?? (get-childitem -path "..\.venvs" -name -include $project_name) $null
if ($p -ne $null) {
. "..\.venvs\$p\Scripts\activate.ps1"
write-host -foreground green "venv has been activated"
} else {
if ($project_name -ne $null) {
$project_name = (Get-Item -Path ".\").Name
. "..\.venvs\$project_name\Scripts\activate.ps1"
write-host -foreground green "venv has been activated"
} else {
write-host -foreground red "Couldn't find the project's venv directory"
}
}
} else {
write-host -foreground red "No .venv folder found. Cant activate venv."
}
} else {
write-host -foreground red 'Not in intellij.'
}
}
$intellij_var = get-childitem env: -name -include __INTELLIJ*
$pyenvname = get-childitem env: -name -include PYENVNAME
if (($intellij_var -ne $null) -and ($pyenvname -ne $null)) {
py-avenv -venvName $env:PYENVNAME
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment