Bottom Page

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.
You like this post
Quote
#2
You don't happen to have a couple of files named numpy.py and matploblib.py, do you?
You like this post
Quote
#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.
You like this post
Quote
#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.
You like this post
Quote
#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
Quote
#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.
Quote
#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);
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  import numpy in sub-files paul18fr 1 89 Aug-06-2019, 12:38 PM
Last Post: chakrimakam
  Converting set of tuples to set of 2D numpy arrays ThemePark 0 75 Aug-04-2019, 05:07 PM
Last Post: ThemePark
  Trying to replace for loops with numpy vspoloni 8 315 Aug-04-2019, 07:09 AM
Last Post: paul18fr
  Add column to numpy matrix Gigux 1 111 Aug-02-2019, 12:05 AM
Last Post: scidam
  Python/C API Matrix to Numpy jibarra 0 75 Jul-29-2019, 03:25 PM
Last Post: jibarra
  numpy.copy / numpy.delete paul18fr 3 129 Jul-26-2019, 01:51 PM
Last Post: paul18fr
  Spacing pie chart colours evenly in matplotlib? Giovanni_diJacopo 1 282 Jul-12-2019, 12:31 PM
Last Post: scidam
  Error installing numpy srm 3 296 Jul-08-2019, 06:26 PM
Last Post: srm
  AttributeError: module 'numpy' has no attribute 'array aapurdel 7 1,301 May-29-2019, 02:48 AM
Last Post: heiner55
  Array in numpy Truman 5 3,520 May-26-2019, 10:14 AM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)