python-poetry / poetry

Python packaging and dependency management made easy
https://python-poetry.org
MIT License
31.14k stars 2.25k forks source link

Update prompt environment variable when opening shell #1642

Closed rfkortekaas closed 2 years ago

rfkortekaas commented 4 years ago

Feature Request

When running poetry shell you have no idea your in the virtualenv or not. Please add the virtualenv's name to the $PROMPT or $PS1 variable.

clintonroy commented 4 years ago

Please try the latest pre-release (Poetry version 1.0.0b8) that has this feature, in a working state.

rfkortekaas commented 4 years ago

I've tried the latest pre-release but it isn't working (on windows). Windows Cmd:

C:\Users\...\pytestproject>poetry shell
Spawning shell within C:\Users\...\Appdata\Local\pypoetry\Cache\virtualenvs\pytestproject-vJd08mD1-py3.7
Microsoft Windows [Version 10.0.18363.476]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\...\pytestproject>

Windows Powershell:

PS C:\Users\...\pytestproject>poetry shell
Spawning shell within C:\Users\...\Appdata\Local\pypoetry\Cache\virtualenvs\pytestproject-vJd08mD1-py3.7
Microsoft Windows [Version 10.0.18363.476]
(c) 2019 Microsoft Corporation. All rights reserved.

PS C:\Users\...\pytestproject>

Cmder

C:\Users\...\pytestproject
位 poetry shell
Spawning shell within C:\Users\...\Appdata\Local\pypoetry\Cache\virtualenvs\pytestproject-vJd08mD1-py3.7
Microsoft Windows [Version 10.0.18363.476]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\...\pytestproject>

Also the cmder shell is not respected; e.g. it runs windows cmd.

wshayes commented 4 years ago

Please try the latest pre-release (Poetry version 1.0.0b8) that has this feature, in a working state.

Worked great for me on MacOS/bash shell.

f0ff886f commented 4 years ago

It doesn't seem to work on Windows 1.0.0 to respect Powershell at all:

PS C:\work\ingress> poetry shell # EXECUTED FROM POWERSHELL
Spawning shell within C:\work\ingress\.venv
Microsoft Windows [Version 10.0.18362.476]
(c) 2019 Microsoft Corporation. All rights reserved.
Clink v0.4.9 [git:2fd2c2] Copyright (c) 2012-2016 Martin Ridgers
http://mridgers.github.io/clink

C:\work\ingress> # PROMPT IN CMD.EXE

using activate directly:

PS C:\work\ingress> .\.venv\Scripts\activate # EXECUTED FROM POWERSHELL
(.venv) PS C:\work\ingress> # PROMPT IN POWERSHELL
yelled1 commented 4 years ago

This is for Cmder on Windows 10

This is supposed to be an automatic feature on Cmder in the next release. https://github.com/cmderdev/cmder/blob/master/vendor/clink.lua#L30-L36 But it has not still hit the scoop yet. Other issue is that obviously what's mentioned by @rfkortekaas I attempted to go around by an alias poet 位 alias poet=poetry shell $t %ConEmuDir%\..\init.bat This did & did not ~ work

C:\Prj\FinmartDelta  
位 alias poet=poetry shell $t %ConEmuDir%\..\init.bat
C:\Prj\FinmartDelta  
位 poet
Spawning shell within C:\Users\ny11559\AppData\Local\pypoetry\Cache\virtualenvs\finmartdelta-rJ3Qparu-py3.7
name=cmd        path=cmd.exe  {i attempted to put this directly in to shell.py, but failed}
Microsoft Windows [Version 10.0.18363.778]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Prj\FinmartDelta>                         {darn same thing}
C:\Prj\FinmartDelta>                         {hit return to see if I can activate it / not}
C:\Prj\FinmartDelta>poet                  {try again}
Virtual environment already activated: C:\Users\ny11559\AppData\Local\pypoetry\Cache\virtualenvs\finmartdelta-rJ3Qparu-py3.7

C:\Prj\FinmartDelta>                         {darn same thing}
C:\Prj\FinmartDelta                           {hit return again}
[finmartdelta-rJ3Qparu-py3.7] 位           {finally got what I needed}
C:\Prj\FinmartDelta  
[finmartdelta-rJ3Qparu-py3.7] 位 

Obviously, I need to do something here C:\Users\myUser.poetry\lib\poetry\utils

 shell.py                                                                                                       buffers  
42         if cls._shell is not None:                                                                                   
43             return cls._shell                                                                                        
44                                                                                                                      
45         try:                                                                                                         
46             name, path = detect_shell(os.getpid())                                                                   
47         except (RuntimeError, ShellDetectionFailure):                                                                
48             raise RuntimeError("Unable to detect the current shell.")                                                
49                                                                                                                      
50         cls._shell = cls(name, path)                                                                                 
51                                                                                                                      
52         return cls._shell                                                                                            
53                                                                                                                      
54     def activate(self, env):  # type: (VirtualEnv) -> None                                                           
55         if WINDOWS:                                                                                                  
56             return env.execute(self.path)   

{tried adding pull path to init.bat but got another error.}

TBBle commented 3 years ago

I suspect this doesn't work on Windows because the logic that calls "activate" in poetry shell is bypassed on Windows, and has been since this feature was introduced in #1395.

dedale commented 3 years ago

I found a solution on Windows with AutoRun scripts.

You need to update registry and add a key "Autorun" REG_SZ below Computer\HKEY_CURRENT_USER\Software\Microsoft\Command Processor with the path to the script (in my case "C:\Users\ded.rc.cmd".

Sample contents for this script:

@if "%RC_CMD%"=="1" goto :skip
@cls
@prompt $p$g 
@set RC_CMD=1
:skip
@if "%POETRY_ACTIVE%"=="1" prompt poetry $p$g 

Result: image

鈿狅笍 in case of errors in this script, it will not be possible to launch a cmd anymore.

TBBle commented 3 years ago

I assume that only works if you're using cmd.exe as your shell?

dedale commented 3 years ago

I assume that only works if you're using cmd.exe as your shell?

There is a similar solution for PowerShell: https://superuser.com/a/886960/350929 I cannot tell for linux.

TBBle commented 3 years ago

This already works on Linux, it's only Windows where it's bypassed for some reason: https://github.com/python-poetry/poetry/issues/1642#issuecomment-753911955

dedale commented 3 years ago

I found a solution on Windows with AutoRun scripts.

[...]

Improved version displaying name of project: https://gist.github.com/dedale/85b603ea378caa34a397818b47ad46d9

Manezki commented 2 years ago

I adapted this Pipenv solution to the Poetry Powershell.

Add the following line to $profile file (e.g. by running notepad $profile in Powershell).

function prompt{
  if ($env:POETRY_ACTIVE -eq 1) {
    $poetry_env = ($env:VIRTUAL_ENV -split "\\")[-1]
    $venv = ($poetry_env -split "-")[0..(($poetry_env -split "-").Length-3)] -join "-"
    "($venv) $(Get-Location)> ";
  } else {
    "$(Get-Location)$('>' * ($nestedPromptLevel + 1))";
  }
}

I can confirm that misconfiguring above addition did not break my Powershell.

Result:

image

github-actions[bot] commented 6 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.