如何在 mac 上使用 pipenv?

How to use pipenv on mac?

通过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/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/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 的正确方法有点复杂,但如果您不想 运行 在升级 [=53] 时遇到问题,这是唯一的方法=]版本:

  1. 撤消到目前为止所做的操作:
    % pip uninstall pipenv
    % brew uninstall pipenv
    
  2. 将以下内容添加到您的 .zshrc 文件中:
    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. 打开一个新的终端window(所以上面的更改生效)并执行以下操作:
    % 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 或打开一个新终端 window)以使上述生效。