If you mean from the command line, either pip show <module>
or pip freeze
.
If you mean from code, I don't know if there is a standardized way to achieve this for all modules. Probably not, as @definitely_not_me has already explained well. Depending on where (and if) the version is specified, you can try one of these:
import <module>
<module>.__version__
or
from <module> import __version__
from <module> import version
if these don't work, you probably will need to investigate where the version is defined and try to read it.
It looks like there is a new module since Python 3.8, called importlib.metadata:
from importlib.metadata import version
version('<module>')
For Python versions < 3.8 it looks like you can achieve something very similar to that by using pkg_resources
(distributed with setuptools
) but you have to use the package name, not the module name (for example "pyyaml" instead of "yaml"):
import pkg_resources
pkg_resources.get_distribution('<package>').version