LoginSignup
1
2

More than 5 years have passed since last update.

Pythonスクリプトからインストールされているモジュールをチェックする

Posted at

Pythonスクリプトを非プログラマなどに配布する際、依存モジュールがインストールされているかチェックしたい場合があります。
pipモジュールを使えば、インストール済みのモジュールとバージョンの一覧を取得可能です。
お薦めはしませんが、スクリプトからモジュールをインストールする事も可能です。

# coding: utf-8
import pip

# インストールされているpackageを返す
pip.get_installed_distributions()
"""
[
    'watchdog 0.8.3'
    'toml 0.9.1'
    'sympy 1.0'
    'Sphinx 1.5.2'
    'sphinx-rtd-theme 0.1.9'
    .
    .
    .
]
"""

# スクリプト上からpackage_nameをインストールする
if pip.main(['install', package_name]) == 0:
    print("ok")

なお、配布先の非プログラマが黒い画面(コンソール画面)を立ち上げる知識を有している場合。

動作済みのPC環境から

$ pip freeze > requirements.txt

で出力したファイルを一緒に配布して

$ pip install -r requirements.txt --upgrade

をしてもらうか、バッチファイルを実行してもらうのが一番です。

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