18

I have a new MacBook with fresh installs of everything which I upgraded to macOS Catalina. I installed homebrew and then pyenv, and installed Python 3.8.0 using pyenv. All these things seemed to work properly.

However, neither pyenv local nor pyenv global seem to take effect. Here are all the details of what I'm seeing:

thewizard@Special-MacBook-Pro ~ % pyenv versions
   system
 * 3.8.0 (set by /Usersthewizard/.python-version)
thewizard@Special-MacBook-Pro ~ % python --version
 Python 2.7.16
thewizard@Special-MacBook-Pro ~ % pyenv global 3.8.0
thewizard@Special-MacBook-Pro ~ % python --version
 Python 2.7.16
thewizard@Special-MacBook-Pro ~ % pyenv local 3.8.0
thewizard@Special-MacBook-Pro ~ % python --version
 Python 2.7.16
thewizard@Special-MacBook-Pro ~ % echo $PATH
 /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/thewizard/.pyenv/bin
thewizard@Special-MacBook-Pro ~ % cat ~/.zshenv
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
   eval "$(pyenv init -)"
fi

BTW there is no /bin in my .pyenv, I only added those commands per some other instructions but I'm planning to remove it because I think it is wrong:

thewizard@Special-MacBook-Pro ~ % ls -al ~/.pyenv 
total 8
drwxr-xr-x   5 thewizard  staff  160 Nov  2 15:03 .
drwxr-xr-x+ 22 thewizard  staff  704 Nov  2 15:36 ..
drwxr-xr-x  22 thewizard  staff  704 Nov  2 15:03 shims
-rw-r--r--   1 thewizard  staff    6 Nov  2 15:36 version
drwxr-xr-x   3 thewizard  staff   96 Nov  2 15:01 versions

It's worth noting that Catalina moved to zsh from bash, not sure if that's relevant here.

Stephen
  • 8,508
  • 12
  • 56
  • 96
  • When looking at your `$PATH`, it seems the system directories `/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin` all have precedence over your pyenv directory. If your system has Python 2.7 in `/usr/local/bin`, running `python` will use Python 2.7 in that case. Can you set the path manually with `export PATH="/Users/thewizard/.pyenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"` and re-try? – Pierre Nov 02 '19 at 21:00
  • I suspect you have another dotenv file being sourced after `.zshenv`, which overwrites your path. Does your `.zshrc` overwrite the `$PATH`? – Pierre Nov 02 '19 at 21:04
  • @Pierre yes I can do that and you might be on to something - but as I mentioned there is no /bin in my .pyenv, so I think it might be fruitless. The only bin I see is in `~/.pyenv/versions/3.8.0/` As for `.zshrc` that file doesn't exist. I don't have other `zsh` files in my home directory, and in fact `.zshenv` is a file I made myself. – Stephen Nov 02 '19 at 21:43
  • I'm not sure it would be in the spirit of StackOverflow, but would you be open to just re-installing pyenv using the [official pyenv installer](https://github.com/pyenv/pyenv-installer) rather than Brew? I wrote a [tutorial](https://pwal.ch/posts/2018-03-03-sane-python/) a while ago with all the steps. – Pierre Nov 02 '19 at 21:51
  • Thanks, I will try that! – Stephen Nov 02 '19 at 23:57
  • I was able to get it working just by adding the commands in your tutorial to my `.zshrc` file. Incidentally one of the commands (with `virtualenv-init` in it) gives an error to me, but things seem to work regardless. Thanks for the tip! – Stephen Nov 08 '19 at 19:39
  • I ran into the same issue today. Like Stephen said there is no bin in .pyenv. Putting /Users//.pyenv/versions/3.7.3/bin in first place in the PATH in .zprofile (the new -bash_profile) worked. However, I am also trying to figure out why this is happening. There is no .zshrc in my home folder. – Malte Nov 17 '19 at 03:50

6 Answers6

28

If you're using pyenv with pipenv and encountering the same issue, you can add the following lines to your .zshrc or .zprofile file:

export PYENV_ROOT="$HOME/.pyenv/shims"
export PATH="$PYENV_ROOT:$PATH"
export PIPENV_PYTHON="$PYENV_ROOT/python"

Referencing pyenv's /shims folder helps to keep it more general and to allow you to easily switch between different Python versions, should you have more than one installed.

pipenv will then always reference the version of Python that is currently set as global by pyenv.

martin-martin
  • 3,274
  • 1
  • 33
  • 60
7

I added the following to my ~/.zprofile and got it working.

export PYENV_ROOT="$HOME/.pyenv/versions/3.7.3"
export PATH="$PYENV_ROOT/bin:$PATH"
Malte
  • 337
  • 1
  • 11
  • And below that... . $PYENV_ROOT/bin/virtualenvwrapper.sh instead of . ~/.pyenv/versions/3.7.3/bin/virtualenvwrapper.sh – Malte Nov 17 '19 at 04:03
  • this helped me, thx. it can be made more general like so: https://stackoverflow.com/a/59909981/5717580 – martin-martin Jan 25 '20 at 14:06
1

Also check that venv is not activated.

It may be activated automatically in your IDE console.

flomaster
  • 1,563
  • 16
  • 16
  • 1
    In VS Code, my issue was `$PYENV_VERSION` was set and therefore `pyenv local ...` didn't work. Setting `"python.terminal.activateEnvironment": false,` in the `settings.json` fixed it. – jammygrams Aug 30 '23 at 13:47
0

catalina (and OS X in general) uses /etc/zprofile to set the $PATH in advance of what you're specifying within the local dotfiles.

it uses the path_helper utility to specify the $PATH and i suspect this is overriding the shim injection in your local dotfiles. you can comment out the following lines in /etc/zprofile. this will get overridden in subsequent OS updates.

# if [ -x /usr/libexec/path_helper ]; then
#   eval `/usr/libexec/path_helper -s`
# fi

alternately, and less intrusively, you can unset the GLOBAL_RCS option (add unsetopt GLOBAL_RCS) in your personal .zshenv file which will allow you to suppress sourcing all of the system default RC files for zsh and allow the pyenv shims to operate as intended.

steve ulrich
  • 121
  • 4
0

Check if exist any symbolic links on your account root

ls -al .pyenv/versions/x.x.x/bin

if you don't have symlink files

unset CLICOLOR
unset CLICOLOR_FORCE
unset LSCOLORS
unalias ls

and try python install again with pyenv

maneuling
  • 1
  • 4
0

I think the issue is due to the default HD partitions that might be causing confusion. "With macOS Catalina, you can no longer store files or data in the read-only system volume, nor can you write to the "root" directory ( / ) from the command line, such as with Terminal" (https://support.apple.com/en-ca/HT210650). I had the same issues on macbook pro and imac, which forced me to perform factory resets. I've given up on pyenv and decided to go with Anaconda to manage python versions.

Bo Louie
  • 31
  • 1
  • 10