pyenv giving shopt command not found error on macos
Asked Answered
P

1

3

I have been using pyenv for managing python versions and virtual environments on my mac for some time now. Recently I reinstalled pyenv and since then on every pyenv command I try to run, I get the following error

/usr/local/bin/pyenv:94: command not found: shopt

My default shell is the latest MacOS's default shell i.e. zsh.
On searching for the fix, I found out it has something to do with bash interfering with the zsh.
My default shell is zsh in settings, the contents of ~/.bash_profile, /etc/profile & /etc/bashrc are all commented out but I am still having the issue.
My OS Verion is: 10.15.4.
I tried reinstalling pyenv as well but I am getting the same error.

I am unable to run ANY pyenv related command.

Edit 1

I already have these lines in my .zshrc as per the documentation, but as you can see, even this run a command pyenv root and this gives me the same shopt error.

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$(pyenv root)/shims:$(pyenv root)/completions/pyenv.zsh:$PATH"

Edit 2

Run env and add the output in your question

TMPDIR=/var/folders/gm/t0h6v8jx4bqd6cj73_k27myw0000gp/T/
XPC_FLAGS=0x0
TERM_PROGRAM_VERSION=433
TERM_PROGRAM=Apple_Terminal
XPC_SERVICE_NAME=0
TERM_SESSION_ID=55A65E3A-9B71-4C8A-81B8-0170EEAE3DCE
TERM=xterm-256color
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.j6zkqCw6Of/Listeners
SHELL=/bin/zsh
HOME=/Users/abc
LOGNAME=abc
USER=abc
PATH=/usr/local/opt/[email protected]/bin:/Users/saadali/Library/Python/3.7/bin:/usr/local/Cellar/[email protected]/9.6.16/bin/:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/abc/bin:/usr/local/opt/rabbitmq/sbin
SHLVL=1
PYTHONPATH=
LANGUAGE=en_US.UTF-8
port=
LC_NUMERIC=en_US.UTF-8
LC_TIME=en_US.UTF-8
LC_COLLATE=en_US.UTF-8
LC_MONETARY=en_US.UTF-8
LC_PAPER=en_US.UTF-8
LC_NAME=en_US.UTF-8
LC_ADDRESS=en_US.UTF-8
LC_TELEPHONE=en_US.UTF-8
LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION=en_US.UTF-8
CPPFLAGS=-I/usr/local/opt/[email protected]/include
LDFLAGS=-L/usr/local/opt/[email protected]/lib
CFLAGS=-I/usr/local/opt/[email protected]/include
LANG=en_US.UTF-8
LC_MESSAGES=en_US.UTF-8
LC_ALL=en_US.UTF-8
LC_CTYPE=en_US.UTF-8
_=/usr/bin/env

Edit 3

Please read this chat first before recommending to try something, as I might have already tried it. Chat starts at Apr 29 02:01 UTC +5 and then moves to a thread.

Edit 4

After setting PYENV_DEBUG=1, here the output

(vmds) saadali@A006-00276 ~ :~$ pyenv
+ [:22] enable -f /../libexec/pyenv-realpath.dylib realpath
+ [:29] [ -z '' ']'
+ [:31] READLINK=+ [:31] type -p greadlink readlink
+ [:31] READLINK=+ [:31] head -1
+ [:31] READLINK='greadlink not found' 
+ [:32] [ -n 'greadlink not found' ']'
+ [:57] [ -z '' ']'
+ [:58] PYENV_ROOT=/Users/abc/.pyenv 
+ [:62] export PYENV_ROOT
+ [:65] [ -z '' ']'
+ [:66] [ -n '' ']'
+ [:82] [ -z '' ']'
+ [:83] PYENV_DIR=/Users/abc 
+ [:86] [ ! -d /Users/abc ']'
+ [:86] [ ! -e /Users/abc ']'
+ [:90] PYENV_DIR=+ [:90] cd /Users/abc
+ [:90] echo /Users/abc
+ [:90] PYENV_DIR=/Users/abc 
+ [:91] export PYENV_DIR
+ [:94] shopt -s nullglob
/usr/local/bin/pyenv:94: command not found: shopt
Pothook answered 28/4, 2020 at 11:8 Comment(11)
What's the first line of /usr/local/bin/pyenv?Bersagliere
What's the output of which shopt?Ballocks
Run env and add the output of the same to your questionCroup
@MarkSetchell first line is #!/usr/bin/env bash but this problem happens in bash terminal too.Pothook
@con-- shopt is an internal bash command, on zsh output of which shopt is of course command not found, on bash terminal it returns nothing as it's not a command, but if I do simple shopt, it shows some shopt related stuff.Pothook
@TarunLalwani done.Pothook
@Pothook Is /usr/bin/env bash really executing /usr/bin/bash? What happens if you change the shebang in your pyenv executable to #!/usr/bin/bash? Also, remember you can run pyenv with debug output by setting PYENV_DEBUG=1.Government
@Government added the debug output in question. Also, changing /usr/bin/env bash to /bin/bash worked for me. But to made it work, I had to change my default terminal shell to bash instead of zsh and I had been using it with zsh and it used to work fine. Also, running /usr/bin/evn bash works fine in my zsh terminal.Pothook
So it wasn't working properly, like pyenv --version worked but pyenv ls did not work, I did $ which bash and then seeing the output, I did ls -l /usr/local/bin/bash which showed me a symbolic link /usr/local/bin/bash -> /bin/zsh should this link be here? Also making the change in the above comment made some commands work and some not, what kind of behaviour is this?Pothook
@Pothook For me, this looks like some broken bash command, e.g. some wrong symlinks? That said, pyenv should work fine (at least not choke on shopt) when you invoke it directly with /bin/bash /usr/local/bin/pyenv.Government
I THINK it will work if I somehow break that link but I am also scared of the repercussions as it might break many other things. Is there a solution besides reinstalling OS?Pothook
G
1

According to your info and comments it seems your issue has 3 causes working hand in hand:

  1. pyenv is a bash (and bash-only) script with a shebang line #!/usr/bin/env bash
  2. /usr/local/bin comes before /usr/bin or /bin in your PATH, so executables therein are picked up first by /usr/bin/env (desired behaviour, especially when using homebrew)
  3. /usr/local/bin/bash is symlinked to /bin/zsh!?!

So in the end you are running pyenv with zsh, which, albeit being a close replacement for bash, doesn't know about shopt and therefore chokes. I don't know why the symlink is in place, but it shouldn't, because zsh is not a fully compatible drop-in replacement for bash.

I'd suggest to

  1. (in case you are using homebrew)
    Check if you have/had bash installed through homebrew (which was later somehow replaced by a symlink to zsh):

    # shows only top-level packages (directly installed)
    brew leaves        
    
    # shows *all* packages with dependency tree
    brew deps --tree --installed
    

    And uninstall bash if not required anymore (which should then remove /usr/local/bin/bash).

  2. Either delete or at least rename the culprit:

    mv /usr/local/bin/bash /usr/local/bin/bash_link_to_zsh

    Or, if some program requires /usr/local/bin/bash to be in place, just have it point to /bin/bash.

Government answered 11/5, 2020 at 21:45 Comment(1)
According to your observation#2 from comments, I re arranged my PATH and now it works fine thanks.Pothook

© 2022 - 2024 — McMap. All rights reserved.