GMPY2安装失败及conjugate函数调用报错求助
Hey there! Let's work through your gmpy2 issues one by one:
mpir.h missing error during compilation It looks like your pip install is trying to use the Windows Visual Studio compiler (cl.exe) instead of the Linux GCC compiler from your WSL Ubuntu environment—this is why it can't find the mpir.h file you installed in Ubuntu. Here's how to fix this:
First, make sure you're fully in your WSL Ubuntu shell, and confirm you're using the Linux versions of Python and pip. Run these commands to check:
which python3 which pip3The output should point to paths inside your Ubuntu filesystem (like
/usr/bin/python3), not Windows paths starting withC:\.If you haven't already, install Python's development headers in Ubuntu to support compiling extensions:
sudo apt-get install python3-devEnsure your MPIR installation was properly installed system-wide. When you compiled MPIR, did you run
sudo make installafter building? This step copies thempir.hheader to/usr/includeand the library files to/usr/lib, so the compiler can find them.Finally, use Ubuntu's pip3 to install gmpy2, which will use the Linux GCC compiler instead of Windows' cl.exe:
pip3 install gmpy2 --user(The
--userflag installs it for your user account, avoiding permission issues.)
The AttributeError happens because gmpy2 doesn't have a module-level conjugate function—instead, the conjugate method belongs directly to mpc (gmpy2 complex) objects. Alternatively, you can use the module-level conj function. Here's both correct approaches:
Option 1: Use the mpc object's conjugate() method
import gmpy2 from gmpy2 import mpc # Create a complex number instance my_complex = mpc(3 + 4j) # Call conjugate on the instance conjugated = my_complex.conjugate() print(conjugated) # Output: (3-4j)
Option 2: Use the module-level gmpy2.conj() function
import gmpy2 from gmpy2 import mpc my_complex = mpc(3 + 4j) conjugated = gmpy2.conj(my_complex) print(conjugated) # Same result: (3-4j)
Your original code tried to call gmpy2.conjugate(mpc())—that's why you got the error, since the module doesn't expose that function name.
内容的提问来源于stack exchange,提问作者gypsyzz

