如何在 Mac 上使用 pipenv?

问题描述

通过pip(pip install pipenv)安装时,在zsh shell上找不到命令pipenv

如果通过brew安装:brew install pipenv,然后运行pipenv shell,出错

Loading .env environment variables...
Launching subshell in virtual environment...
Traceback (most recent call last):
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/bin/pipenv",line 8,in <module>
    sys.exit(cli())
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py",line 829,in __call__
    return self.main(*args,**kwargs)
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py",line 782,in main
    rv = self.invoke(ctx)
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py",line 1259,in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py",line 1066,in invoke
    return ctx.invoke(self.callback,**ctx.params)
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py",line 610,in invoke
    return callback(*args,**kwargs)
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/decorators.py",line 73,in new_func
    return ctx.invoke(f,obj,*args,**kwargs)
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/cli/command.py",line 429,in shell
    do_shell(
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/core.py",line 2387,in do_shell
    shell.fork_compat(*fork_args)
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/shells.py",line 106,in fork_compat
    c = pexpect.spawn(self.cmd,["-i"],dimensions=(dims.lines,dims.columns))
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/pexpect/pty_spawn.py",line 205,in __init__
    self._spawn(command,args,preexec_fn,dimensions)
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/pexpect/pty_spawn.py",line 276,in _spawn
    raise ExceptionPexpect('The command was not found or was not ' +
pipenv.vendor.pexpect.exceptions.ExceptionPexpect: The command was not found or was not executable: /use/bin/zsh.

没有命名 /use/bin/zsh 的路径。为什么不稳定?

shell 路径为

echo $SHELL
/bin/zsh

解决方法

你问了两个问题,真的。我将在下面的单独部分中回答每个问题:

如何修复该错误

Loading .env environment variables...
...
The command was not found or was not executable: /use/bin/zsh.

看起来像 in your .env file,你有 PIPENV_SHELL=/use/bin/zsh。那是不正确的。相反,它应该是

PIPENV_SHELL=/bin/zsh

甚至干脆

PIPENV_SHELL=zsh

或者您也可以将其删除。然后 pipenv shell 将简单地使用与您从中调用它相同的 shell。

如何在 macOS 上正确安装 pipenv

在 macOS 上安装 pipenv 的正确方法有点复杂,但如果您不想在升级 Python 版本时遇到问题,这是唯一的方法:

  1. 撤消您目前所做的操作:
    % pip uninstall pipenv
    % brew uninstall pipenv
    
  2. 将以下内容添加到您的 .zprofile 文件中:
    eval "$( brew shellenv )"
    export PYENV_VERSION=3.9.5  # Set your preferred Python version.
    export PYENV_ROOT=~/.pyenv
    export PIPX_BIN_DIR=~/.local/bin
    export -U PATH path         # -U eliminates duplicates
    path=( 
        $PIPX_BIN_DIR
        $PYENV_ROOT/{bin,shims} 
        $path
    )
    
  3. 打开一个新的终端窗口(使上述更改生效)并执行以下操作:
    % brew install pyenv
    % pyenv install $PYENV_VERSION # Install your preferred Python.
    % pyenv global $PYENV_VERSION  # Make it your default Python.
    % pip install -U pip           # Update pip.
    % pip install -U --user pipx   # Install pipx into ~/.local/bin
    % pipx install pipenv          # pipx is like brew but for Python.
    
  4. 将以下内容添加到您的 .zshrc 文件中:
    eval "$( pyenv init - )"
    eval "$( pip completion --zsh )"
    eval "$( register-python-argcomplete pipx )"
    eval "$( pipenv --completion )"
    
  5. 重新启动您的 shell(通过键入 exec zsh 或打开一个新的终端窗口)以使上述内容生效。