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

Update prompt environment variable when opening shell #1642

Closed
2 tasks done
rfkortekaas opened this issue Nov 28, 2019 · 13 comments · Fixed by #5053
Closed
2 tasks done

Update prompt environment variable when opening shell #1642

rfkortekaas opened this issue Nov 28, 2019 · 13 comments · Fixed by #5053
Labels
kind/feature Feature requests/implementations

Comments

@rfkortekaas
Copy link

  • I have searched the issues of this repo and believe that this is not a duplicate.
  • I have searched the documentation and believe that my question is not covered.

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.

@rfkortekaas rfkortekaas added the kind/feature Feature requests/implementations label Nov 28, 2019
@clintonroy
Copy link
Contributor

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

@rfkortekaas
Copy link
Author

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

wshayes commented Dec 4, 2019

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

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

yelled1 commented Apr 28, 2020

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

TBBle commented Jan 4, 2021

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

dedale commented Mar 16, 2021

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 �[92m$p$g�[0m 
@set RC_CMD=1
:skip
@if "%POETRY_ACTIVE%"=="1" prompt �[95mpoetry �[92m$p$g�[0m 

Result:
image

⚠️ in case of errors in this script, it will not be possible to launch a cmd anymore.

@TBBle
Copy link
Contributor

TBBle commented Mar 17, 2021

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

@dedale
Copy link

dedale commented Mar 17, 2021

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

TBBle commented Mar 17, 2021

This already works on Linux, it's only Windows where it's bypassed for some reason: #1642 (comment)

@dedale
Copy link

dedale commented Mar 28, 2021

I found a solution on Windows with AutoRun scripts.

[...]

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

@Manezki
Copy link

Manezki commented Nov 19, 2021

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

Copy link

github-actions bot commented Mar 2, 2024

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/feature Feature requests/implementations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants