rasbt / watermark

An IPython magic extension for printing date and time stamps, version numbers, and hardware information
Other
883 stars 90 forks source link

support -iv edge cases? #58

Open jGaboardi opened 4 years ago

jGaboardi commented 4 years ago

I am wondering if there is an efficient way to support fetching the versions of Python packages that don't follow the __version__ convention, gurobi's Python API for example?

In [1]: %load_ext watermark                                                                                                      

In [2]: %watermark -w                                                                                                            
watermark 2.0.2

In [3]: import gurobi                                                                                                            

In [4]: import gurobipy                                                                                                          

In [5]: %watermark -iv                                                                                                           

In [6]: gurobi.gurobi.version()                                                                                                  
Out[6]: (9, 0, 0)

In [7]: gurobipy.gurobi.version()                                                                                                
Out[7]: (9, 0, 0)

In [8]: %watermark                                                                                                               
2019-12-25T17:55:30-05:00

CPython 3.7.3
IPython 7.10.2

compiler   : Clang 9.0.0 (tags/RELEASE_900/final)
system     : Darwin
release    : 19.2.0
machine    : x86_64
processor  : i386
CPU cores  : 4
interpreter: 64bit

Or would this have to be (1) a brute-force search through the entire directory; or (2) a manual print() for the version.

rasbt commented 4 years ago

Good question. In most cases, I think that

pkg_resources.get_distribution(p).version

(https://github.com/rasbt/watermark/blob/master/watermark/watermark.py#L157)

should take care of it. But if pkg_resources can find the version, we have the manual brute-force approach in the lines below. I guess there is not really a better way than brute-forcing these.

The cleanest way, I believe, would be through the metadata package (from the stdlib), but it's Python 3.8-only (#56) so that it's not that useful, yet.