如何在 Mac 上使用 Pipenv?

Mia*_*ian 18 python macos zsh pipenv pipenv-install

当通过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.
Run Code Online (Sandbox Code Playgroud)

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

外壳路径是

echo $SHELL
/bin/zsh
Run Code Online (Sandbox Code Playgroud)

Mar*_*ert 29

你确实在问两个问题。我将在单独的部分中回答每个问题:

如何修复该错误

Loading .env environment variables...
...
The command was not found or was not executable: /use/bin/zsh.
Run Code Online (Sandbox Code Playgroud)

看起来在你的.env文件中,你有PIPENV_SHELL=/use/bin/zsh。这是不正确的。反而,

如何pipenv在 macOS 上正确安装

在 macOS 上安装的正确方法pipenv很复杂,但这是避免升级 Python 时遇到麻烦的唯一方法:

  1. 撤消您迄今为止所做的操作:

    PIPENV_SHELL=zsh
    
    Run Code Online (Sandbox Code Playgroud)
  2. 添加到您的.zshrc文件:

    % pip uninstall pipenv
    % brew uninstall pipenv
    
    Run Code Online (Sandbox Code Playgroud)
  3. 跑步:

    eval "$( brew shellenv )"
    
    # Set your preferred python version.
    # If you just want the latest release, you don't need to 
    # specify anything more than the major version number.
    export PYENV_VERSION=3
    
    # Tell pyenv where to keep your python installations.
    export PYENV_ROOT=~/.pyenv
    
    # Tell pipx where to install executables.
    # pipx is like brew, but for python.
    export PIPX_BIN_DIR=~/.local/bin
    
    # -U eliminates duplicates.
    export -U PATH path         
    path=( 
        $PIPX_BIN_DIR
        $PYENV_ROOT/{bin,shims} 
        $path
    )
    
    # Installs/updates pipenv and all of its dependencies.
    pybake() {
      # If any commands fail, exit the function.
      setopt LOCAL_OPTIONS ERR_RETURN
    
      install-or-upgrade() {
        print -n ${${$( command -v $1 ):+upgrade}:-install} $1
      }
    
      {
        brew ${=$( install-or-upgrade pyenv )}
        pyenv install --skip-existing $PYENV_VERSION
        pip install --upgrade pip
    
        # --user installs to ~/.local/bin
        pip install --upgrade --user pipx
    
        pipx ${=$( install-or-upgrade pipenv )}
    
      } always {
        unfunction install-or-upgrade
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 添加到您的.zshrc文件:

    % exec zsh  # Restart your shell.
    % pybake
    
    Run Code Online (Sandbox Code Playgroud)
  5. 再次运行exec zsh或打开新的终端选项卡。

更新中pipenv

按照上述步骤后,要更新pipenv,您需要做的就是运行:

```shell
% pybake && exec zsh
```
Run Code Online (Sandbox Code Playgroud)