Skip to content
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

Open
sam-val opened this issue Sep 12, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@sam-val
Copy link

sam-val commented Sep 12, 2023

Like title, current version (v0.6.2) doesn't do this. It'd be great if we also have it too like the original.

@arzkar
Copy link
Member

arzkar commented Sep 12, 2023

The current behavior is to activate the env if you open a shell from within a directory which has the .python-version.
To automatically look for the env in every directory, I will need to add an Event Watcher which will check for the .python-version every time we change the directory, might add an overhead to the shell but I will look into it when I get the time.

@arzkar arzkar added the enhancement New feature or request label Nov 7, 2023
@bucefalog
Copy link

If anybody lands on this page, adding the following lines to powershell's profile might work as a provisory solution:

# ACTIVE PYTHON ENVIRONMENT AUTOMATICALLY:
$script_file = "$HOME/.config/powershell/set_location_proxy.ps1"
#  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()`r`n`${1}pyenv-venv init root" 
"function Set-Location {`n" | Set-Content -Path "$script_file" 
$temp_var | Add-Content -Path "$script_file" 
"`n}" | Add-Content -Path "$script_file"
. "$script_file"

The script is creating a proxy to the Set-Location function and sourcing it.
The proxy replaces the powershell default function and works with any command that uses the function (including cd and zoxide).
It may also be useful to modify the code to deactivate the current environment when moving into a folder with no python version found.

@Helveg
Copy link

Helveg commented Aug 6, 2024

@bucefalog your snippet works, but it errors out if the $script_file path doesn't exist. It seems that $HOME/.config/powershell is not guaranteed to exist. When I created it your snippet worked as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants