17

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
  • 249
  • 1
  • 3
  • 8

9 Answers9

20

Added to ~/.bashrc

alias python="$(pyenv which python)"
alias pip="$(pyenv which pip)"
4lk4tr43
  • 376
  • 2
  • 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
  • 3
    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
4

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
  • 2,652
  • 17
  • 20
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

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)"
if [ -n "$PS1" -a -n "$BASH_VERSION" ]; then source ~/.bashrc; fi

eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
arno_v
  • 15,124
  • 3
  • 26
  • 31
3

Under mac OS 10.15

We add the following to .bashrc file

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

if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
zangw
  • 37,361
  • 17
  • 142
  • 172
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
  • 11,510
  • 1
  • 38
  • 42
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
1

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.

  • This is probably the most useful answer to this whole matter of Python environment management. – Kambiz Jun 04 '22 at 06:00
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
  • 249
  • 1
  • 3
  • 8