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
  Keras + Matplotlib causing crash spearced 2 174 May-17-2019, 09:47 AM
Last Post: spearced
  MatPlotLib 2d plot of current density? ruben 0 44 May-13-2019, 06:47 AM
Last Post: ruben
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 144 Apr-09-2019, 04:54 PM
Last Post: PhysChem
  while creating a array using numpy.arrange, how to negate only the last 50 numbers? vicky53 1 146 Apr-06-2019, 11:52 AM
Last Post: scidam
  How to arrange the four pictures of a matplotlib.pyplot? vokoyo 0 178 Apr-04-2019, 10:58 PM
Last Post: vokoyo
  counting the occurence of a specified number in a numpy-matrix PhysChem 1 116 Apr-03-2019, 01:37 PM
Last Post: PhysChem
  How to get the size of a numpy array? mcgrim 2 171 Mar-23-2019, 02:25 PM
Last Post: perfringo
  Numpy Quick start Tutorial leodavinci1990 1 307 Feb-03-2019, 07:10 AM
Last Post: buran
  creating an empty NumPy array in Python sandeepatel 2 382 Jan-16-2019, 09:24 AM
Last Post: rinu
  tensorflow stopped after numpy install Devilish 0 351 Dec-28-2018, 09:29 PM
Last Post: Devilish

Forum Jump:


Users browsing this thread: 1 Guest(s)