Print version of the module without importing the entire package

Is it possible to check the package version if only the module is imported?

When a package is imported as ...

import pandas as pd 

I use:

 print('pandas : version {}'.format(pd.__version__)) 

to print the version number.

How to check version number if only module is imported, for example

 import matplotlib.pyplot as plt 

or

 from sklearn.metrics import confusion_matrix 

Any suggestions?

+5
source share
1 answer

I usually do this:

 import matplotlib.pyplot as plt import sys print (sys.modules[plt.__package__].__version__) 

if you import only the function:

 from sklearn.metrics import confusion_matrix as function import sys try:module_name = function.__module__[:function.__module__.index(".")] except:module_name = function.__module__ print (sys.modules[module_name].__version__) 

and if that doesn't work, you can just import pip and for code all the modules.

+3
source

Source: https://habr.com/ru/post/1270622/


All Articles