Python Forum
Numpy and matplotlib in incorporated Python. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: Data Science (https://python-forum.io/Forum-Data-Science)
+--- Thread: Numpy and matplotlib in incorporated Python. (/Thread-Numpy-and-matplotlib-in-incorporated-Python)



Numpy and matplotlib in incorporated Python. - CSKir - Jan-10-2019

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.


RE: Numpy and matplotlib in incorporated Python. - micseydel - Jan-10-2019

You don't happen to have a couple of files named numpy.py and matploblib.py, do you?


RE: Numpy and matplotlib in incorporated Python. - CSKir - Jan-11-2019

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.


RE: Numpy and matplotlib in incorporated Python. - CSKir - Jan-13-2019

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.


RE: Numpy and matplotlib in incorporated Python. - wiai - Jan-14-2019

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


RE: Numpy and matplotlib in incorporated Python. - CSKir - Jan-15-2019

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.


RE: Numpy and matplotlib in incorporated Python. - wiai - Jan-15-2019

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);


RE: Numpy and matplotlib in incorporated Python. - CSKir - Jan-15-2019

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.