Recently, I started to touch python a little for studying machine learning. This time, I will organize about building a python environment on CentOS 7.
The python 3 series is not provided in the CentOS 7 repository, so you need to register the repository separately.
# yum install -y https://centos7.iuscommunity.org/ius-release.rpm
However, since it is a repository that is not normally used, it should be disabled and used only if you want to use it.
# vim /etc/yum.repos.d/ius.repo
[ius]
enabled=1
↓
enabled=0
The latest provided by ius was 3.5.2, so I will use it this time.
# yum clean all
# yum install --enablerepo=ius -y python35u python35u-libs python35u-devel python35u-pip
However, as it is, the path is heading to python, which is included by default, so it points to system 2.
$ python --version
Python 2.7.5
The reason for this is that the alias is pasted as follows.
# ls -l /bin/python
lrwxrwxrwx 1 root root 7 December 17 17:56 /bin/python -> python2
# ls -l /bin/python2
lrwxrwxrwx 1 root root 9 December 17 17:56 /bin/python2 -> python2.7
# ls -l /bin/python3*
-rwxr-xr-x 2 root root 11304 June 28 2016/bin/python3.5
lrwxrwxrwx 1 root root 26 January 1 20:02 /bin/python3.5-config -> /usr/bin/python3.5m-config
-rwxr-xr-x 2 root root 11304 June 28 2016/bin/python3.5m
-rwxr-xr-x 1 root root 173 June 28 2016/bin/python3.5m-config
-rwxr-xr-x 1 root root 3398 June 28 2016/bin/python3.5m-x86_64-config
Note that it is NG to re-paste the symbolic link from python to 3 series. If you replace it, various things using python2 system will not work.
For example, yum can't be used. I get an error like this.
File "/usr/bin/yum", line 30
except KeyboardInterrupt, e:
^
So, when using the 3rd system, try using it as follows for the time being. (Usually, how should I use it? Is it better to use pyenv?)
# ln -s /bin/python3.5 /bin/python3
# python3 --version
Python 3.5.2
Since pip also has a pass to 2.7.
# ln -s /bin/pip3.5 /bin/pip3
# pip3 --version
pip 9.0.1 from /usr/lib/python3.5/site-packages (python 3.5)
This time, for studying, I will use "NumPy" which provides a multidimensional array and "Matplotlib" which can draw graphs and visualize data.
The following packages are also required to import Matplotlib, so install them.
# yum install --enablerepo=ius -y python35u-tkinter
Then install NumPy and Matplotlib.
# pip3 install numpy matplotlib
The preparation is now complete. After that, if you write a script like this and run it, you can output a graph.
sample-graph.py
1 #!/bin/python3
2 import numpy as np
3 import matplotlib.pyplot as plt
4
5 x = np.arange(0,6,0.1)
6 y = np.sin(x)
7
8 plt.plot(x, y)
9 plt.show()
If you are not in a GUI environment such as connecting remotely with SSH, you will get the following error when executing the plt.plot (x, y) part of the above script.
_tkinter.TclError: no display name and no $DISPLAY environment variable
The following measures can be considered.
First, change the python settings.
/usr/lib64/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc
38 backend : tkagg
↓
38 #backend : tkagg
39 backend : agg
In addition to the above modifications, modify the last line of the script "sample-graph.py" as follows.
8 plt.show()
↓
8 plt.savefig('sample-graph.png')
You can now output the specified graph to a file.
If you are connecting remotely with ssh, do you want to operate the machine directly? If the machine is CUI, change it to GUI with the following command and operate it. What's good after that!
# systemctl isolate graphical.target
I think that you have built the python environment necessary for studying. (I took a lot of time to prepare the environment ...)
Recommended Posts