ほぼ老人のプログラミング日記

定年後の平凡なサラリーマンの趣味の日記

Debian に Python の実行環境を作成する

この記事

tiger62shin.hatenablog.com

で書いた「Excel ファイル→テキストファイル出力」ツールは Linux (Debian) で動かすので、自宅サーバー (未公開) に Python の実行環境を作成します。

LinuxPython の実行環境を作成する手順は多くの方々により公開されていますので、この記事はほぼ私のメモ書きです。

Python の実行環境を作成する方法は様々あると思いますが、ライブラリのバージョン等の依存関係をあまり気にしたくないので Miniconda を使って構築します。

Miniconda のインストール

インストーラーのダウンロード

オフィシャルサイトから、Python3.9 の最新インストール用shファイル (Miniconda3-py39_4.11.0-Linux-x86_64.sh) をダウンロード

user@server:~$ wget https://repo.anaconda.com/miniconda/Miniconda3-py39_4.11.0-Linux-x86_64.sh
--2022-05-09 21:08:01--  https://repo.anaconda.com/miniconda/Miniconda3-py39_4.11.0-Linux-x86_64.sh
repo.anaconda.com (repo.anaconda.com) をDNSに問いあわせています... 104.16.131.3, 104.16.130.3, 2606:4700::6810:8203, ...
repo.anaconda.com (repo.anaconda.com)|104.16.131.3|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 75660608 (72M) [application/x-sh]
`Miniconda3-py39_4.11.0-Linux-x86_64.sh' に保存中

Miniconda3-py39_4.11.0-Linux-x8 100%[=======================================================>]  72.16M  6.22MB/s 時間 9.3s     

2022-05-09 21:08:11 (7.72 MB/s) - `Miniconda3-py39_4.11.0-Linux-x86_64.sh' へ保存完了 [75660608/75660608]

インストーラーの実行

  1. Miniconda3-py37_4.11.0-Linux-x86_64.sh を実行

     user@server:~$ bash Miniconda3-py39_4.11.0-Linux-x86_64.sh
    
     Welcome to Miniconda3 py39_4.11.0
    
     In order to continue the installation process, please review the license
     agreement.
     Please, press ENTER to continue
     >>>
    
  2. "Please, press ENTER to continue" に対し、[Enter] キーを入力して処理を継続する
  3. "End User License Agreement - Miniconda" が表示されるので、全部読んで "yes" と入力して [Enter] キーを入力

     Do you accept the license terms? [yes|no]
     [no] >>> yes
    
  4. インストール確認メッセージが表示されるので [Enter] キーを入力してインストールを実行する

     Miniconda3 will now be installed into this location:
     /home/user/miniconda3
    
       - Press ENTER to confirm the location
       - Press CTRL-C to abort the installation
       - Or specify a different location below
    
     [/home/user/miniconda3] >>>
    
  5. インストールが実行される

     PREFIX=/home/user/miniconda3
     Unpacking payload ...
    
    
     Preparing transaction: done
     Executing transaction: done
     installation finished.
    
  6. "conda init" を実行するかどうか問い合わせがある。"yes" でも "no" でもよいと思うが、自分で初期設定を行うつもりなので "no" とした

     Do you wish the installer to initialize Miniconda3
     by running conda init? [yes|no]
     [no] >>> no
    
  7. インストール完了

     You have chosen to not have conda modify your shell scripts at all.
     To activate conda's base environment in your current shell session:
    
     eval "$(/home/user/miniconda3/bin/conda shell.YOUR_SHELL_NAME hook)" 
    
     To install conda's shell functions for easier access, first activate, then:
    
     conda init
    
     If you'd prefer that conda's base environment not be activated on startup, 
        set the auto_activate_base parameter to false: 
    
     conda config --set auto_activate_base false
    
     Thank you for installing Miniconda3!
    

.bashrc の編集

~/.bashjrc に下記を追加

. ~/miniconda3/etc/profile.d/conda.sh

変更内容を適用する

. ./.bashrc

conda コマンドが実行できるか確認

user@server:~$ conda --version
conda 4.11.0

これで、base の環境は使えるようになっているはず

  1. conda 環境をアクティブにする

     user@server:~$ conda activate
     (base) user@server:~$
    
  2. Python のバージョン確認

     (base) user@server:~$ python --version
     Python 3.9.7
    

自作ツールの実行環境を作成

base の環境をそのまま使おうとも思ったが、学習の意味もかねてツール実行用の仮想環境を作成することにした。
なお、Python は開発環境と同じ 3.9 系とした

user@server:~$ conda create -n tools python=3.9

conda 環境に切り替えてみる

user@server:~$ conda activate tools
(tools) user@server:~$

Python のバージョン確認

(tools) user@server:~$ python --version
Python 3.9.12

仮想環境から抜ける

(tools) user@server:~$ conda deactivate
user@server:~$

パッケージのインストール

今回は「Excel ファイル→テキストファイル出力」ツールの実行環境を作成することが目的なので、openpyxl と jinja2 をインストール

user@server:~$ conda activate tools
(tools) user@server:~$ conda install openpyxl
(tools) user@server:~$ conda install jinja2

確認

(tools) user@server:~$ conda list
# packages in environment at /home/tiger/miniconda3/envs/tools:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main  
_openmp_mutex             4.5                       1_gnu  
ca-certificates           2022.4.26            h06a4308_0  
certifi                   2021.10.8        py39h06a4308_2  
et_xmlfile                1.1.0            py39h06a4308_0  
jinja2                    3.0.3              pyhd3eb1b0_0  
ld_impl_linux-64          2.35.1               h7274673_9  
libffi                    3.3                  he6710b0_2  
libgcc-ng                 9.3.0               h5101ec6_17  
libgomp                   9.3.0               h5101ec6_17  
libstdcxx-ng              9.3.0               hd4cf53a_17  
markupsafe                2.0.1            py39h27cfd23_0  
ncurses                   6.3                  h7f8727e_2  
openpyxl                  3.0.9              pyhd3eb1b0_0  
openssl                   1.1.1n               h7f8727e_0  
pip                       21.2.4           py39h06a4308_0  
python                    3.9.12               h12debd9_0  
readline                  8.1.2                h7f8727e_1  
setuptools                61.2.0           py39h06a4308_0  
sqlite                    3.38.3               hc218d9a_0  
tk                        8.6.11               h1ccaba5_1  
tzdata                    2022a                hda174b7_0  
wheel                     0.37.1             pyhd3eb1b0_0  
xz                        5.2.5                h7f8727e_1  
zlib                      1.2.12               h7f8727e_2  

動作確認

user@server:~/work$ python excel_to_formatted_text.py --excelfile=personal_infomation.xlsx --sheetname=personal_infomation --outputfile={住所1}/{氏名}.txt --templatefile=sample_template.txt
Excel file : personal_infomation.xlsx
Sheet name : personal_infomation
Output file format : {住所1}/{氏名}.txt
Output file encoding : utf8
Output file lineterminator : lf
Template file : sample_template.txt
Template file encoding : utf8
Start row : 1
Start column : 1
Blank skip column : None
連番
氏名
電話番号
メールアドレス
郵便番号
住所1
住所2
/home/user/temp/福岡県/内村佳祐.txt is modified.
/home/user/temp/福岡県/三輪順一.txt is modified.
/home/user/temp/福岡県/正木重樹.txt is modified.
/home/user/temp/福岡県/近藤正次郎.txt is modified.
/home/user/temp/福岡県/中岡治雄.txt is modified.
/home/user/temp/福岡県/川島知里.txt is modified.
/home/user/temp/福岡県/松元夏音.txt is modified.
/home/user/temp/福岡県/横川昌信.txt is modified.
/home/user/temp/福岡県/野中忠広.txt is modified.
/home/user/temp/福岡県/神保和奏.txt is modified.

大丈夫そうですが、ちょっと確認してみます。

user@server:~/work$ ls -l 福岡県
合計 40
-rw-r--r-- 1 tiger tiger 136  5月  9 21:33 横川昌信.txt
-rw-r--r-- 1 tiger tiger 142  5月  9 21:33 近藤正次郎.txt
-rw-r--r-- 1 tiger tiger 122  5月  9 21:33 三輪順一.txt
-rw-r--r-- 1 tiger tiger 121  5月  9 21:33 松元夏音.txt
-rw-r--r-- 1 tiger tiger 117  5月  9 21:33 神保和奏.txt
-rw-r--r-- 1 tiger tiger 124  5月  9 21:33 正木重樹.txt
-rw-r--r-- 1 tiger tiger 136  5月  9 21:33 川島知里.txt
-rw-r--r-- 1 tiger tiger 137  5月  9 21:33 中岡治雄.txt
-rw-r--r-- 1 tiger tiger 118  5月  9 21:33 内村佳祐.txt
-rw-r--r-- 1 tiger tiger 110  5月  9 21:33 野中忠広.txt
user@server:~/work$ cat 福岡県/横川昌信.txt 
氏名 : 横川昌信
電話番号 : 0927830160
郵便番号 : 822-0146
住所 : 福岡県宮若市黒丸3-1-10プレシャス黒丸310

以上です。