LoginSignup
0
1

More than 3 years have passed since last update.

Python触ってみた(インストール編)

Posted at

はじめに

SEとして一からプログラムを書く現場はほぼ経験したことがなく。。。
今回も前任者が開発したPythonのプログラムを改修してほしいとの依頼が。。。

上記のような状況のSEの方って意外と多くいるのではと思う今日この頃。
今回はPythonを読むのに最低限必要な知識を書き残しておこうと思います。

方針とゴール

  • 今回は以下のサイトでお勉強していきます。
    • https://www.python-izm.com/
    • 参考サイトではPythonをPCに直接インストールしています。
    • PCは綺麗にしておきたいので仮想マシンを使用します。
  • ゴールは「Hello World」相当のものを出力できればと思ってます。

前提

  • 今回もVagrantを使って仮想マシン上にPython環境を用意します。
  • Pythonのバージョンは3系を使用します。
    • Pythonは2系と3系で互換性がありません。
    • プリインストールされているのはPython2です。

Python3をインストール

Python3系を明示的にインストールします。

仮想マシン
# CentOSデフォルトのPythonバージョンを確認
## パス確認
$ which python
/usr/bin/python
# Pythonバージョン:2.7.5
$ /usr/bin/python -V
Python 2.7.5

# Python3をyumからインストール
$ yum -y install python3
~省略~
Installed:
  python3.x86_64 0:3.6.8-13.el7

Dependency Installed:
  python3-libs.x86_64 0:3.6.8-13.el7                      python3-pip.noarch 0:9.0.3-7.el7_7                      python3-setuptools.noarch 0:39.2.0-10.el7

Complete!

# インストールしたPython3のバージョン確認
## パス確認
$ which python3.6
/usr/bin/python3.6
## Pythonバージョン:3.6.8
$ python3.6 -V
Python 3.6.8

# Pythonコマンドを2系から3系に切り替え
## デフォルトのPythonコマンドの参照先確認:Python2
$ ls -l /usr/bin/python
lrwxrwxrwx. 1 root root 7 Jun  1  2019 /usr/bin/python -> python2
## デフォルトのシンボリックリンクを退避
$ sudo mv /usr/bin/python /usr/bin/python_bk
## 参照先がなくなったことを確認
$ ls -l /usr/bin/python
ls: cannot access /usr/bin/python: No such file or directory
## Python3.6のシンボリックリンクを作成
$ sudo ln -s python3.6 /usr/bin/python
## 変更したPythonコマンドの参照先確認:Python3.6
$ ls -l /usr/bin/python
lrwxrwxrwx. 1 root root 9 Sep 25 07:26 /usr/bin/python -> python3.6

# Pythonコマンド切り替え後の確認
## バス確認
$ which python
/usr/bin/python
## Pythonバージョン:3.6.8
$ python -V
Python 3.6.8

# pip(pkg管理システム)コマンドも使用可能
$ pip3 search ansible
ansible-stubs (0.1.dev1)                     - ansible-stubs aids in the development and testing of Ansible roles
~以下略~

Pythonプログラム(.py)を実行

Python3をインストールしたのでプログラムを実行してみます。

仮想マシン
# .pyファイルを作成
$ vi test01.py
$ cat test01.py
print('python-izm')

# Pythonコマンドを使用して作成した.pyファイルを実行
$ python test01.py
python-izm

対話形式でPython実行

プログラムを用意しなくても対形式で実行することもできます。

仮想マシン
# 対話型シェル起動
$ python
Python 3.6.8 (default, Apr  2 2020, 13:34:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
対話型シェル
# .pyファイルに記載したものと同じものを実行
>>> print('python-izm')
python-izm
# 終了する場合はquit()を実行
>>> quit()

おわりに

インストールからプログラムの実行までは他の言語とそんなに遜色なくできました。
対話型シェルがあるのは動作確認などの際に便利そうで期待大です!
次回はソースの基本構文について勉強していこうと思います。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1