LoginSignup
1
1

More than 1 year has passed since last update.

CentOSでPython3.7環境構築

Last updated at Posted at 2020-12-10

まえがき

LinuxもPythonも初心者ですがCentOSでPython3.7の環境構築してみました。
WindowsでPythonの環境はあるのですが、別のPCでも環境作るか~と思ったのがきっかけです。
どっちもWindowsじゃ面白くないので、仮想で建てたCentOSにブチ込んでみようと思った次第です。

環境

基盤となるホストの環境と、これから構築予定のゲストの環境です。

ホスト環境
  • OS:Windows10 Home 64bit
  • RAM:16GB
  • 仮想化ソフト:VMware WorkStation 15 Player
ゲスト(仮想)環境
  • OS:CentOS 8.3
  • RAM:4GB(可変)
  • 開発言語:Python 3.7

手順

OSのisoファイルを入手

何はともあれ、まずはOSイメージの入手から。
リンクはこちら(「CentOS-8.3.2011-x86_64-dvd1.iso」ってやつ)。

仮想マシンを作成

VMware WorkStationで、以下の通りに進める。

  1. 「新規仮想マシンの作成」をクリック
  2. 「インストール元」で「インストーラ ディスクイメージ ファイル」を選択し、さっきダウンロードしたisoを指定。
  3. ウィザードに従ってポチポチやって仮想マシンを作成。
  4. 無事インストール完了。

image.png

どうでもいいですが、パスワードに"@"を使ったらキーボードの配列の関係で入力できなくて詰んだかと思った。
[ Shift ] + [ 2 ] で [ @ ]

Python3.7 のインストール

作成した仮想マシンでターミナルを起動し、以下の通りに進める。
インストールは公式ドキュメントを参照。

1. ログインユーザーをwheelグループに追加

これをしないとsudoでコマンドを実行できない。詳細はこちらを参照されたし。
実行コマンドは以下の通り。

rootユーザーに切り替え
$ su -

一般ユーザー「username」を「wheel」グループに追加
# usermod -G wheel username

追加されたことを確認
# id username
> uid=1000(username)..., 10(wheel)

一般ユーザーに戻ってsudoでコマンドを実行
(一度コンピュータにログインし直してから実行)
$ sudo su -
> Last login: Thu Dec 10 04:07:47 PST 2020 on pts/0

2.Python3.7のインストール

以下の手順でコマンドを実行する。
※一般ユーザーでログインしているのでイチイチsudoが必要。

ビルドツールのインストール
$ sudo yum groupinstall "development tools"
> ...
> Complete!

ライブラリのインストール
$ sudo yum install\
  bzip2-devel gdbm-devel libffi-devel libuuid-devel\
  ncurses-devel openssl-devel readline-devel sqlite-devel\
  tk-devel wget xz-devel zlib-devel
> Package wget-1.19.5-10.el8.x86_64 is already installed.
> ...
> Conplete!

Python用のディレクトリをルート直下に作成
$ sudo mkdir /python3.7
$ sudo cd /python3.7

ソースコードをダウンロード
$ sudo wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tar.xz

ソースコードを解凍
$ sudo tar xJf Python-3.7.9.tar.xz 

ビルドとインストール
$ sudo cd /python3.7/Python-3.7.9
$ sudo ./configure
> If you want a release build with all stable optimizations active (PGO, etc),
> please run ./configure --enable-optimizations

$ sudo make
> Python build finished successfully!
> The necessary bits to build these optional modules were not found:
> nis                                                            
> To find the necessary bits, look in setup.py in detect_modules() for the module's name.

$ sudo make install
> Successfully installed pip-20.1.1 setuptools-47.1.0

エラー(というか警告)が出ていたので一応メモしておく。

WARNING: The script easy_install-3.7 is installed in '/usr/local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The scripts pip3 and pip3.7 are installed in '/usr/local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

Pythonが起動できることを確認する。

$ python3
Python 3.7.9 (default, Dec 10 2020, 04:44:11) 
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

3.仮想環境の構築

やっぱり公式ドキュメントを参考にしながらやります。
以下の手順でコマンドを実行する。

プロジェクトディレクトリを作成する。
$ mkdir Document/Project01

プロジェクトディレクトリ直下に仮想環境を作成
$ cd Document/Project01
$ python3 -m venv .venv

仮想環境への切り替え
$ sourece .venv/bin/activate

以下の表示に切り替わればOK
(.venv) [kei@localhost Project01]$ 

4.おまけ

せっかくなので、仮想環境にrequestsモジュールをインストールして阿部寛のホームページにアクセスしてみる。
仮想環境に切り替えた状態で、以下のコマンドを実行。

$ vi abehiroshi.py

%---abehiroshi.py---
import requests
print(requests.get("http://abehiroshi.la.coocan.jp").text)
-------------------%

$ python3 abehiroshi.py
> <html>
>   <head>
>     <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
>     <meta name="GENERATOR" content="JustSystems Homepage Builder Version 20.0.6.0 for Windows">
>     <meta http-equiv="Content-Style-Type" content="text/css">
>     <title>阿部寛のホームページ</title>
>   </head>
>   <frameset cols=18,82>
>     <frame src="menu.htm" marginheight="0" marginwidth="0" scrolling="auto" name="left">
>     <frame src="top.htm" marginheight="0" marginwidth="0" scrolling="auto" name="right">
>     <noframes>
>       <body></body>
>     </noframes>
>   </frameset>
> </html>

以上です。阿部寛かっこいいね。

1
1
1

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
1
1