Python Forum
Numpy and matplotlib in incorporated Python.
Thread Rating:
  • 4 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Numpy and matplotlib in incorporated Python.
#1
Hi!

I am trying to incorporate Python in my Lazarus (IDE using Free Pascal) application using python4delphi (PythonForDelphi) components. They support both Delphi and Lazarus. I can execute both standard commands and commands from standard imports (math, random, itertools). But I can't use numpy or matplotlib.

I have an error "AttributeError: module 'numpy' has no attribute 'array'' for numpy when I am trying to use a numpy.array and an error "ImportError: cannot import name 'rcParams'" when I am using "import matplotlib.pyplot as plt".

Both libraries are working fine when used from installed Python. When I print sys.path from Lazarus it shows ['PythonPath\\python36.zip', 'PythonPath\\DLLs', 'PythonPath\\lib', 'ProgramPath', 'C:\\Users\\MyUser\\AppData\\Roaming\\Python\\Python36\\site-packages', 'PythonPath', 'PythonPath\\lib\\site-packages']. PythonPath is location where Python is installed, ProgramPath is location where Lazarus program is located.

Thanks in advance for your response.
Reply
#2
You don't happen to have a couple of files named numpy.py and matploblib.py, do you?
Reply
#3
No, I don't have any PY-files. I am executing Lazarus EXE-file which uses the python36.dll file from Python installation folder. All the code is situated inside the TMemo component (standard component for writing text). As far as I understand from the source code of PythonForDelphi components PyRun_String function is used to execute Python code.

I have some additional information. I have tried to import PIL. There are no problems using it. So I was wrong assuming that the problem is in using non-standard modules.
Reply
#4
Hi!

I have found the solution. Numpy and matplotlib have different FPU flags behaviour from Lazarus and Delphi. So import wasn't executed correctly due to "Floating point division by zero exception" (it wasn’t shown but it is the different problem). So I have used SetExceptionMask function to prevent "Floating point division by zero" exception. After that numpy and matplotlib became working correctly.
Reply
#5
Hi!
Thanks for posting this info. I have the same problem when trying to use numpy in the "Python-for-Lazarus" demo project.
At which place in the code do you use the "SetExceptionMask" and which exceptions do need to be masked exactly to prevent the "SIGFPE" appearing ??
Thanks in advance!
wiai
Reply
#6
I have used "SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]);" as the first command in the FormCreate procedure. But I don't know if there are drawbacks of this method.
Reply
#7
Thanks! This seems to work with numpy now. In my case, it looks like masking [exZeroDivide, exPrecision] is fixing the problem.

It is probably safer to mask/unmask the exceptions only when calling the Python execution, e.g.

FPUExceptionMask := GetExceptionMask;
SetExceptionMask([exZeroDivide, exPrecision]);
GetPythonEngine.ExecString('python_commands_in_this_string');
SetExceptionMask(FPUExceptionMask);
Reply
#8
Yes, using GetExceptionMask and SetExceptionMask is better. But [exZeroDivide, exPrecision] is not always enough. For example, "import matplotlib.pyplot as plt" also requires exUnderflow. So I don't know if some options can be omitted for arbitrary Python import.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 290 Mar-26-2024, 02:18 PM
Last Post: snippsat
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,528 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 2,929 Apr-09-2019, 04:54 PM
Last Post: PhysChem
  numpy.float64 and plot_date in matplotlib iv20023 1 5,698 Jun-21-2017, 10:23 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020