-[Python2to3 update method] Installation of Python3 series
$ 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 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
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
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
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.
$ curl -OL https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py
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
$ pip install virtualenv
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
$ 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
$ source sample-env/bin/activate
$ (sample-env) user@MacBook:~$
$ deactivate
Virtualenvwrapper Things that make venv management easier
--Reference: Introduction of Virtualenvwrapper
Install
$ sudo pip install virtualenvwrapper
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
Specify the python to use with --python
. If you use python3, use / usr / local / bin / python3
.
$ mkvirtualenv --python=/usr/local/bin/python sample-env
$ workon
sample-env
activate
$ workon sample-env
deactivate
$ deactivate
$ rmvirtualenv sample-env
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
-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