pyenv-win / pyenv-win-venv

A CLI to manage virtual envs with pyenv-win
Apache License 2.0
78 stars 8 forks source link

Auto activate with profile not work #12

Open gruiyuan opened 1 year ago

gruiyuan commented 1 year ago

In order to get the same experience as pyenv under linux, I used the method mentioned in the note. But it doesn't work.

I confirmed that there is a .python-version file in my directory, and the profile has been executed.

It work when I manually run command 'pyenv-venv init'

image

arzkar commented 1 year ago

Did you add the command to your powershell profile as instructed in this section of the readme? https://github.com/pyenv-win/pyenv-win-venv#env-automatic-activation-using-python-version-file

arzkar commented 1 year ago

Note: This only works if you open the shell in the current directory using "Open shell here". The CLI currently doesn't support auto activation if the user changes the directory. I could look into adding that feature but it would mean checking for a .python-version every time the user changes their directory. Not sure if that would make the shell slower. Something to look into.

gruiyuan commented 1 year ago

I followed the Readme file and added the command in the profile. And I also noticed that the "open powershell window here" should be used. But it still does not initialize successfully, unless init is executed manually. Sorry for not describing clearly.

Here is a more detailed description.

In order to prove that the profile has been executed, and the 'pyenv-venv' command can be executed successfully, I wrote such a profile file.

image

And then, in the target directory, use "open powershell window here" to open a shell window.

image

As you see, the command pyenv-venv help outputs correctly.

image

But the python environment is not loaded successfully.

image

Next, I manually executed the command 'pyenv-venv init', then the python environment was initialized successfully.

image

Even if I add 'sleep 10' at the beginning of the profile file, it still have nothing change.

gruiyuan commented 1 year ago

Oh, I know the reason.

https://github.com/pyenv-win/pyenv-win-venv/blob/aff05d419953be4b06369f989d97f839f4ba216a/bin/pyenv-win-venv.ps1#L21

The output of (Get-Location).Path in profile file is 'C:\windows\system32', which seems to be the default workdir of powershell. Not the current location where open the powershell window.

gruiyuan commented 1 year ago

Oh, I know the reason.

https://github.com/pyenv-win/pyenv-win-venv/blob/aff05d419953be4b06369f989d97f839f4ba216a/bin/pyenv-win-venv.ps1#L21

The output of (Get-Location).Path in profile file is 'C:\windows\system32', which seems to be the default workdir of powershell. Not the current location where open the powershell window.

Since the definition of the right-click shortcut key is in the registry:

image

Create a profile file for this, and it works.

$powershellDir = $((Get-Location).Path)

$workingDir = if ([Environment]::CommandLine -match '\b(set-location|cd|chdir\sl)\s+(-(literalpath|lp|path|PSPath)\s+)?(?<path>(?:\\").+?(?:\\")|"""[^"]+|''[^'']+|[^ ]+)') {
    $Matches.path -replace '^(\\"|"""|'')' -replace '\\"$'
  } else { # No Set-Location command passed, use the current dir.
    $PWD.ProviderPath
  }

Set-Location $workingdir; pyenv-venv init root

Set-Location $powershellDir
arzkar commented 1 year ago

Thanks for all the investigation into this. I will look into this once I am back home.

The CLI is initializing the env correctly in my personal laptop but I tried it out in my work pc and it fails to activate. Need to check why its working as intended in my laptop.

arzkar commented 1 year ago

@gruiyuan I pushed an update which adds a -debug flag so that we can see the environment values being set. It will help with debugging.

Update the CLI using: pyenv-venv update self Update the $profile: pyenv-venv init -debug

After that, when you open the shell, it should output the debug log.

arzkar commented 1 year ago

The CLI is initializing the env correctly in my personal laptop but I tried it out in my work pc and it fails to activate. Need to check why its working as intended in my laptop.

Update regarding this. My work pc didnt have pyenv-venv init root in the powershell profile hence it was not automatically activating. So I will need to see your debug log as I mentioned above to investigate the issue.

gruiyuan commented 1 year ago

The CLI is initializing the env correctly in my personal laptop but I tried it out in my work pc and it fails to activate. Need to check why its working as intended in my laptop.

Update regarding this. My work pc didnt have pyenv-venv init root in the powershell profile hence it was not automatically activating. So I will need to see your debug log as I mentioned above to investigate the issue.

Ok.

profile is

pyenv-venv init root -debug

When I open the shell by 'shift + right-clicking' in the working directory,hear is the debug log.

image

It is consistent with my previous analysis.

arzkar commented 6 months ago

Are you still encountering this issue? I wasn't able to duplicate this in any of my systems.