1. ホーム
  2. python

[解決済み] Pythonモジュールのバージョンを確認する方法は?

2022-03-23 09:51:55

質問

Pythonモジュールをインストールしたところです。 constructstatlibsetuptools このように

# Install setuptools to be able to download the following
sudo apt-get install python-setuptools

# Install statlib for lightweight statistical tools
sudo easy_install statlib

# Install construct for packing/unpacking binary data
sudo easy_install construct

そのバージョンを(プログラム的に)チェックできるようにしたい。 に相当するものはありますか? python --version コマンドラインから実行できますか?

私の python のバージョンは 2.7.3 .

解決方法は?

を使うことをお勧めします。 easy_installの代わりにpip . pip を使用すると、インストールされているすべてのパッケージとそのバージョンを次のように一覧表示することができます。

pip freeze

ほとんどの Linux システムでは、これをパイプで grep (または findstr をクリックし、目的のパッケージの行を探します。

Linux:
$ pip freeze | grep lxml
lxml==2.3

Windows:
c:\> pip freeze | findstr lxml
lxml==2.3

個々のモジュールについては __version__ 属性 しかし、これがないモジュールもあります。

$ python -c "import requests; print(requests.__version__)"
2.14.2
$ python -c "import lxml; print(lxml.__version__)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute '__version__'

最後に、ご質問のコマンドは、接頭辞が sudo グローバルなPython環境にインストールされているようです。pythonを調べてみることを強くお勧めします。 仮想環境 マネージャー、例えば virtualenvwrapper