[PYTHON] system-wide pyenv

Prerequisites

procedure

Below, execute all as root.

#Include the packages needed to build python
# https://github.com/pyenv/pyenv/wiki#suggested-build-environment
yum install gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel

# install pyenv
mkdir -p /usr/local/opt
git clone https://github.com/pyenv/pyenv.git /usr/local/opt/pyenv

#Version specification
cd /usr/local/opt/pyenv
git log --oneline --decorate --tags
#The tag that comes to the top is the latest
git checkout v1.2.18

#When you check which version later, try again
# git log --oneline --decorate --tags
#To see which tag the HEAD matches

cat << 'EOF' > /etc/profile.d/pyenv.sh
export PYENV_ROOT="/usr/local/opt/pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
EOF

source /etc/profile.d/pyenv.sh

#Put the required python
pyenv install 3.6.10

If you want to use it via sudo, set sudo in one of the following two patterns.

--Add the required path to pyenv to secure_path --Disable secure_path to make PATH transparent

Either is fine, but choose the one that suits your existing sudoers settings. I wish I could use + = for secure_path ...

#pattern 1: `secure_path`Add the required path to pyenv

visudo
# secure_Add the following two to path
# - /usr/local/opt/pyenv/shims
# - /usr/local/opt/pyenv/bin

#Change before
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

#After change
Defaults    secure_path = /usr/local/opt/pyenv/shims:/usr/local/opt/pyenv/bin:/sbin:/bin:/usr/sbin:/usr/bin
#pattern 1: `secure_path`Add the required path to pyenv(cont'd)

cat << 'EOF' > /etc/sudoers.d/pyenv
Defaults env_keep += "PYENV_ROOT PYENV_VERSION"
EOF
#Pattern 2: `secure_path`To make the PATH transparent

cat << 'EOF' > /etc/sudoers.d/pyenv
Defaults !secure_path
Defaults env_keep += "PATH"

Defaults env_keep += "PYENV_ROOT PYENV_VERSION"
EOF
# (Optional)Passwordless sudo settings
#Allow sudo to execute pyenv related commands without passwords for specific user groups
#pattern 1,2 Valid in either case
#The following is an example when using the pyenv group

cat << 'EOF' > /etc/sudoers.d/pyenv_nopasswd
Cmnd_Alias PYENV = /usr/local/opt/pyenv/bin/
%pyenv ALL = (ALL) NOPASSWD: PYENV
EOF

Note that if sudoers matches multiple conditions, the "last one" will take precedence. https://askubuntu.com/questions/100051/why-is-sudoers-nopasswd-option-not-working/100112#100112

visudo
#Close as it is without changing anything
#If you do this/etc/sudoers.d/*Properly corrects the permission of

misc

Error displayed when logging in

pyenv: cannot rehash: /usr/local/opt/pyenv/shims isn't writable

This is the ʻeval "$ (pyenv init-)" in /etc/profile.d/pyenv.shwhere the logged-in user writes to/ usr / local / opt / pyenv / shimsI don't have the authority. It's harmless, so just ignore it, or redirect to/ dev / null` if you're curious and drop it.

/etc/profile.d/pyenv.sh


export PYENV_ROOT="/usr/local/opt/pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)" > /dev/null 2>&1

Put poetry in python 3.4.0

Since pip is too old and pip install poetry is moss as it is, first upgrade pip.

PYENV_VERSION=3.4.0 pip install --upgrade "pip<19.2"
PYENV_VERSION=3.4.0 pip install poetry

If you accidentally do pip install --upgrade pip without specifying the version and a pip that does not work in 3.4.0 is included, you can recover it by following the steps below.

pyenv shell 3.4.0
rm -Rf /usr/local/opt/pyenv/versions/3.4.0/lib/python3.4/site-packages/pip*
python -m ensurepip
python -m pip uninstall pip
python -m ensurepip

#Return pyenv shell
unset PYTHON_VERSION

Manually remove it and re-install the default pip with ensure pip, then use the pip command again to uninstall itself and then use ensure pip to restore it. The first ensure pip does not return you to being able to hit the pip command directly.

Recommended Posts

system-wide pyenv
pyenv note
pyenv memo
pyenv summary
Install pyenv and rbenv on CentOS system-wide
pyenv + virtualenv + Anaconda
pyenv installation notes
pyenv for linux
pyenv + anaconda + python3
pyenv cheat sheet
pyenv + pyenv-virtualenv (CentOS7)