labunix's blog

labunixのラボUnix

debian bookwormのvenvでaws CloudShellと同等のPython環境を準備する

■debian bookwormのvenvでaws CloudShellと同等のPython環境を準備する
 今回は開発と本番のプロジェクトに分けて、プロジェクト単位で仮想環境(venv)を使用する

 システムのPython環境は変えたくない
 バージョン違いやインストールモジュールの異なるPython環境を使いたい
 プロジェクト単位でライブラリを管理したい

■pipについて

$ cat /etc/*release | awk '/PRETTY_NAME/'
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"

$ dpkg -l | awk '$1 ~ /ii/ && $2 ~ /python3-pip/{print $2,$3,$4}'
python3-pip 23.0.1+dfsg-1 all

$ apt-cache search python3-pip
python3-pip - Python package installer
python3-pip-whl - Python package installer (pip wheel)
python3-pipdeptree - display dependency tree of the installed Python 3 packages

$ cat /etc/*release | awk '/PRETTY_NAME/'
PRETTY_NAME="Amazon Linux 2023"

$ rpm -qa | grep pip
python3-pip-wheel-21.3.1-2.amzn2023.0.7.noarch
python3-pip-21.3.1-2.amzn2023.0.7.noarch
libpipeline-1.5.3-2.amzn2023.0.2.x86_64

$ yum search python3-pip | grep -v '='
Last metadata expiration check: 0:07:53 ago on Sat 23 Mar 2024 09:32:16 AM UTC.
python3-pip.noarch : A tool for installing and managing Python3 packages
python3-pip-wheel.noarch : The pip wheel

■deb/rpmパッケージのバージョンのため、両環境共に最新版ではない

$ pip list --outdated | awk '/^Package|^pip/'
Package            Version   Latest    Type
pip                23.0.1    24.0      wheel

$ pip --version
pip 23.0.1 from /usr/lib/python3/dist-packages/pip (python 3.11)

$ pip list --outdated | awk '/^Package|^pip/'
Package                   Version         Latest         Type
pip                       21.3.1          24.0           wheel

$ pip --version
pip 21.3.1 from /usr/lib/python3.9/site-packages/pip (python 3.9)

■バージョンを統一するための現状取得

$ pip freeze > requirements.txt.deb
$ pip freeze > requirements.txt.rpm

■仮想環境ディレクトリ名は「venv」で統一、その上位をプロジェクト名のディレクトリとする

$ mkdir cloudshell && cd cloudshell

$ python3 -m venv venv
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt-get install python3-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: ['/home/labunix/cloudshell/venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']


■「python3-venv」パッケージを入れた後にやってねというエラーなので

$ sudo apt-get install -y python3-venv

$ python3 -m venv venv

■有効化と無効化

$ source venv/bin/activate
(venv) $ deactivate

■不要になったら、プロジェクトディレクトリ配下を削除する

$ ls -d ../cloudshell/venv/
../cloudshell/venv/

■(脱線)せっかくなので勉強用のプロジェクト環境を作成
 ホストと異なるモジュールの無いクリーンな環境

$ mkdir study && cd study
$ python3 -m venv venv
$ cp ../requirements.txt.deb requirements.txt.deb
$ cp requirements.txt.deb{,.org}

$ source venv/bin/activate
$ pip freeze > requirements.txt.study
$ du requirements.txt.study 
0	requirements.txt.study
$ pip freeze

■開発用のプロジェクトを作ってコピーする

$ cd ../
$ mkdir cloudshell-dev
$ cp -r cloudshell/venv/ cloudshell-dev/

■開発側を有効化、cloudshellのrequirements.txtを使用する

$ cd cloudshell-dev/
$ source venv/bin/activate
$ cp ../requirements.txt.rpm .
$ cp requirements.txt.rpm{,.org}
$ pip install -r requirements.txt.rpm 

■エラーを参考にバージョン指定を外したり、必要なパッケージを導入したりする対処を行う

 「_」や大文字小文字の差を無視すると、バージョン指定を削除したモジュールのバージョンが異なること、
 コメントアウトした「rpm」が無いことが環境差異となることが分かる

$ sudo apt-get install -y libgpgme-dev
$ sudo apt-get install -y swig

$ sdiff -s requirements.txt.rpm{,.org}
gpg							      |	gpg==1.15.1
libcomps						      |	libcomps==0.1.18
#rpm==4.16.1.3						      |	rpm==4.16.1.3

(venv) $ pip freeze > requirements.txt.cloudshell-dev
(venv) $ sdiff -s requirements.txt.cloudshell-dev requirements.txt.rpm.org 
aws-lambda-builders==1.45.0				      <
							      >	aws_lambda_builders==1.45.0
flask==3.0.2						      |	Flask==3.0.2
gpg==1.10.0						      |	gpg==1.15.1
libcomps==0.1.21.post1					      |	libcomps==0.1.18
pydantic-core==2.16.2					      |	pydantic_core==2.16.2
pygments==2.17.2					      |	Pygments==2.17.2
							      >	rpm==4.16.1.3
typing-extensions==4.9.0				      |	typing_extensions==4.9.0
werkzeug==3.0.1						      |	Werkzeug==3.0.1

(venv) $ deactivate 

■開発側で行った作業を本番側に同じ手順で適用する
 ※今度はcloudshellと同じにするのではなく、開発側と本番側を同一にする

$ cd ../cloudshell
$ cp ../cloudshell-dev/requirements.txt.cloudshell-dev .
$ source venv/bin/activate
(venv) $ pip install -r requirements.txt.cloudshell-dev
(venv) $ pip freeze > requirements.txt.cloudshell
(venv) $ sdiff -s requirements.txt.cloudshell{,-dev};echo $?
0

(venv) $ deactivate 


■本番の仮想環境とオリジナルのaws CloudShellとの差異を確認、同一ではないものの同等にはなった
 gpg==1.15.11.10.0
 libcomps==0.1.180.1.21.post1
 rpm==4.16.1.3 → なし

$ sdiff -i -s requirements.txt.cloudshell ../cloudshell-dev/requirements.txt.rpm.org 
aws-lambda-builders==1.45.0				      <
							      >	aws_lambda_builders==1.45.0
gpg==1.10.0						      |	gpg==1.15.1
libcomps==0.1.21.post1					      |	libcomps==0.1.18
pydantic-core==2.16.2					      |	pydantic_core==2.16.2
							      >	rpm==4.16.1.3
typing-extensions==4.9.0				      |	typing_extensions==4.9.0