3

I know of

import mpmath as mp
print mp.libmp.BACKEND

but if I'm not mistaken it won't say if I'm using gmpy or gmpy2.

Also, I that every time you use a newer version of something you don't get the version number next to it but since I can import gmpy and gmpy2 separately I'm a bit worried I may be using the older version of gmpy.

Thank you

UPDATE:

I also tried the following which confused / worried me.

import mpmath as mp
import gmpy as gm
import gmpy2 as gm2
print mp.mpf('1') == gm.mpf('1')
# Result is FALSE
print mp.mpf('1') == gm2.mpfr('1')
# Result is FALSE
print gm.mpf('1') == gm2.mpfr('1')
# Result is FALSE
print mp.mpf('1') == 1
# Result is TRUE
print gm.mpf('1') == 1
# Result is TRUE
print gm2.mpfr('1') == 1
# Result is TRUE

What the heck?

evan54
  • 3,585
  • 5
  • 34
  • 61

2 Answers2

12

mpmath will try to import gmpy2 first.

While you can import both gmpy and gmpy2 at the same time, it is not a supported scenario since gmpy and gmpy2 aren't aware of each other.

I maintain both gmpy and and gmpy2 but I consider gmpy to be obsolete and gmpy2 should be used instead.

casevh
  • 11,093
  • 1
  • 24
  • 35
3

I'm pretty sure it is possible because python records all the modules it has imported in a dictionary. You can see it if you do

   import sys
   print(sys.modules)

This is a dictionary that allows it to see if it has previously imported a module, so it doesn't have to do it again when it sees an import statement. For example, in my code I have import numpy as np in lots of places because any one of those places might be the first time python gets asked to import it. But the import takes a long time, so python doesn't want to do that every time it sees that statement. If numpy is in sys.modules, it just assumes it already knows what it needs to know and skips it.

So to answer your question, do this:

import mpmath
import sys
'gmpy2' in sys.modules.keys()

If you get True, you're using gmpy2 somewhere. And if mpmath and sys are the only things you've imported, it's probably safe to assume that mpmath is the one using gmpy2.

Mike
  • 19,114
  • 12
  • 59
  • 91