Pipenv: Completion is not working with zsh on macOS

Created on 25 Jul 2017  路  20Comments  路  Source: pypa/pipenv

Maybe this is related to #226 ?

$ pipsi install pipenv
Running virtualenv with interpreter /Users/markus/.local/venvs/pipsi/bin/python3.6
Using real prefix '/usr/local/var/pyenv/versions/3.6.1'
New python executable in /Users/markus/.local/venvs/pipenv/bin/python3.6
Also creating executable in /Users/markus/.local/venvs/pipenv/bin/python
Installing setuptools, pip, wheel...done.
Collecting pipenv
Collecting pew>=0.1.26 (from pipenv)
  Using cached pew-0.1.26-py2.py3-none-any.whl
Requirement already satisfied: pip in ./.local/venvs/pipenv/lib/python3.6/site-packages (from pipenv)
Collecting virtualenv (from pipenv)
  Using cached virtualenv-15.1.0-py2.py3-none-any.whl
Collecting pythonz-bd>=1.10.2 (from pew>=0.1.26->pipenv)
  Using cached pythonz_bd-1.11.4-py3-none-any.whl
Collecting virtualenv-clone>=0.2.5 (from pew>=0.1.26->pipenv)
Requirement already satisfied: setuptools>=17.1 in ./.local/venvs/pipenv/lib/python3.6/site-packages (from pew>=0.1.26->pipenv)
Collecting resumable-urlretrieve; python_version == "3.6" (from pythonz-bd>=1.10.2->pew>=0.1.26->pipenv)
  Using cached resumable_urlretrieve-0.1.5-py2.py3-none-any.whl
Collecting requests (from resumable-urlretrieve; python_version == "3.6"->pythonz-bd>=1.10.2->pew>=0.1.26->pipenv)
  Downloading requests-2.18.2-py2.py3-none-any.whl (88kB)
    100% |鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅| 92kB 1.7MB/s
Collecting certifi>=2017.4.17 (from requests->resumable-urlretrieve; python_version == "3.6"->pythonz-bd>=1.10.2->pew>=0.1.26->pipenv)
  Using cached certifi-2017.4.17-py2.py3-none-any.whl
Collecting urllib3<1.23,>=1.21.1 (from requests->resumable-urlretrieve; python_version == "3.6"->pythonz-bd>=1.10.2->pew>=0.1.26->pipenv)
  Downloading urllib3-1.22-py2.py3-none-any.whl (132kB)
    100% |鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅| 133kB 3.4MB/s
Collecting idna<2.6,>=2.5 (from requests->resumable-urlretrieve; python_version == "3.6"->pythonz-bd>=1.10.2->pew>=0.1.26->pipenv)
  Using cached idna-2.5-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests->resumable-urlretrieve; python_version == "3.6"->pythonz-bd>=1.10.2->pew>=0.1.26->pipenv)
  Using cached chardet-3.0.4-py2.py3-none-any.whl
Installing collected packages: certifi, urllib3, idna, chardet, requests, resumable-urlretrieve, pythonz-bd, virtualenv, virtualenv-clone, pew, pipenv
Successfully installed certifi-2017.4.17 chardet-3.0.4 idna-2.5 pew-0.1.26 pipenv-5.1.2 pythonz-bd-1.11.4 requests-2.18.2 resumable-urlretrieve-0.1.5 urllib3-1.22 virtualenv-15.1.0 virtualenv-clone-0.2.6
  Linked script /Users/markus/.local/bin/pipenv
Done.
$ pipenv --version
pipenv, version 5.1.2
$ _PIPENV_COMPLETE=source pipenv
Traceback (most recent call last):
  File "/Users/markus/.local/venvs/pipenv/lib/python3.6/site-packages/pipenv/vendor/click_completion.py", line 409, in get_auto_shell
    import psutil
  File "/Users/markus/.local/venvs/pipenv/lib/python3.6/site-packages/pipenv/vendor/psutil/__init__.py", line 136, in <module>
    from . import _psosx as _psplatform
  File "/Users/markus/.local/venvs/pipenv/lib/python3.6/site-packages/pipenv/vendor/psutil/_psosx.py", line 14, in <module>
    from . import _psutil_osx as cext
ImportError: cannot import name '_psutil_osx'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/markus/.local/bin/pipenv", line 11, in <module>
    sys.exit(cli())
  File "/Users/markus/.local/venvs/pipenv/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/Users/markus/.local/venvs/pipenv/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 692, in main
    _bashcomplete(self, prog_name, complete_var)
  File "/Users/markus/.local/venvs/pipenv/lib/python3.6/site-packages/pipenv/vendor/click_completion.py", line 297, in _shellcomplete
    echo(get_code(prog_name=prog_name, env_name=complete_var))
  File "/Users/markus/.local/venvs/pipenv/lib/python3.6/site-packages/pipenv/vendor/click_completion.py", line 387, in get_code
    shell = get_auto_shell()
  File "/Users/markus/.local/venvs/pipenv/lib/python3.6/site-packages/pipenv/vendor/click_completion.py", line 415, in get_auto_shell
    raise click.UsageError("Please explicitly give the shell type or install the psutil package to activate the"
click.exceptions.UsageError: Please explicitly give the shell type or install the psutil package to activate the automatic shell detection.
$ _PIPENV_COMPLETE=source-zsh pipenv

#compdef pipenv
_pipenv() {
  eval $(env COMMANDLINE="${words[1,$CURRENT]}" _PIPENV_COMPLETE=complete-zsh  pipenv)
}
if [[ "$(basename ${(%):-%x})" != "_pipenv" ]]; then
  autoload -U compinit && compinit
  compdef _pipenv pipenv
fi

But it's not completing anything ...

$ eval $(_PIPENV_COMPLETE=source-zsh pipenv)
$ pipenv i<Tab>
-- file --
Applications/             Downloads/                Movies/                   VirtualBox\ VMs/

Most helpful comment

Ok, I found a workaround which "works for me":tm:, but expects oh-my-zsh to be used:

pipenv --completion >> .oh-my-zsh/custom/pipenv_completion.zsh

All files there get auto-loaded on zsh startup. Bonus: It's faster since pipenv does not need to be executed on shell startup. I'm sure there are standard/non-oh-my-zsh places that can be used instead, but my vanilla-zsh-fu is not strong enough for this.

All 20 comments

I have the same issue on macOS, but it works in a created virtual environment:

$ pipenv check
check      -- Checks PEP 508 markers provided in Pipfile.
install    -- Installs provided packages and adds them to...
lock       -- Generates Pipfile.lock.
run        -- Spawns a command installed into the...
shell      -- Spawns a shell within the virtualenv.
uninstall  -- Un-installs a provided package and removes it...
update     -- Updates pip to latest version, uninstalls all...

BTW, It's really slow. I guess it requires Python running to provide a complementation?

it does.

do you have requests[security] installed in the same virtualenv? that'd slow things down a lot.

That's why I recommend installing pipenv w/ pipsi.

Not working here at all. :(

@kennethreitz I really have requests installed, but I cannot image how could requests slow down shell autocomplimentation.

I have another doubt -- when I did source pipenv in zsh, I got the full help doc of ImageMagic! And ends with:

import: delegate library support not built-in '' (X11) @ error/import.c/ImportImageCommand/1294.
from: can't read /var/mail/pipenv
/usr/local/bin/pipenv: parse error near `)'

What may this be related to?

@kxxoling but do you have requests[security] installed? (e.g. pyopenssl?) it vastly slows down import time of requests.

The library being used for completion is here:

https://github.com/click-contrib/click-completion

@kennethreitz I didn't install requests[security] literally, but pyOpenSSL, cryptography and idna are all installed. Maybe they are required by other dependencies.

If you're using Python 2.6 I believe pipenv will install requests[security] automatically. Anyway, this is why you're seeing such slow responsiveness.

@kennethreitz I'm using Python 2.7.13 actually. But I still don't understand why would requests[security] slows down pipenv autocomplete. Could this be optimised?

I tried eval "$(_PIPENV_COMPLETE=source-zsh pipenv)" in a Python 3.6 environment, the responsiveness is much faster and acceptable now.

This is working for me, at least on my Linux machine.

@tilgovi what shell and command are you using?

In both cases it's zsh. On the Mac it's ZSH with homebrew. Dotfiles are the same (sync'd through git). Works fine on Linux but not MacOS, for some reason.

Ooh, wait. I was forgetting to quote it on my Mac.

eval "$(_PIPENV_COMPLETE=source-zsh pipenv)"

... yields "basename: illegal option -- z"

@tilgovi I thought you said it was working?

@tilgovi try removing the quotes ...

BTW.: It works now for me (outside and inside a virtual env) but I have no clue why...

@kennethreitz Sorry to revive a closed issue, but I have the exact same problems using pipenv with zsh (both installed through Homebrew) on macOS: Doing eval "$(pipenv --completion)" gives me:

basename: illegal option -- z
usage: basename string [suffix]
       basename [-a] [-s suffix] string [...]

Interestingly, the completion for pipenv works after I do that manually in the shell. If I put the eval in my .zshrc I don't get the error, but I also don't get the completion... :(

Ok, I found a workaround which "works for me":tm:, but expects oh-my-zsh to be used:

pipenv --completion >> .oh-my-zsh/custom/pipenv_completion.zsh

All files there get auto-loaded on zsh startup. Bonus: It's faster since pipenv does not need to be executed on shell startup. I'm sure there are standard/non-oh-my-zsh places that can be used instead, but my vanilla-zsh-fu is not strong enough for this.

Was this page helpful?
0 / 5 - 0 ratings