Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print version of a module without importing the entire package

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

When a package is imported like...

import pandas as pd

I use:

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

to print the version number.

How do I check the version number if only a module is imported, like

import matplotlib.pyplot as plt

or

from sklearn.metrics import confusion_matrix

Any suggestions?

like image 535
Rene Avatar asked Aug 06 '17 09:08

Rene


1 Answers

I usually do this:

import matplotlib.pyplot as plt
import sys

print (sys.modules[plt.__package__].__version__)

if you import just a 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 this doesn't work you could just import pip and for loop all the modules.

like image 71
Liam Avatar answered Nov 14 '22 20:11

Liam