pyscripter / python4delphi

Free components that wrap up Python into Delphi and Lazarus (FPC)
MIT License
923 stars 315 forks source link

Floating point division by zero when first run import (python 3.6) RAD 10.2 x64 #69

Closed RavWin closed 5 years ago

RavWin commented 5 years ago

RAD Studio 10.2 Testing script:

import numpy as np import matplotlib.mlab as mlab import matplotlib.pyplot as plt

example data

mu = 100 # mean of distribution sigma = 15 # standard deviation of distribution x = mu + sigma * np.random.randn(10000) num_bins = 50

the histogram of the data

n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5)

add a 'best fit' line

y = mlab.normpdf(bins, mu, sigma) plt.plot(bins, y, 'r--') plt.xlabel('Smarts') plt.ylabel('Probability') plt.title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')

Tweak spacing to prevent clipping of ylabel

plt.subplots_adjust(left=0.15) plt.show()

Sample Project Demo01 when Execute it generates error floating point division by zero. Second time it runs ok. Crash is on 5305 string in PythonEngine Result := PyRun_String(PAnsiChar(CleanString(command)), mode, _globals, _locals);

pyscripter commented 5 years ago

You need to call MaskFPUExceptions before importing numpy.

See https://stackoverflow.com/questions/3933851/nan-giving-an-error-depending-on-python-startup for details.

RavWin commented 5 years ago

Thanks. It does help in Delphi2010 with 32 bit app. But in RAD 10.2 with x64 app it crashes on the same simple script

from bokeh.plotting import figure, output_file, show

prepare some data

x = [1, 2, 3, 4, 5] y = [6, 7, 2, 4, 5]

output to static HTML file

output_file("lines.html")

create a new plot with a title and axis labels

p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')

add a line renderer with legend and line thickness

p.line(x, y, legend="Temp.", line_width=2)

show the results

show(p)

image

RavWin commented 5 years ago

You need to call MaskFPUExceptions before importing numpy.

See https://stackoverflow.com/questions/3933851/nan-giving-an-error-depending-on-python-startup for details.

It helps for Delphi2010 only.

pyscripter commented 5 years ago

Your script runs fine inside pyscripter's internal engine (python 3.7). PyScripter uses P4D and is a 64bit Delphi app compiled with Delphi 10.3. So I cannot reproduce the issue.

PyScripter calls MaskFPUExceptions before loading the python library and before executing each script.

I am not sure it is related but have you seen the wiki page on manifest files?

RavWin commented 5 years ago

PyScripter calls MaskFPUExceptions before loading the python library and before executing each script.

I have installed Python_XE7.dpk in RAD Studio 10.2, and modified Demo01 procedure TForm1.Button1Click(Sender: TObject); var OldControlWord: Word; begin OldControlWord := Get8087CW(); Set8087CW($133F); try PythonEngine1.ExecStrings( Memo1.Lines ); finally Set8087CW(OldControlWord); end; end;

Is it correct MaskFPUExceptions method? With 32 bit version it works OK. but in Python_XE7 no option for x64 platfom to Install VCL components. May be problem in this? Only with x32 version of Python_XE7.dpk installed project could be opened.

I am not sure it is related but have you seen the wiki page on manifest files? If you mean https://github.com/pyscripter/python4delphi/tree/master/PythonForDelphi, then yes.

pyscripter commented 5 years ago

MaskFPUExceptions is a function in PythonEngine.pas. No need to provide your own.

Regarding the Vcl components, Delphi is a 32 bit application. You only compile the package for 32 bit. You can design the forms in 32 bit and then compile for 64 bits.

RavWin commented 5 years ago

MaskFPUExceptions is a function in PythonEngine.pas. No need to provide your own.

Regarding the Vcl components, Delphi is a 32 bit application. You only compile the package for 32 bit. You can design the forms in 32 bit and then compile for 64 bits.

Oh, I see! Thank you very much! Now it works pretty well!

IbrahimAdham commented 7 months ago

same problem with Delphi 11.3 Python 11.3.8 MaskFPUExceptions(true); plt.show(); MaskFPUExceptions(false); solved the problem