45

I want to use Python3 with pyenv.

$ pyenv root
/Users/asari/.pyenv
$ pyenv versions
  system
  2.7.15
  3.6.2
  3.6.3
  3.6.4
* 3.6.6 (set by /Users/asari/workspace/hoge/.python-version)
$ python -V
pyenv: python: command not found

The `python' command exists in these Python versions:
  2.7.15

but, python command not found.

I read it in .pyenv/shims/python, thought that there was not python in .pyenv/versions/3.6.6/bin/, but I did not know why python was missing.

$ pwd
/Users/asari/.pyenv/versions/3.6.6/bin
$ ls -la
total 12096
drwxr-xr-x  19 asari  staff      608  8 16 00:51 .
drwxr-xr-x   6 asari  staff      192  8 16 00:51 ..
lrwxr-xr-x   1 asari  staff        8  8 16 00:51 2to3 -> 2to3-3.6
-rwxr-xr-x   1 asari  staff      135  8 16 00:51 2to3-3.6
-rwxr-xr-x   1 asari  staff      276  8 16 00:51 easy_install-3.6
lrwxr-xr-x   1 asari  staff        7  8 16 00:51 idle3 -> idle3.6
-rwxr-xr-x   1 asari  staff      133  8 16 00:51 idle3.6
-rwxr-xr-x   1 asari  staff      258  8 16 00:51 pip3
-rwxr-xr-x   1 asari  staff      258  8 16 00:51 pip3.6
lrwxr-xr-x   1 asari  staff        8  8 16 00:51 pydoc3 -> pydoc3.6
-rwxr-xr-x   1 asari  staff      118  8 16 00:51 pydoc3.6
lrwxr-xr-x   1 asari  staff        9  8 16 00:51 python3 -> python3.6
lrwxr-xr-x   1 asari  staff       16  8 16 00:51 python3-config -> python3.6-config
-rwxr-xr-x   2 asari  staff  3078944  8 16 00:51 python3.6
lrwxr-xr-x   1 asari  staff       17  8 16 00:51 python3.6-config -> python3.6m-config
-rwxr-xr-x   2 asari  staff  3078944  8 16 00:51 python3.6m
-rwxr-xr-x   1 asari  staff     2076  8 16 00:51 python3.6m-config
lrwxr-xr-x   1 asari  staff       10  8 16 00:51 pyvenv -> pyvenv-3.6
-rwxr-xr-x   1 asari  staff      475  8 16 00:51 pyvenv-3.6

$PATH

$ echo $PATH | perl -p -e 's/:/\n/g'
/Users/asari/.pyenv/shims
/Users/asari/.pyenv/bin
/Users/asari/.rbenv/shims
/Users/asari/.cargo/bin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

install log

$ pyenv install 3.6.6
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.6.6.tar.xz...
-> https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz
Installing Python-3.6.6...
python-build: use readline from homebrew
Installed Python-3.6.6 to /Users/asari/.pyenv/versions/3.6.6
$ pyenv --version
pyenv 1.2.7
$ brew list | grep py
python
python@2

pyenv clone and installed from github(I have not installed pyenv on brew)

.zshrc

# python
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

Thank you for your time.

UPDATE

I created python 's symlink, and python worked.

Why is there no python s symlink? (I was wondering if install failed, I am running install and uninstall many times

create symlink

$ pwd
/Users/asari/.pyenv/versions/3.6.6/bin
$ ln -s python3 python

Work,

$ pwd
/Users/asari/workspace/hoge
$ python -V
Python 3.6.6
asari-mtr
  • 529
  • 1
  • 5
  • 10

13 Answers13

35

Added to ~/.bashrc

alias python="$(pyenv which python)"
alias pip="$(pyenv which pip)"
4lk4tr43
  • 542
  • 4
  • 7
  • Works, but it seems you need to run this again if you activate a different pyenv? – Juan A. Navarro Mar 25 '22 at 12:01
  • 11
    Using these kind of aliases can cause unforseen problems. E.g. it will prevent a virtual environment to be activated incorrectly when you e.g. run `pipenv shell`. Instead simply ensure that the shims for `python` and `pip` are in your path - by ensuring that `eval "$(pyenv init --path)"` is being executed in your `.bash_profile` or `.zprofile`. – abulka Mar 29 '22 at 03:17
  • 2
    This is indeed problematic. @arno_v's answer - basically read the docs - should be the accepted and most voted one! – stephendwolff Jul 31 '22 at 16:05
19

Under mac OS 10.15

We add the following to .bashrc file or .zshrc file

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

if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
Jason FB
  • 4,752
  • 3
  • 38
  • 69
zangw
  • 43,869
  • 19
  • 177
  • 214
15

For me the config in my .zshrc.local file needed an update. Using the info on configure your shell's environment for Pyenv page I changed the pyenv init stuff to this:

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"

eval "$(pyenv init --path)"
eval "$(pyenv init -)"

# Optionally (when you use pyenv virtualenv)
eval "$(pyenv virtualenv-init -)"
arno_v
  • 18,410
  • 3
  • 29
  • 34
  • Curious why `source ~/.bashrc` the bash config file inside zsh config? Are you using `.zshrc.local` as a common config for both shells? Seems very specific to your setup and could cause issues on other people's systems e.g if they have bash specific syntax in a `~/.bashrc` file on their system. The `virtualenv-init` part also relates to a plugin that others might not use. – Davos Feb 07 '23 at 09:33
  • 1
    Very good questions. It seems the source `.bashrc` is not even used on my systems (I copy pasted it from the pyenv docs, but I see they've updated that in the meantime as well). I'll update the answer a bit :) – arno_v Feb 07 '23 at 09:50
  • @asari-mtr you should mark this as the correct answer. My earlier comment no longer applies. – Davos Feb 07 '23 at 09:53
7

on MAC OS. I solved it by adding to below lines to the ~/.bash_profile

At the terminal call vi ~/.bash_profile

Insert 2 below lines

alias python="$(pyenv which python)"
alias pip="$(pyenv which pip)"

Call this command after saving above file source ~/.bash_profile

Phat Tran
  • 3,404
  • 1
  • 19
  • 22
  • This is a copy of the incorrect answer posted by @4lk4tr43 4 months earlier, just with a different config file. It is not a good solution as commented there. The problem is you don't have the shims directory in your path, e.g. this: `export PATH="$PYENV_ROOT/shims:$PATH"`. Proof: set your global pyenv `pyenv global 3.11` then set this alias `alias python="$(pyenv which python)"` now check where `python` points to `which python`, now change global version `pyenv global 3.9` and check your alias `which python`. Notice it is still 3.11? These aliases just lead to confusion. – Davos Feb 07 '23 at 09:46
5

On MacOS Montery 12.5 there is no longer a python binary in /usr/local/bin/. A quick fix is to ensure that is a default ref that points to the system python3.

ln -s python3 /usr/local/bin/python
cmcginty
  • 113,384
  • 42
  • 163
  • 163
4

add below 2 lines to ./zshrc IS THE ANSWER, GIVEN THAT you already have eval "$(pyenv init -)"

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

Trick is to let shims play its deserving part.

3

when I use python with pyenv, show me:

/Users/zhezhezhu/.pyenv/shims/python: line 21: /usr/local/Cellar/pyenv/1.2.18/libexec/pyenv: No such file or directory

I solved it by : pyenv rehash

chaojie
  • 31
  • 1
  • 5
3

I was experiencing the same error. I just had to follow the error message pyenv was saying:

Note: See 'pyenv help global' for tips on allowing both...

Which made me run the following command:

pyenv global 2.7.18 3.10.6

which are versions existing in my pyenv versions

Richard Domingo
  • 379
  • 1
  • 19
2

I was using Ubuntu 20.04 and it seemed like I had wrongly setup the commands in my ~/.bashrc. I followed configure your shell's environment for Pyenv:

If your ~/.profile sources ~/.bashrc (Debian, Ubuntu, Mint):

# the sed invocation inserts the lines at the start of the file
# after any initial comment lines
sed -Ei -e '/^([^#]|$)/ {a \
export PYENV_ROOT="$HOME/.pyenv"
a \
export PATH="$PYENV_ROOT/bin:$PATH"
a \
' -e ':a' -e '$!{n;ba};}' ~/.profile
echo 'eval "$(pyenv init --path)"' >>~/.profile

echo 'eval "$(pyenv init -)"' >> ~/.bashrc

If you are using MacOs, Fedora, or CentOS, please follow the link above. The commands may also change in the future, depending on pyenv/os/distributions udpates.

Iman Mirzadeh
  • 12,710
  • 2
  • 40
  • 44
2

In my case shims were initialized correctly:

$ which python
/home/me/.pyenv/shims/python

Yet, similar to richard-domingo's answer, I was seeing:

$ pyenv which python
pyenv: python: command not found

The `python' command exists in these Python versions:
  3.8.13

Note: See 'pyenv help global' for tips on allowing both
      python2 and python3 to be found.

The problem here was that the active python version was system, and my system python3 did not have a python alias (symlink).

Two options to fix this:

  • either create a python symlink for the system's python3, similar to cmcginty's answer, for example:

    ln -s python3 /usr/bin/python
    
  • or explicitly set the python version to one of the versions listed in the error message, using e.g. pyenv shell, pyenv local, or pyenv global, depending on your use-case. This is explained in the documentation and discussed here and here.

    In this case

    pyenv local 3.8.13
    

    whould create/modify a .python-version file in the current directory.

It is really worth reading the following section in the docs: Understanding Python version selection.

djvg
  • 11,722
  • 5
  • 72
  • 103
2

I had previously installed pyenv and then installed my required python distribution as follows:

pyenv install 3.7.10

To remove subsequent pyenv: python :command not found errors -- I first had to run:

pyenv global 3.7.10

That solved my problem.

Grant Shannon
  • 4,709
  • 1
  • 46
  • 36
1

Using Ubuntu 18.04 environment, using pyenv 2.2.5 here is what you need to update the .bashrc:

export PATH="~/.pyenv/bin:$PATH"
if ! command -v pyenv &> /dev/null
then # If no pyenv is found 
    echo "pyenv could not be found, cannot initialize env"
else
    eval "$(pyenv init --path)"
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"
fi

If you are using some other OS, then it may be that you need to add this to another dotfile such as .bash_profile. For more detailed info, check out the docs: https://github.com/pyenv/pyenv#basic-github-checkout

  • This answer worked for me on MacOS 12. I had to add `eval "$(pyenv init --path)"` for it to work. Just `eval "$(pyenv init -)"` wasn't enough. – Alex May 03 '22 at 01:24
0

I solved it.

I used the following grep option in .zshrc

export GREP_OPTIONS = '- color = auto'

It seems that even if ANSI escape code was included in the search result of grep used in pyenv, it was not used properly as a character string.

I think that you all know, but GREP_OPTIONS is deprecated.

asari-mtr
  • 529
  • 1
  • 5
  • 10