I believe you can use Unity's PackageManager.
First you search for the package you need info of (TMP) with PackageManager.Client (I think it will look something like this: var tmp = Client.Search("com.unity.textmeshpro").Result;, be aware that .Result is synchronous, maybe there's a better way to implement this using Tasks).
Then you get the package's info (version) with PackageManager.PackageInfo (something like tmp.version). Done.