-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatically detect .python-version and switch in/out of enviroment when changing directories #19
Comments
The current behavior is to activate the env if you open a shell from within a directory which has the |
If anybody lands on this page, adding the following lines to powershell's profile might work as a provisory solution:
The script is creating a proxy to the Set-Location function and sourcing it. |
@bucefalog your snippet works, but it errors out if the |
@bucefalog hi! Thanks for the excellent tip! # # ACTIVE PYTHON ENVIRONMENT AUTOMATICALLY:
$powershellConfigPath = "$HOME/.config/powershell"
$script_file = "$powershellConfigPath/pyenv-venv-auto.ps1"
if (Test-Path -Path $script_file) {
. "$script_file"
exit
}
if (-not (Test-Path -Path $powershellConfigPath)) {
New-Item -Path $powershellConfigPath -ItemType Directory > $null
}
# # NOTE: the following should only be executed once and the regex substitution may fail in the future
$temp_var = New-Object System.Management.Automation.CommandMetadata (Get-Command Set-Location)
$temp_var = [System.Management.Automation.ProxyCommand]::Create($temp_var)
$temp_var = ($temp_var) -replace '(\s*)\$steppablePipeline\.End\(\)', @'
${1}$steppablePipeline.End()
# pyenv-venv-auto start
if ($global:pyenvVenvPath) {
if ($pwd.Path.StartsWith($global:pyenvVenvPath) -ne $true) {
$global:pyenvVenvPath = $null
pyenv-venv deactivate
}
}
else {
$cwd = $pwd.Path
while ($cwd.length -ne 0) {
if (Test-Path "$cwd\.python-version") {
$global:pyenvVenvPath = $cwd
pyenv-venv init root
break
}
else { $cwd = Split-Path $cwd }
}
}
# pyenv-venv-auto end
'@
"function Set-Location {`n" | Set-Content -Path "$script_file"
$temp_var | Add-Content -Path "$script_file"
"`n}" | Add-Content -Path "$script_file"
. "$script_file" It's not a very clean trick, because the file is searched twice, but I didn't find any other way to determine that venv was activated than to search for the file myself. However, there is a bug from pyenv-venv itself #26 that, when activated via PS, it throws it into cmd, where, of course, this script will no longer work. There are two solutions that you can try to apply locally to solve this problem |
Like title, current version (v0.6.2) doesn't do this. It'd be great if we also have it too like the original.
The text was updated successfully, but these errors were encountered: