How to use pipenv on mac?

Question:

When install it by pip(pip install pipenv), on zsh shell can’t find the command pipenv.

If install it by brew: brew install pipenv, then run pipenv shell, got error

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.

There isn’t a path naming /use/bin/zsh. Why it’s unstable?

The shell path is

echo $SHELL
/bin/zsh
Asked By: Miantian

||

Answers:

You’re asking two questions, really. I’ll answer each in a separate section:

How to fix that error

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

Looks like in your .env file, you have PIPENV_SHELL=/use/bin/zsh. That’s incorrect. Instead,

  • it should be
    PIPENV_SHELL=/bin/zsh
    
  • or simply
    PIPENV_SHELL=zsh
    
  • or you can just remove it. Then pipenv shell will automatically use the same shell as from which you invoked it.

How to properly install pipenv on macOS

The right way to install pipenv on macOS is convoluted, but it’s the only way to avoid running into trouble whenever you upgrade Python:

  1. Undo what you’ve done so far:
    % pip uninstall pipenv
    % brew uninstall pipenv
    
  2. Add to your .zshrc file:
    eval "$( brew shellenv )"
    
    # Set your preferred Python version.
    export PYENV_VERSION=3.10.6
    
    export PIPX_BIN_DIR=~/.local/bin
    export PYENV_ROOT=~/.pyenv
    
    # -U eliminates duplicates.
    export -U PATH path         
    path=( 
        $PIPX_BIN_DIR
        $PYENV_ROOT/{bin,shims} 
        $path
    )
    
    # Updates the global python, if necessary, and installs/upgrades pipenv.
    pybake() {
      # Install pyenv, if necessary.
      command -v pyenv > /dev/null || 
          brew install pyenv
    
      # Install your preferred Python.
      # Does nothing if $PYENV_VERSION hasn't changed.
      pyenv install --skip-existing $PYENV_VERSION
    
      pyenv global $PYENV_VERSION  # Make it your default.
      pip install -U pip           # Update pip.
    
      # Install pipx (into ~/.local/bin) or update it.
      # pipx is like brew, but for Python.
      pip install -U --user pipx   
    
      # Install or update pipenv.
      pipx ${${$( command -v pipenv ):+upgrade}:-install} pipenv
    }
    
  3. Run on the command line:
    % exec zsh
    % pybake
    
  4. Add to your .zshrc file:
    eval "$( pyenv init - )"
    eval "$( pip completion --zsh )"
    eval "$( register-python-argcomplete pipx )"
    
  5. Run exec zsh again or open a new terminal tab.

Updating pipenv

After following the steps above, to updatepipenv, all you need to do is the following:

  1. If you want to update your global python, too, then first update the line in your .zshrc that starts with export PYENV_VERSION=.
  2. Run on the command line:
    % exec zsh
    % pybake
    
Answered By: Marlon Richert