question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pipenv powershell shell should label prompt when inside pipenv shell (similar to venv)

See original GitHub issue

tl;dr pipenv shell should label the powershell prompt when inside a shell. This is what venv currently does. Something like
(pipenv: project1-LQTIN6Io) PS C:\Users\user1\Projects\project1>


venv module

When using the venv module and then entering the virtual environment shell, the shell prompt becomes labeled. Here’s an example in powershell:


     PS C:\Users\user1\Projects\project1> C:\Python\Python37\python.exe -m venv .venv
     PS C:\Users\user1\Projects\project1> .\.venv\Scripts\activate
     (.venv) PS C:\Users\user1\Projects\project1>

I find the prepended label “(.venv)” tremendously helpful. That prepended label is also colored green which is different than the remainder of the powershell prompt “PS C:\Users\user1\Projects>” in light-gray. It’s easy to visually distinguish the green string “(.venv)” was prepended to the default powershell prompt. I often have several powershell windows open. That prepended label helps me remember which powershell window is currently in a python virtual environment.

this pipenv enhancement follows venv module

This Enhancement is to recommend pipenv implement the same feature. I imagine this would look like


    PS C:\Users\user1\Projects\project1> C:\Python\Python37\Scripts\pipenv.exe --python 3
    Creating a virtualenv for this project…
    Pipfile: C:\Users\user1\Projects\project1\Pipfile
    Using C:/Python/Python37/python.exe (3.7.1) to create virtualenv…
    [=== ] Creating virtual environment...Using base prefix 'C:\\Python\\Python37'
    New python executable in C:\Users\user1\.virtualenvs\project1-LQTIN6Io\Scripts\python.exe
    Installing setuptools, pip, wheel...
    done.
    Running virtualenv with interpreter C:/Python/Python37/python.exe

    Successfully created virtual environment!
    Virtualenv location: C:\Users\user1\.virtualenvs\project1-LQTIN6Io

    PS C:\Users\user1\Downloads\deleteme> C:\Python\Python37\Scripts\pipenv.exe shell
    (pipenv: project1-LQTIN6Io) PS C:\Users\user1\Projects\project1>

Notice the prepended prompt string "(pipenv: project1-LQTIN6Io) ". Ideally, it would also be colorized different than the remaining prompt string.


This Issue is similar to pipenv Issue #40 (which addressed bash shell).

btw, after reading Pipenv: A Guide to the New Python Packaging Tool (realpython.com), I really want more pipenv!


Additional context

$ pipenv --support

Pipenv version: '2018.11.26'

Pipenv location: 'c:\\python\\python37\\lib\\site-packages\\pipenv'

Python location: 'c:\\python\\python37\\python.exe'

Python installations found:

  • 3.7.1: C:\Python\Python37\python.exe
  • 3.6.7: C:\Python\Python36\python.exe
  • 3.4: C:\Python\Python34\python.exe

PEP 508 Information:

{'implementation_name': 'cpython',
 'implementation_version': '3.7.1',
 'os_name': 'nt',
 'platform_machine': 'AMD64',
 'platform_python_implementation': 'CPython',
 'platform_release': '10',
 'platform_system': 'Windows',
 'python_full_version': '3.7.1',
 'python_version': '3.7',
 'sys_platform': 'win32'}

Powershell version:

PS C:\Users\user1 > $PSVersionTable.PSVersion

Major  Minor
-----  -----
5      1

System environment variables:

  • PIPENV_ACTIVE
  • PIP_DISABLE_PIP_VERSION_CHECK
  • PIP_PYTHON_PATH
  • PROMPT
  • PSMODULEPATH
  • PYTHONDONTWRITEBYTECODE
  • VIRTUAL_ENV
  • PIP_SHIMS_BASE_MODULE
  • PYTHONFINDER_IGNORE_UNSUPPORTED

Pipenv?specific environment variables:

  • PIPENV_ACTIVE: 1

Debug?specific environment variables:

  • PATH: ...
  • VIRTUAL_ENV: C:\Users\user1\.virtualenvs\foo-3kR5jZ-L

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:18
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

24reactions
DraugurHundurcommented, Nov 11, 2020

Here is what I use to get a specific prompt for each environment (faster than above as it does not run a command every time the prompt is evaluated):

function prompt{
  if ($env:PIPENV_ACTIVE -eq 1) {
    # @TODO: works only on Windows
    $venv = (($env:VIRTUAL_ENV -split "\\")[-1] -split "-")[0]
    "[pipenv:$venv] $(Get-Location)> ";
  } else {
    "$(Get-Location)$('>' * ($nestedPromptLevel + 1))";
  }
}

See https://ss64.com/ps/syntax-prompt.html on how to update your configuration to include this.

6reactions
jakovlev-fedorcommented, Apr 17, 2021

Here is some refactoring of code above I used original ‘Activate.ps1’ file from venv package for reference

  • prompt prefix is now green as in original venv
  • this modification doesn’t breaks original prompt
if ($env:PIPENV_ACTIVE -eq 1) {

    function _OLD_PROMPT { "" }
    Copy-Item -Path function:prompt -Destination function:_OLD_PROMPT


    $_PROMPT_PREFIX = (($env:VIRTUAL_ENV -split "\\")[-1] -split "-")[0]

    function prompt {
        Write-Host -NoNewline -ForegroundColor Green "($_PROMPT_PREFIX) " 
        _OLD_PROMPT
    
    }
}

Here is what I use to get a specific prompt for each environment (faster than above as it does not run a command every time the prompt is evaluated):

function prompt{
  if ($env:PIPENV_ACTIVE -eq 1) {
    # @TODO: works only on Windows
    $venv = (($env:VIRTUAL_ENV -split "\\")[-1] -split "-")[0]
    "[pipenv:$venv] $(Get-Location)> ";
  } else {
    "$(Get-Location)$('>' * ($nestedPromptLevel + 1))";
  }
}

See https://ss64.com/ps/syntax-prompt.html on how to update your configuration to include this.

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Windows: How to show environment when using pipenv?
When I run pipenv shell , it takes me to my new environment. PS C:\Users\micah\Dev\try-zappa> pipenv shell Launching subshell in virtual ...
Read more >
pipenv shell does not work in powershell ISE - Microsoft Q&A
I can run powershell ISE, and navigate to a folder where a pipenv is set up, e.g. if I run pipenv --venv I...
Read more >
pipenv Documentation - Read the Docs
Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the. Python...
Read more >
Pipenv - Virtual Environments have wrong builds - Python
I have copied my code below so that you can see what I have done. C:/…/project1> pipenv shell. Launching subshell in virtual environment…...
Read more >
PowerShell prompt doesn't show virtualenv : PY-22469
There's a workaround for this issue. Under Settings > Tools > Terminal you can uncheck the Activate virtualenv option and then set the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found