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

Auto activate with profile not work #12

Open
gruiyuan opened this issue Jun 2, 2023 · 10 comments
Open

Auto activate with profile not work #12

gruiyuan opened this issue Jun 2, 2023 · 10 comments

Comments

@gruiyuan
Copy link

gruiyuan commented Jun 2, 2023

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
Copy link
Member

arzkar commented Jun 2, 2023

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
Copy link
Member

arzkar commented Jun 2, 2023

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
Copy link
Author

gruiyuan commented Jun 5, 2023

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
Copy link
Author

gruiyuan commented Jun 5, 2023

Oh, I know the reason.

$python_version_file = "$((Get-Location).Path)\.python-version"

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
Copy link
Author

gruiyuan commented Jun 5, 2023

Oh, I know the reason.

$python_version_file = "$((Get-Location).Path)\.python-version"

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
Copy link
Member

arzkar commented Jun 5, 2023

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
Copy link
Member

arzkar commented Jun 5, 2023

@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
Copy link
Member

arzkar commented Jun 7, 2023

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
Copy link
Author

gruiyuan commented Jun 28, 2023

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
Copy link
Member

arzkar commented Jan 2, 2024

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

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

No branches or pull requests

2 participants