😎
pip installで自動的にrequirements.txtを更新するシェルスクリプト
はじめに
Pythonプロジェクトで開発をしていると、パッケージの依存関係をrequirements.txt
で管理することが一般的です。しかし、pip install
するたびに手動でrequirements.txt
を更新するのは面倒です。今回は、パッケージのインストール時に自動的にrequirements.txt
を更新するシェルスクリプトを紹介します。
スクリプトの機能
-
pip install
実行時に自動的にrequirements.txt
を更新 - 変更内容の差分表示
- エラー時のロールバック機能
- カラー付きの進捗表示
- 仮想環境内でのみ動作
コード
function pip() {
# カラーコードの定義
local GREEN='\033[0;32m'
local YELLOW='\033[0;33m'
local BLUE='\033[0;34m'
local RED='\033[0;31m'
local NC='\033[0m'
# 仮想環境がアクティブでない場合は通常のpipを実行
if [[ -z "$VIRTUAL_ENV" ]]; then
command pip "$@"
return
fi
# インストールまたはアンインストールコマンドかチェック
if [[ "$1" == "install" || "$1" == "uninstall" ]]; then
# バックアップの作成
local venv_dir=$(dirname "$VIRTUAL_ENV")
local req_file="$venv_dir/requirements.txt"
if [[ -f "$req_file" ]]; then
cp "$req_file" "$req_file.bak"
fi
# 元のpipコマンドを実行
echo -e "${BLUE}Running: pip $@${NC}"
command pip "$@"
# コマンドが成功した場合
if [[ $? -eq 0 ]]; then
# requirements.txtを更新
echo -e "${YELLOW}Updating requirements.txt...${NC}"
command pip freeze > "$req_file"
# 更新完了メッセージ
echo -e "${GREEN}Successfully updated requirements.txt${NC}"
# 変更された行数を表示
if [[ -f "$req_file.bak" ]]; then
echo -e "${BLUE}Changes in requirements.txt:${NC}"
diff --color "$req_file.bak" "$req_file" 2>/dev/null || true
rm "$req_file.bak"
fi
else
echo -e "${RED}Failed to execute pip command. requirements.txt not updated.${NC}"
if [[ -f "$req_file.bak" ]]; then
mv "$req_file.bak" "$req_file"
fi
fi
else
# その他のコマンドは通常通り実行
command pip "$@"
fi
}
セットアップ方法
- お使いのシェル設定ファイルに上記のコードを追加します:
# Bashの場合
echo 'source ~/.pip_functions.sh' >> ~/.bashrc
# Zshの場合
echo 'source ~/.pip_functions.sh' >> ~/.zshrc
- シェルを再起動するか、設定を再読み込みします:
# Bashの場合
source ~/.bashrc
# Zshの場合
source ~/.zshrc
使用例
パッケージのインストール
$ pip install requests
Running: pip install requests
Successfully installed requests-2.31.0
Updating requirements.txt...
Successfully updated requirements.txt
Changes in requirements.txt:
+ requests==2.31.0
+ urllib3==2.1.0
パッケージのアンインストール
$ pip uninstall requests
Running: pip uninstall requests
Successfully uninstalled requests-2.31.0
Updating requirements.txt...
Successfully updated requirements.txt
Changes in requirements.txt:
- requests==2.31.0
- urllib3==2.1.0
動作の詳細
-
仮想環境チェック
- 仮想環境内でのみ自動更新機能が動作
- 仮想環境外では通常の
pip
コマンドとして実行
-
バックアップ作成
- 更新前に
requirements.txt
のバックアップを作成 - エラー時に自動でロールバック
- 更新前に
-
差分表示
- 追加/削除されたパッケージを色付きで表示
- 依存関係の変更も確認可能
-
エラーハンドリング
- インストール失敗時は
requirements.txt
を更新しない - バックアップから自動復元
- インストール失敗時は
カスタマイズのヒント
開発用パッケージの分離
開発用パッケージを別ファイルで管理したい場合:
# requirements-dev.txtに開発用パッケージを保存
if [[ "$*" == *"--dev"* ]]; then
command pip freeze > "requirements-dev.txt"
else
command pip freeze > "requirements.txt"
fi
特定パッケージの除外
特定のパッケージをrequirements.txt
から除外:
command pip freeze | grep -v "package-to-exclude" > "$req_file"
注意点
- 仮想環境内でのみ機能します
-
git
やdiff
コマンドがインストールされていない環境では一部機能が制限されます - 複数の
requirements.txt
を使用するプロジェクトでは調整が必要な場合があります
まとめ
このスクリプトを使用することで、パッケージ管理の手間を大幅に削減できます。特に複数人での開発時に、依存関係の管理が容易になります。
必要に応じてカスタマイズし、プロジェクトの要件に合わせて使用してください。
その他の記事紹介
これ以外にも以下のようなPythonの開発に役立つシェルスクリプトを紹介しています。
Discussion