How to use pipenv on mac?
Asked Answered
E

3

19

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
Evanthe answered 28/5, 2021 at 5:56 Comment(6)
Are you sure it says /use, with an e, not /usr, with an r? /usr/bin/zsh would be a somewhat reasonable place to look for zsh (but not on a Mac, apparently), but /use/bin/zsh is just bonkers.Stephan
@TurePålsson Yes, I added the full error message.Evanthe
@Evanthe : So there is obviously a typo in some script. Search for where /use/bin/ is written, and fix this using your text editior.Wensleydale
@Wensleydale When I ran ls /use/bin/, got "/use/bin/": No such file or directory (os error 2). There isn't this directory.Evanthe
Of course. What did you expect???Wensleydale
Consider using pyenv instead of managing python and pip manuallyBucharest
H
29

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.
    # 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
      }
    }
    
  3. Run:

    % exec zsh  # Restart your shell.
    % 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 update pipenv, all you need to do is run:

```shell
% pybake && exec zsh
```
Helvetia answered 28/5, 2021 at 12:23 Comment(5)
Your final line draws an error: Error: No such option: --completion Did you mean --python? Open issue here but it looks like they have no plans to add completion back: github.com/pypa/pipenv/issues/4991 If you remove eval "$( pipenv --completion )" you then begin to receive "command not found: complete". It looks like this entire procedure may need an update.Croupier
OK, that sucks; it used to work. Thanks for letting me know; I’ll remove that line.Helvetia
That script doesn't work for me. While running pybake I get: Successfully installed argcomplete-3.0.5 click-8.1.3 pipx-1.2.0 userpath-1.8.0 pybake:17: command not found: pipx. And the second time exec zsh is run it gives: .zshrc:53: command not found: register-python-argcomplete.Crayfish
@Crayfish Sorry, I'm not able to reproduce that. Feel free to open a new question for this.Helvetia
I have ran it now and have a same issue as @WebSnake. I've run export PATH=$(python3 -m site --user-base)/bin:$PATH after and rerunning the script worked.Crosslink
R
0

Update for 2024, tested on macOS.

After installing the latest Python version, install pipenv inside the new Python:

~ python3.12 -m pip install pipenv

Next, add the path and create a Symlink to the "python" command (to .zprofile; not shown here, ask if not clear, path maybe not necessary). Then use pipenv from inside of your new python version (for example for installing dependencies inside a project), so you don´t have to struggle with the setup of the pipenv command globally:

~ cd path/to/project
~ python -m pipenv install
Reeves answered 30/7 at 11:19 Comment(0)
A
-1

This works 100% for MacOS

How to setup virtual env using pipenv

brew install pipenv # if you have brew installed

pip3 install pipenv # if you don't have brew installed

Open the ~/.bash_profile using a text editor by the following command

vi ~/.bash_profile # i used vi text editor

And write the following statements at the end of .bash_profile


export LC_ALL=en_US.UTF-8

export LANG=en_US.UTF-8

Then use pipenv easily without issues by the following commands:

pipenv shell # To activate pipenv
exit # deactivate and quit
pipenv install <libraryName> # install a library

I hope this helps. Happy coding!

Anomie answered 11/2 at 0:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.