python (pyenv + pyenv-virtualenv) + CentOS7 installation

environment

Related article

-[Python2to3 update method] Installation of Python3 series

Dependency installation

$ sudo yum -y groupinstall "Development Tools"
$ sudo yum -y install readline-devel zlib-devel bzip2-devel sqlite-devel openssl-devel \
    libXext.x86_64 libSM.x86_64 libXrender.x86_64 gcc gcc-c++ libffi-devel python-devel bzip2

Only the following is OK.

$ sudo yum -y install zlib tk-devel tcl-devel ncurses-devel gdbm-devel db4-devel readline-devel zlib-devel \
  bzip2-devel sqlite-devel openssl-devel libXext.x86_64 libSM.x86_64 libXrender.x86_64 gcc gcc-c++ libffi-devel python-devel patch bzip2

If you use matplotlib, install the following first

If you put it in later, it seems that you have to rebuild python.

$ sudo yum install -y tk.x86_64 tk-devel.x86_64 tkinter.x86_64

pyenv install

$ pyenv_install() {
  # skip installation when pyenv is already installed.
  if [ `pyenv --version > /dev/null 2>&1; echo $?` == 0 ]; then
    echo '.pyenv is already installed.(skipping...)'
    return
  fi

  # pyenv
  git clone https://github.com/yyuu/pyenv.git ~/.pyenv;
  echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile;
  echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile;
  echo 'eval "$(pyenv init -)"' >> ~/.bash_profile;
  
  # pyenv-virtualenv
  git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv;
  echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile;

  source ~/.bash_profile;
}

$ pyenv_install

python install

List of installable items

$ pyenv install --list

python2 series

$ pyenv install 2.7.12
$ pyenv global 2.7.12

anaconda2 series

$ pyenv install anaconda2-4.1.0
$ pyenv global anaconda2-4.1.0

python3 series

$ pyenv install 3.5.2
$ pyenv global 3.5.2

anaconda3 series

$ pyenv install anaconda3-4.1.0
$ pyenv global anaconda3-4.1.0

Check the installed version

$ pyenv versions

pyenv-virtualenv

It is already built in the pyenv_install function mentioned above, but if you want to use pyenv-virtualenv and separate the pips to be managed for each application, execute the following

# pyenv-virtualenv
git clone https://github.com/yyuu/pyenv-virtualenv.git  ~/.pyenv/plugins/pyenv-virtualenv
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile

Generate env for each application with the following command

$ pyenv virtualenv 3.5.2 application-name

Check if it is installed

$ pyenv versions

  system
* 3.5.2
  3.5.2/envs/application-name
  application-name

switching

$ pyenv local 3.5.2/envs/application-name

When building python from source

Download python

Download from here https://www.python.org/downloads/

Example) When installing Python-2.7.10.

$ curl -OL https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz
$ tar zxfv Python-2.7.10.tgz

Installation

When installing python in / usr / local / bin / Install pip with --with-ensure pip.

$ cd Python-2.7.10
$ sudo ./configure --enable-unicode=ucs4 --prefix=/usr/local --with-ensurepip
$ sudo make
$ sudo make altinstall

Warning make install may overwrite the python3 binary or break the link. Therefore, make altinstall, which installs only exec_prefix / bin / pythonversion instead of make install, is recommended.

pip installation

$ curl -OL https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py

When changing the installation destination of pip

By default, root privileges are required, but if you want to run pip with user privileges, you need to change the installation destination to a location where you can operate with user privileges.

$ mkdir -p ~/local/lib/python/site-packages/
$ echo 'export PYTHONPATH=$HOME/local/lib/python/site-packages:$PYTHONPATH' >> ~/.bash_profile

When doing pip install, specify the path with --install-option.

$ pip install --install-option="--prefix=$HOME/local" awscli

Reference: Specify the installation destination of pip

venv

venv installation

$ pip install virtualenv

Create a venv called samle-env

If you want to use virtualenvwrapper, skip this step here

--Reference: Python development environment for macOS using venv 2016

# Python 3.5 or earlier
$ virtualenv sample-env

# Python 3.6 or later
$ python3 -m venv sample-env

Files related to sample-env are created below

$ ll ~/sample-env/
total 8
drwxr-xr-x  12 user  staff  384 Apr 30 13:50 bin
drwxr-xr-x   2 user  staff   64 Apr 30 13:50 include
drwxr-xr-x   3 user  staff   96 Apr 30 13:50 lib
-rw-r--r--   1 user  staff   75 Apr 30 13:50 pyvenv.cfg

activate Activate the virtual environment

$ source sample-env/bin/activate
$ (sample-env) user@MacBook:~$

deactivate

$ deactivate

Virtualenvwrapper Things that make venv management easier

--Reference: Introduction of Virtualenvwrapper

Install

$ sudo pip install virtualenvwrapper

Added to ~ / .bashrc etc.

Since it cannot be used just by installing it, set the following

Maybe /usr/bin/virtualenvwrapper.sh

if [ -f /usr/local/bin/virtualenvwrapper.sh ]; then
    export WORKON_HOME=$HOME/.virtualenvs
    source /usr/local/bin/virtualenvwrapper.sh
fi

env creation

Specify the python to use with --python. If you use python3, use / usr / local / bin / python3.

$ mkvirtualenv --python=/usr/local/bin/python sample-env

You can see the list of env created by the following command

$ workon
sample-env

activate

$ workon sample-env

deactivate

$ deactivate

env deleted

$ rmvirtualenv sample-env

Other useful libraries

pp

import pprint
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(alarms)

[Reference] [Python 2.7] The standard output of objects containing Japanese as elements can be output as Japanese character strings in print (pp (object)) format / 4a5d6f1f0a23e787bc34)

inspect A function that returns a list of object attributes as a pair of name and content

import inspect
print(inspect.getmembers(alarms))

pip.conf When installing pip.conf, the location differs depending on the OS.

Reference: https://pip.pypa.io/en/stable/user_guide/#configuration

OS path
MacOS $HOME/.pip/pip.conf
Unix $HOME/.pip/pip.conf
Windows %HOME%\pip\pip.ini
Pyenv on Unix-When setting for each virtualenv ~/.pyenv/versions/[venv_name]/pip.conf
Pyenv on Mac-When setting for each virtualenv /usr/local/opt/pyenv/versions/[venv_name]/pip.conf
" virtualenv ~/.virtualenvs/[venv_name]/pip.conf

Dockerfile

From centos:7

# parameters
ARG PY_VER='3.7.1'

# python
WORKDIR /src
RUN curl -L https://www.python.org/ftp/python/${PY_VER}/Python-${PY_VER}.tgz -o /src/Python-${PY_VER}.tgz
RUN tar zxfv Python-${PY_VER}.tgz

RUN yum -y install make zlib tk-devel tcl-devel ncurses-devel gdbm-devel db4-devel readline-devel zlib-devel \
  bzip2-devel sqlite-devel openssl-devel libXext.x86_64 libSM.x86_64 libXrender.x86_64 gcc gcc-c++ libffi-devel python-devel patch bzip2

WORKDIR /src/Python-${PY_VER}
RUN ./configure --enable-unicode=ucs4 --prefix=/usr/local --enable-optimizations
RUN make && make altinstall

RUN ln -s /usr/local/bin/python3 /usr/local/bin/python

# pip
RUN curl -L https://bootstrap.pypa.io/get-pip.py -o /src/get-pip.py
WORKDIR /src
RUN python get-pip.py

reference

-Separate environment for each Python application (pyenv + virtualenv (or venv) + pip) -Python Tips: I want to find out the methods of an object -matplotlib is not drawn

Recommended Posts

python (pyenv + pyenv-virtualenv) + CentOS7 installation
pyenv + pyenv-virtualenv (CentOS7)
Python 2.7 installation (yum) (CentOS 6.8)
Install python (pyenv, pyenv-virtualenv)
Python 3.5 installation (yum) (CentOS 6.8)
Python installation
Python installation
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
Install Python on CentOS using Pyenv
Install Python on CentOS using pyenv
centOS 7 installation error
Installation of CentOS 8
Python2.7 + CentOS7 + OpenCV3
Construction of Python local development environment Part 1 (pyenv, pyenv-virtualenv, pip installation)
PHP installation (CentOS 8)
Python installation (Windows)
pyenv installation notes
pyenv + anaconda + python3
Python installation 2020 (macOS)
Python3.4 installation notes
CentOS8 --Install --Python3
CentOS 7 + ffmpeg installation
Python 3.x environment construction by Pyenv (CentOS, Ubuntu)
python openCV installation (memo)
Install pyenv and pyenv-virtualenv
Python basic course (2 Python installation)
Reinforcement learning 1 Python installation
Python installation method Windows
Install Python3.4 on CentOS 6.6
Installation of Python 3.3 rc1
Introducing Python 2.7 to CentOS 6.6
Install Python 2.7.3 on CentOS 5.4
Install python with pyenv
Installation on CentOS8 VirtualBox
Python version switching (pyenv)
Installation of matplotlib (Python 3.3.2)
Building a Python environment for pyenv, pyenv-virtualenv, Anaconda (Miniconda)
Installation method when using RealSense from Python (pyenv edition)
[Python] Django environment construction (pyenv + pyenv-virtualenv + Anaconda) for macOS
Python CMS Mezzanine installation procedure
How to install Python2.7 python3.5 with pyenv (on RHEL5 CentOS5) (2016 Nov)
[CentOS7] Install anaconda using pyenv
CentOS 8 installation procedure (latest version)
Docker Easy Installation Procedure (CentOS)
Python 3.6 installation procedure [for Windows]
Install Python 3.8 on CentOS 7 (SCL)
Apache installation fails on CentOS 8.2
Python installation and basic grammar
OpenCV3 installation for Python3 @macOS
Python installation (Mac edition) (old)
Fastest Python installation on Windows
Change python version using pyenv
Install Python 3.7 and Django 3.0 (CentOS)
Python 3 series installation for mac
Reinstall python with pyenv with -fPIC
Switch from python2.7 to python3.6 (centos7)
Ruby, Python Module Installation Guide
Install pyenv from Homebrew, install Python from pyenv
Install Python 3.8 on CentOS 8 (AppStream)
python with pyenv and venv
[Python] Anaconda, pyenv, virtualenv, .bash_profile