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
  creating an empty NumPy array in Python sandeepatel 2 119 Jan-16-2019, 09:24 AM
Last Post: rinu
  tensorflow stopped after numpy install Devilish 0 136 Dec-28-2018, 09:29 PM
Last Post: Devilish
  Pandas and numpy gehrenfeld 7 337 Dec-26-2018, 07:37 AM
Last Post: ayaz786amd
  Oct2py and Matplotlib installation problems Leloup 2 178 Nov-23-2018, 03:32 PM
Last Post: Leloup
  how can I create a recursive graphic with matplotlib royer14 3 215 Nov-22-2018, 05:00 PM
Last Post: Gribouillis
  how can I create graphics using matplotlib royer14 8 279 Nov-21-2018, 07:02 AM
Last Post: Gribouillis
  future warning when using numpy fancy indexing bluefrog 0 153 Nov-20-2018, 09:44 AM
Last Post: bluefrog
  double pendulum help. (matplotlib) FramedLink 1 170 Nov-08-2018, 11:46 AM
Last Post: FramedLink
  numpy dtype anomaly bluefrog 4 224 Nov-07-2018, 12:37 AM
Last Post: bluefrog
  Numpy library working out cosine. 6pathsMadara 1 195 Nov-05-2018, 03:36 PM
Last Post: Gribouillis

Forum Jump:


Users browsing this thread: 1 Guest(s)