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.

powershell: `pipenv shell` still spawns CMD

See original GitHub issue

Issue description

I have pipenv, version 2018.11.26 installed on powershell 5.1.17763.1007 and it still spawns the CMD instead of a powershell window

> echo "This is powershell"
"This is powershell"
> pipenv shell
Launching subshell in virtual environment…
Microsoft Windows [Version 10.0.17763.1217]
(c) 2018 Microsoft Corporation. All rights reserved.

(yyyy-TjK7vz5S) C:\Users\xxxx\yyyy>  echo "This is CMD"
"This is CMD"

This was mentioned once in issue https://github.com/pypa/pipenv/issues/915 and there is a merged PR but it seems to have no effect on my system. This puzzles me, since I thought it was fixed. Have I missed something?

Expected result

pipenv shell spawning the same shell as it was called in, in this case powershell.

Actual result

Spawns CMD shell.

Steps to replicate

# install pyenv to easily install other python versions
choco install pyenv-win -y
refreshenv

# install a first python version
pyenv install -q 3.6.8
pyenv rehash

# install packages
python -m pip install --upgrade pip
pyenv rehash
pip install pipenv
pyenv rehash

refreshenv

# start pipenv
pipenv shell

$ pipenv --support

Pipenv version: '2018.11.26'

Pipenv location: 'c:\\users\\andbo\\.pyenv\\pyenv-win\\versions\\3.6.0\\lib\\site-packages\\pipenv'

Python location: 'c:\\users\\andbo\\.pyenv\\pyenv-win\\versions\\3.6.0\\python.exe'

Python installations found:

  • 3.8.1: C:\Users\andbo\.pyenv\pyenv-win\versions\3.8.1\python.exe
  • 3.7.0: C:\Users\andbo\.pyenv\pyenv-win\versions\3.7.0\python.exe
  • 3.6.0: C:\Users\andbo\.pyenv\pyenv-win\versions\3.6.0\python.exe

PEP 508 Information:

{'implementation_name': 'cpython',
 'implementation_version': '3.6.0',
 'os_name': 'nt',
 'platform_machine': 'AMD64',
 'platform_python_implementation': 'CPython',
 'platform_release': '10',
 'platform_system': 'Windows',
 'platform_version': '10.0.17763',
 'python_full_version': '3.6.0',
 'python_version': '3.6',
 'sys_platform': 'win32'}

System environment variables:

  • ALLUSERSPROFILE
  • APPDATA
  • CHOCOLATEYINSTALL
  • CHOCOLATEYLASTPATHUPDATE
  • CHOCOLATEYTOOLSLOCATION
  • COMMONPROGRAMFILES
  • COMMONPROGRAMFILES(X86)
  • COMMONPROGRAMW6432
  • COMPUTERNAME
  • COMSPEC
  • DRIVERDATA
  • FPS_BROWSER_APP_PROFILE_STRING
  • FPS_BROWSER_USER_PROFILE_STRING
  • HOMEDRIVE
  • HOMEPATH
  • LOCALAPPDATA
  • LOGONSERVER
  • NUMBER_OF_PROCESSORS
  • ONEDRIVE
  • ONEDRIVECONSUMER
  • OS
  • PATH
  • PATHEXT
  • PROCESSOR_ARCHITECTURE
  • PROCESSOR_ARCHITEW6432
  • PROCESSOR_IDENTIFIER
  • PROCESSOR_LEVEL
  • PROCESSOR_REVISION
  • PROGRAMDATA
  • PROGRAMFILES
  • PROGRAMFILES(X86)
  • PROGRAMW6432
  • PROMPT
  • PSMODULEPATH
  • PUBLIC
  • SESSIONNAME
  • SYSTEMDRIVE
  • SYSTEMROOT
  • TEMP
  • TMP
  • USERDOMAIN
  • USERDOMAIN_ROAMINGPROFILE
  • USERNAME
  • USERPROFILE
  • WINDIR
  • PIP_DISABLE_PIP_VERSION_CHECK
  • PYTHONDONTWRITEBYTECODE
  • PIP_SHIMS_BASE_MODULE
  • PIP_PYTHON_PATH
  • PYTHONFINDER_IGNORE_UNSUPPORTED

Pipenv?specific environment variables:

Debug?specific environment variables:

  • PATH: C:\Users\andbo\.pyenv\pyenv-win\versions\3.6.0\;C:\Python38\Scripts\;C:\Python38\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\ProgramData\chocolatey\bin;C:\Program Files\Git\cmd;C:\Program Files\Microsoft VS Code\bin;C:\Users\andbo\AppData\Local\Microsoft\WindowsApps;C:\Users\andbo\AppData\Local\atom\bin;C:\Users\andbo\.pyenv\pyenv-win\bin;C:\Users\andbo\.pyenv\pyenv-win\shims;

Contents of Pipfile (‘C:\Users\andbo\projects\agv_windows_apps\xlsx2xml\Pipfile’):

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[dev-packages]

[packages]
openpyxl = "*"
dicttoxml = "*"
pytest = "*"
pytest-watch = "*"
validators = "*"
pyinstaller = "*"

[requires]
python_version = "3.6"

Contents of Pipfile.lock (‘C:\Users\andbo\projects\agv_windows_apps\xlsx2xml\Pipfile.lock’):

{
    "_meta": {
        "hash": {
            "sha256": "9eed1efd2e082e2ac4b854cdb46fdedddaa551c9b2bd7ee645bf012add6de240"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.6"
        },
        "sources": [
            {
                "name": "pypi",
                "url": "https://pypi.org/simple",
                "verify_ssl": true
            }
        ]
    },
    "default": {
        "altgraph": {
            "hashes": [
                "sha256:1f05a47122542f97028caf78775a095fbe6a2699b5089de8477eb583167d69aa",
                "sha256:c623e5f3408ca61d4016f23a681b9adb100802ca3e3da5e718915a9e4052cebe"
            ],
            "version": "==0.17"
        },
        "attrs": {
            "hashes": [
                "sha256:08a96c641c3a74e44eb59afb61a24f2cb9f4d7188748e76ba4bb5edfa3cb7d1c",
                "sha256:f7b7ce16570fe9965acd6d30101a28f62fb4a7f9e926b3bbc9b61f8b04247e72"
            ],
            "version": "==19.3.0"
        },
        "colorama": {
            "hashes": [
                "sha256:7d73d2a99753107a36ac6b455ee49046802e59d9d076ef8e47b61499fa29afff",
                "sha256:e96da0d330793e2cb9485e9ddfd918d456036c7149416295932478192f4436a1"
            ],
            "version": "==0.4.3"
        },
        "decorator": {
            "hashes": [
                "sha256:41fa54c2a0cc4ba648be4fd43cff00aedf5b9465c9bf18d64325bc225f08f760",
                "sha256:e3a62f0520172440ca0dcc823749319382e377f37f140a0b99ef45fecb84bfe7"
            ],
            "version": "==4.4.2"
        },
        "dicttoxml": {
            "hashes": [
                "sha256:ea44cc4ec6c0f85098c57a431a1ee891b3549347b07b7414c8a24611ecf37e45"
            ],
            "index": "pypi",
            "version": "==1.7.4"
        },
        "docopt": {
            "hashes": [
                "sha256:49b3a825280bd66b3aa83585ef59c4a8c82f2c8a522dbe754a8bc8d08c85c491"
            ],
            "version": "==0.6.2"
        },
        "et-xmlfile": {
            "hashes": [
                "sha256:614d9722d572f6246302c4491846d2c393c199cfa4edc9af593437691683335b"
            ],
            "version": "==1.0.1"
        },
        "importlib-metadata": {
            "hashes": [
                "sha256:06f5b3a99029c7134207dd882428a66992a9de2bef7c2b699b5641f9886c3302",
                "sha256:b97607a1a18a5100839aec1dc26a1ea17ee0d93b20b0f008d80a5a050afb200b"
            ],
            "markers": "python_version < '3.8'",
            "version": "==1.5.0"
        },
        "jdcal": {
            "hashes": [
                "sha256:1abf1305fce18b4e8aa248cf8fe0c56ce2032392bc64bbd61b5dff2a19ec8bba",
                "sha256:472872e096eb8df219c23f2689fc336668bdb43d194094b5cc1707e1640acfc8"
            ],
            "version": "==1.4.1"
        },
        "more-itertools": {
            "hashes": [
                "sha256:5dd8bcf33e5f9513ffa06d5ad33d78f31e1931ac9a18f33d37e77a180d393a7c",
                "sha256:b1ddb932186d8a6ac451e1d95844b382f55e12686d51ca0c68b6f61f2ab7a507"
            ],
            "version": "==8.2.0"
        },
        "openpyxl": {
            "hashes": [
                "sha256:547a9fc6aafcf44abe358b89ed4438d077e9d92e4f182c87e2dc294186dc4b64"
            ],
            "index": "pypi",
            "version": "==3.0.3"
        },
        "packaging": {
            "hashes": [
                "sha256:3c292b474fda1671ec57d46d739d072bfd495a4f51ad01a055121d81e952b7a3",
                "sha256:82f77b9bee21c1bafbf35a84905d604d5d1223801d639cf3ed140bd651c08752"
            ],
            "version": "==20.3"
        },
        "pathtools": {
            "hashes": [
                "sha256:7c35c5421a39bb82e58018febd90e3b6e5db34c5443aaaf742b3f33d4655f1c0"
            ],
            "version": "==0.1.2"
        },
        "pluggy": {
            "hashes": [
                "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0",
                "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"
            ],
            "version": "==0.13.1"
        },
        "py": {
            "hashes": [
                "sha256:5e27081401262157467ad6e7f851b7aa402c5852dbcb3dae06768434de5752aa",
                "sha256:c20fdd83a5dbc0af9efd622bee9a5564e278f6380fffcacc43ba6f43db2813b0"
            ],
            "version": "==1.8.1"
        },
        "pyinstaller": {
            "hashes": [
                "sha256:3730fa80d088f8bb7084d32480eb87cbb4ddb64123363763cf8f2a1378c1c4b7"
            ],
            "index": "pypi",
            "version": "==3.6"
        },
        "pyparsing": {
            "hashes": [
                "sha256:4c830582a84fb022400b85429791bc551f1f4871c33f23e44f353119e92f969f",
                "sha256:c342dccb5250c08d45fd6f8b4a559613ca603b57498511740e65cd11a2e7dcec"
            ],
            "version": "==2.4.6"
        },
        "pytest": {
            "hashes": [
                "sha256:0e5b30f5cb04e887b91b1ee519fa3d89049595f428c1db76e73bd7f17b09b172",
                "sha256:84dde37075b8805f3d1f392cc47e38a0e59518fb46a431cfdaf7cf1ce805f970"
            ],
            "index": "pypi",
            "version": "==5.4.1"
        },
        "pytest-watch": {
            "hashes": [
                "sha256:06136f03d5b361718b8d0d234042f7b2f203910d8568f63df2f866b547b3d4b9"
            ],
            "index": "pypi",
            "version": "==4.2.0"
        },
        "six": {
            "hashes": [
                "sha256:236bdbdce46e6e6a3d61a337c0f8b763ca1e8717c03b369e87a7ec7ce1319c0a",
                "sha256:8f3cd2e254d8f793e7f3d6d9df77b92252b52637291d0f0da013c76ea2724b6c"
            ],
            "version": "==1.14.0"
        },
        "validators": {
            "hashes": [
                "sha256:b192e6bde7d617811d59f50584ed240b580375648cd032d106edeb3164099508"
            ],
            "index": "pypi",
            "version": "==0.14.2"
        },
        "watchdog": {
            "hashes": [
                "sha256:c560efb643faed5ef28784b2245cf8874f939569717a4a12826a173ac644456b"
            ],
            "version": "==0.10.2"
        },
        "wcwidth": {
            "hashes": [
                "sha256:cafe2186b3c009a04067022ce1dcd79cb38d8d65ee4f4791b8888d6599d1bbe1",
                "sha256:ee73862862a156bf77ff92b09034fc4825dd3af9cf81bc5b360668d425f3c5f1"
            ],
            "version": "==0.1.9"
        },
        "zipp": {
            "hashes": [
                "sha256:aa36550ff0c0b7ef7fa639055d797116ee891440eac1a56f378e2d3179e0320b",
                "sha256:c599e4d75c98f6798c509911d08a22e6c021d074469042177c8c86fb92eefd96"
            ],
            "version": "==3.1.0"
        }
    },
    "develop": {}
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13

github_iconTop GitHub Comments

9reactions
atschwarzcommented, May 20, 2021

Hi,

I had the same issue. Looking into the sourcecode of pipenv into shells.py gives me the hint, that the powershell is supported and you can enable it using the environment variable PIPENV_SHELL. So you can specify it via Windows settings or run

$Env:PIPENV_SHELL = "powershell"

before running pipenv shell.

If you want to pimp your command prompt with a pipenv prefix, you can look at this link: https://github.com/pypa/pipenv/issues/3505

At the end of the discussion they offer a function you can put at into your powershell profile, e.g. with visual code installed, open it with the following commands

pipenv shell
code $profile

and append this code to the file:

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]
    $pipenv_name = ((($(pip -V) -split ' ')[3]) -split '\\')[4]

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

You have to exit and reenter the pipenv shell to activate the prefix stuff to see it in action.

Ahh, another hint. If you have the problem, that the Powershell Module PSReadline is not able to be loaded, you have to set the Powershell Execution Policy e.g. to RemoteSigned inside your pipenv shell. The change of the Execution Policy is only allowed with administrator privileges. So you have to start a powershell as administrator, navigate to your python project directory, enter the pipenv shell and then run

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

After that, the error goes away and you can use your pipenv shell without administrator privileges 😉 Hope that will help you 😃

4reactions
awkimballcommented, Sep 25, 2021

Still wondering, why it’s not the default behavior to use the same shell as it was spawned from 🤔

@andb0t unfortunately on windows it’s a lot more difficult than unix because powershell doesn’t set any unique environment variables that can be easily used to identify the parent shell.

Further complicating things is that running pipenv shell in powershell actually first spawns a cmd.exe prompt and then runs the actual pipenv command. Effectively, cmd.exe is the actual ‘parent’ shell by the time pipenv runs which leads into pipenv’s behavior:

Currently pipenv uses a combination of methods to determine the shell that spawned it, but on windows it primarily uses the library shellingham which works by inspecting the pid and executable path for the parent process of the current python process. If you happen to use the absolute path of your python install rather than a pyenv shim when running your commands: <python_path> -m pipenv shell you can actually get it to run directly from powershell without cmd and sorta bypass this issue. Not 100% sure why that is yet.

Ultimately one solution that I am considering proposing to the maintainer of shellingham is just to continue inspecting the pid and executable path of more parent processes as long as they continue to match the list of shell names until you reach the root shell, because eventually the parent process will be the terminal emulator or something else not in the list of shells.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pipenv shell Launching subshell in virtual environment ...
So I run: pipenv shell and get only this message: Launching subshell in virtual environment… 'powershell.exe' is not recognized as an internal ...
Read more >
pipenv Documentation
Using $ pipenv run ensures that your installed packages are available to your script. It's also possible to spawn a new shell that...
Read more >
pipenv Documentation
It's also possible to spawn a new shell that ensures all commands have access to your installed packages with $ pipenv shell.
Read more >
Pipenv
txt from Pipfile.lock. run Spawns a command installed into the virtualenv. scripts Lists scripts in current environment config. shell Spawns a shell within...
Read more >
pipenv shell does not work in powershell ISE
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 >

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