JuliaPy / PyPlot.jl

Plotting for Julia based on matplotlib.pyplot
https://github.com/JuliaPy/PyPlot.jl
MIT License
478 stars 88 forks source link

PyPlot crashes Julia 1.0.1 on macOS Mojave #405

Closed portig closed 6 years ago

portig commented 6 years ago

Using a Python 3.7 installation with matplotlib via MacPorts. Below is the output demonstrating the crash. The same type of installation on a different mac running macOS High Sierra does not have this issue. When using a nightly build of Julia 1.1.0 the "using PyPlot" command finishes with the message "Warning: No working GUI backend found for matplotlib".

I did try to use the Conda that is installed by Julia when setting ENV["PYTHON"] = "", and it caused a similar crash to what's shown here.

Starting Julia 1.0.1 . . . julia> using PyPlot

julia> plot(rand(2,2)) 2018-11-03 22:04:13.610 julia[25104:1185443] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7fea7671cce0 2018-11-03 22:04:13.611 julia[25104:1185443] Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7fea7671cce0' First throw call stack: ( 0 CoreFoundation 0x00007fff37eb2e65 exceptionPreprocess + 256 1 libobjc.A.dylib 0x00007fff63f0d720 objc_exception_throw + 48 2 CoreFoundation 0x00007fff37f3022d -[NSObject(NSObject) retain_OA] + 0 3 CoreFoundation 0x00007fff37e54820 forwarding + 1486 4 CoreFoundation 0x00007fff37e541c8 _CF_forwarding_prep_0 + 120 5 libtk8.6.dylib 0x000000013452231d TkpInit + 413 6 libtk8.6.dylib 0x000000013447a17e Initialize + 2622 7 _tkinter.cpython-37m-darwin.so 0x00000001342a2a0f _tkinter_create + 1183 8 libpython3.7m.dylib 0x00000001115922b9 _PyMethodDef_RawFastCallKeywords + 681 9 libpython3.7m.dylib 0x00000001115923bd _PyCFunction_FastCallKeywords + 45 10 libpython3.7m.dylib 0x000000011169bc4b call_function + 603 11 libpython3.7m.dylib 0x0000000111692e45 _PyEval_EvalFrameDefault + 7893 12 libpython3.7m.dylib 0x0000000111690ad9 _PyEval_EvalCodeWithName + 3305 13 libpython3.7m.dylib 0x00000001115911b1 _PyFunction_FastCallDict + 481 14 libpython3.7m.dylib 0x0000000111592ba4 _PyObject_Call_Prepend + 164 15 libpython3.7m.dylib 0x00000001115fa5aa slot_tp_init + 298 16 libpython3.7m.dylib 0x0000000111603ca7 type_call + 295 17 libpython3.7m.dylib 0x0000000111591e73 _PyObject_FastCallKeywords + 691 18 libpython3.7m.dylib 0x000000011169bcb4 call_function + 708 19 libpython3.7m.dylib 0x0000000111692f6c _PyEval_EvalFrameDefault + 8188 20 libpython3.7m.dylib 0x0000000111591a6e function_code_fastcall + 254 21 libpython3.7m.dylib 0x000000011169bcbe call_function + 718 22 libpython3.7m.dylib 0x0000000111692e45 _PyEval_EvalFrameDefault + 7893 23 libpython3.7m.dylib 0x0000000111690ad9 _PyEval_EvalCodeWithName + 3305 24 libpython3.7m.dylib 0x00000001115911b1 _PyFunction_FastCallDict + 481 25 libpython3.7m.dylib 0x0000000111592ba4 _PyObject_Call_Prepend + 164 26 libpython3.7m.dylib 0x00000001115945b4 method_call + 36 27 libpython3.7m.dylib 0x00000001115925b6 PyObject_Call + 246 28 libpython3.7m.dylib 0x00000001116931b5 _PyEval_EvalFrameDefault + 8773 29 libpython3.7m.dylib 0x0000000111690ad9 _PyEval_EvalCodeWithName + 3305 30 libpython3.7m.dylib 0x00000001115911b1 _PyFunction_FastCallDict + 481 31 ??? 0x000000011117a3da 0x0 + 4581729242 32 ??? 0x000000011117fb17 0x0 + 4581751575 33 ??? 0x000000011119fca8 0x0 + 4581883048 34 libjulia.1.0.dylib 0x0000000103981ac3 jl_fptr_trampoline + 51 35 ??? 0x000000011119fbb6 0x0 + 4581882806 36 libjulia.1.0.dylib 0x00000001039924b2 jl_f__apply + 1218 37 libjulia.1.0.dylib 0x000000010399294f jl_f__apply_latest + 63 38 ??? 0x000000011119f785 0x0 + 4581881733 39 ??? 0x000000011119d5fe 0x0 + 4581873150 40 ??? 0x000000011119d724 0x0 + 4581873444 41 ??? 0x0000000111150bbc 0x0 + 4581559228 ) libc++abi.dylib: terminating with uncaught exception of type NSException

signal (6): Abort trap: 6 in expression starting at no file:0 __pthread_kill at /usr/lib/system/libsystem_kernel.dylib (unknown line) Allocations: 24070144 (Pool: 24065397; Big: 4747); GC: 53 Abort trap: 6

tkf commented 6 years ago

Reading https://stackoverflow.com/a/34109240 it looks like it may be solved by installing a compatible matplotlib backend. See also https://github.com/JuliaPy/PyPlot.jl/issues/399#issuecomment-430592493

Also, check it in Python without using Julia and PyPlot to see where the problem is.

portig commented 6 years ago

Thanks for your comments, @tkf.

When running the same Python 3.7 installation from the command line I have no problem using matplotlib. Pasting some of the examples from the matplotlib 3.0.0 tutorial into a Python session give correct results and the GUI buttons in the plot window work great.

tkf commented 6 years ago

Did you try activating Tk backend before importing PyPlot?

julia> using PyCall

julia> pygui(:tk)
:tk

julia> using PyPlot

If it does not work, you can try other backends https://matplotlib.org/tutorials/introductory/usage.html#what-is-a-backend

portig commented 6 years ago

When I try your commands, the response is that tk is not installed. With the command pygui() the response is ERROR: No supported Python GUI toolkit is installed..

When using pip to list the Python packages installed, both on the non-working Mojave system and the working High Sierra system, the list of packages and versions is identical.

Running Python from the command line, there is no problem using Matplotlib to produce graphics implying that some kind of backend is actually installed, however Julia is not able to make use of it.

tkf commented 6 years ago

You can use matplotlib.get_backend() to get the backend used by matplotlib.

As mentioned in the comment I linked above https://github.com/JuliaPy/PyPlot.jl/issues/399#issuecomment-430592493, it is known that macosx backend does not work with PyPlot. It looks like qt5 works.

stevengj commented 6 years ago

Running from Python, it uses the macosx backend by default, but it is really hard to get this working in Julia without losing the Julia event loop (#11).

You can edit your .matplotlibrc file or set the MPLBACKEND environment variable to change the default backend in both Julia and Python.

stevengj commented 6 years ago

Does the Conda installation work? i.e.

ENV["PYTHON"]=""  # use the Conda Python
Pkg.build("PyCall")
using PyPlot

?

portig commented 6 years ago

It turns out that command-line Python is using the MacOSX backend. Adding the environment variable MPLBACKEND=TkAgg caused Matplotlib in Python to no longer work. Eliminating that environment variable or changing it to MacOSX allowed Matplotlib in a Python session to work again.

After experimenting with MPLBACKEND, PyPlot in Julia still does not work, but differently than before: now, instead of crashing during a plot command, the using PyPlot command produces No working GUI backend found for matplotlib.

I then removed PyPlot and PyCall from the installation and replaced them while ENV["PYTHON"]="". After using PyCall the command pygui() now produced the response :tk, but after issuing the plot(rand(2,2) command, Julia crashed again with the following traceback:

julia> plot(rand(2,2))
2018-11-05 22:10:30.910 julia[29167:1988944] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7fa432cf0770
2018-11-05 22:10:30.918 julia[29167:1988944] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7fa432cf0770'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff37eb2e65 __exceptionPreprocess + 256
    1   libobjc.A.dylib                     0x00007fff63f0d720 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff37f3022d -[NSObject(NSObject) __retain_OA] + 0
    3   CoreFoundation                      0x00007fff37e54820 ___forwarding___ + 1486
    4   CoreFoundation                      0x00007fff37e541c8 _CF_forwarding_prep_0 + 120
    5   libtk8.6.dylib                      0x00000001295e331d TkpInit + 413
    6   libtk8.6.dylib                      0x000000012953b17e Initialize + 2622
    7   _tkinter.cpython-37m-darwin.so      0x0000000129363a0f _tkinter_create + 1183
    8   libpython3.7m.dylib                 0x0000000128d162b9 _PyMethodDef_RawFastCallKeywords + 681
    9   libpython3.7m.dylib                 0x0000000128d163bd _PyCFunction_FastCallKeywords + 45
    10  libpython3.7m.dylib                 0x0000000128e1fc4b call_function + 603
    11  libpython3.7m.dylib                 0x0000000128e16e45 _PyEval_EvalFrameDefault + 7893
    12  libpython3.7m.dylib                 0x0000000128e14ad9 _PyEval_EvalCodeWithName + 3305
    13  libpython3.7m.dylib                 0x0000000128d151b1 _PyFunction_FastCallDict + 481
    14  libpython3.7m.dylib                 0x0000000128d16ba4 _PyObject_Call_Prepend + 164
    15  libpython3.7m.dylib                 0x0000000128d7e5aa slot_tp_init + 298
    16  libpython3.7m.dylib                 0x0000000128d87ca7 type_call + 295
    17  libpython3.7m.dylib                 0x0000000128d15e73 _PyObject_FastCallKeywords + 691
    18  libpython3.7m.dylib                 0x0000000128e1fcb4 call_function + 708
    19  libpython3.7m.dylib                 0x0000000128e16f6c _PyEval_EvalFrameDefault + 8188
    20  libpython3.7m.dylib                 0x0000000128d15a6e function_code_fastcall + 254
    21  libpython3.7m.dylib                 0x0000000128e1fcbe call_function + 718
    22  libpython3.7m.dylib                 0x0000000128e16e45 _PyEval_EvalFrameDefault + 7893
    23  libpython3.7m.dylib                 0x0000000128e14ad9 _PyEval_EvalCodeWithName + 3305
    24  libpython3.7m.dylib                 0x0000000128d151b1 _PyFunction_FastCallDict + 481
    25  libpython3.7m.dylib                 0x0000000128d16ba4 _PyObject_Call_Prepend + 164
    26  libpython3.7m.dylib                 0x0000000128d185b4 method_call + 36
    27  libpython3.7m.dylib                 0x0000000128d165b6 PyObject_Call + 246
    28  libpython3.7m.dylib                 0x0000000128e171b5 _PyEval_EvalFrameDefault + 8773
    29  libpython3.7m.dylib                 0x0000000128e14ad9 _PyEval_EvalCodeWithName + 3305
    30  libpython3.7m.dylib                 0x0000000128d151b1 _PyFunction_FastCallDict + 481
    31  ???                                 0x000000012ef01eba 0x0 + 5082455738
    32  ???                                 0x000000012ef020e7 0x0 + 5082456295
    33  ???                                 0x000000012ef5a0d8 0x0 + 5082816728
    34  libjulia.1.0.dylib                  0x000000010a3d2ac3 jl_fptr_trampoline + 51
    35  ???                                 0x000000012ef59fe6 0x0 + 5082816486
    36  libjulia.1.0.dylib                  0x000000010a3e34b2 jl_f__apply + 1218
    37  libjulia.1.0.dylib                  0x000000010a3e394f jl_f__apply_latest + 63
    38  ???                                 0x000000012ef59bb5 0x0 + 5082815413
    39  ???                                 0x000000012ef579be 0x0 + 5082806718
    40  ???                                 0x000000012ef57ae4 0x0 + 5082807012
    41  ???                                 0x000000012ef573d7 0x0 + 5082805207
    42  ???                                 0x000000010f7de2dc 0x0 + 4554875612
)
libc++abi.dylib: terminating with uncaught exception of type NSException

signal (6): Abort trap: 6
in expression starting at no file:0
__pthread_kill at /usr/lib/system/libsystem_kernel.dylib (unknown line)
Allocations: 62842323 (Pool: 62826961; Big: 15362); GC: 139
Abort trap: 6

This is basically the same response as when using the Python installation outside of Conda.

The Conda installed by Julia is version 3.7. Running pip list in its bin subdirectory gives the following packages as being installed:

./pip list
Package         Version   
--------------- ----------
asn1crypto      0.24.0    
certifi         2018.10.15
cffi            1.11.5    
chardet         3.0.4     
conda           4.5.11    
cryptography    2.3.1     
cycler          0.10.0    
idna            2.7       
kiwisolver      1.0.1     
matplotlib      3.0.1     
mkl-fft         1.0.6     
mkl-random      1.0.1     
numpy           1.15.3    
pip             10.0.1    
pycosat         0.6.3     
pycparser       2.18      
pyOpenSSL       18.0.0    
pyparsing       2.3.0     
PySocks         1.6.8     
python-dateutil 2.7.5     
pytz            2018.7    
requests        2.19.1    
ruamel-yaml     0.15.46   
setuptools      40.2.0    
six             1.11.0    
tornado         5.1.1     
urllib3         1.23      
wheel           0.31.1    
You are using pip version 10.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

It turns out that the Python installed in Conda is not able to plot either. Executing the following commands:

./python
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
matplotlib.get_backend()

This leads to the output TkAgg The command that crashes this Python session is fig, ax = plt.subplots() and leads to the following traceback:

2018-11-05 22:27:48.455 python[29521:2028138] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7ffc9b49e890
2018-11-05 22:27:48.457 python[29521:2028138] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7ffc9b49e890'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff37eb2e65 __exceptionPreprocess + 256
    1   libobjc.A.dylib                     0x00007fff63f0d720 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff37f3022d -[NSObject(NSObject) __retain_OA] + 0
    3   CoreFoundation                      0x00007fff37e54820 ___forwarding___ + 1486
    4   CoreFoundation                      0x00007fff37e541c8 _CF_forwarding_prep_0 + 120
    5   libtk8.6.dylib                      0x000000011c63731d TkpInit + 413
    6   libtk8.6.dylib                      0x000000011c58f17e Initialize + 2622
    7   _tkinter.cpython-37m-darwin.so      0x000000011c3b7a0f _tkinter_create + 1183
    8   python                              0x00000001087ce8d6 _PyMethodDef_RawFastCallKeywords + 230
    9   python                              0x000000010890eb87 call_function + 247
    10  python                              0x000000010890c6ba _PyEval_EvalFrameDefault + 45274
    11  python                              0x0000000108900332 _PyEval_EvalCodeWithName + 418
    12  python                              0x00000001087cd5a7 _PyFunction_FastCallDict + 231
    13  python                              0x0000000108851091 slot_tp_init + 193
    14  python                              0x000000010885b051 type_call + 241
    15  python                              0x00000001087ce2a3 _PyObject_FastCallKeywords + 179
    16  python                              0x000000010890ec24 call_function + 404
    17  python                              0x000000010890c7ae _PyEval_EvalFrameDefault + 45518
    18  python                              0x00000001087ce098 function_code_fastcall + 120
    19  python                              0x000000010890eb3e call_function + 174
    20  python                              0x000000010890c6ba _PyEval_EvalFrameDefault + 45274
    21  python                              0x0000000108900332 _PyEval_EvalCodeWithName + 418
    22  python                              0x00000001087cd5a7 _PyFunction_FastCallDict + 231
    23  python                              0x00000001087d14c2 method_call + 130
    24  python                              0x00000001087ceef4 PyObject_Call + 100
    25  python                              0x000000010890c905 _PyEval_EvalFrameDefault + 45861
    26  python                              0x0000000108900332 _PyEval_EvalCodeWithName + 418
    27  python                              0x00000001087cd5a7 _PyFunction_FastCallDict + 231
    28  python                              0x000000010890c905 _PyEval_EvalFrameDefault + 45861
    29  python                              0x0000000108900332 _PyEval_EvalCodeWithName + 418
    30  python                              0x00000001087ce7a3 _PyFunction_FastCallKeywords + 195
    31  python                              0x000000010890eb3e call_function + 174
    32  python                              0x000000010890c6ba _PyEval_EvalFrameDefault + 45274
    33  python                              0x0000000108900332 _PyEval_EvalCodeWithName + 418
    34  python                              0x000000010896722b PyRun_InteractiveOneObjectEx + 1147
    35  python                              0x0000000108963e11 PyRun_InteractiveLoopFlags + 321
    36  python                              0x0000000108963c60 PyRun_AnyFileExFlags + 64
    37  python                              0x0000000108990d3a pymain_main + 10938
    38  python                              0x00000001087a0a6d main + 125
    39  libdyld.dylib                       0x00007fff64fdc08d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6
tkf commented 6 years ago

Please quote code blocks with a pair of three backticks (not one). It's very hard to read. :wink:

It turns out that the Python installed in Conda is not able to plot either.

If it doesn't work in Python then it won't work in Julia. Did you try other backends? How about Qt5Agg? You may need to run using Conda; Conda.add("pyqt") in Julia (or conda install pyqt in system shell).

portig commented 6 years ago

Thank you, @tkf, for your suggestion to install pyqt into Conda. This solved the problem. Surprisingly your procedure appears to have added something to Conda that now allows both :tk and :qt5 to be usable, both in Julia and in Python.

portig commented 6 years ago

After the Conda installed by Julia allowed PyPlot to work I added the same packages that the Conda.add("pyqt") had added to my system-wide Python 3.7 installation. I then removed PyPlot from Julia and reinstalled it with ENV["PYTHON"] set to the executable of the system-wide Python. This worked also. Now I have removed Conda from the Julia installation going as far as deleting the ~/.julia/conda subdirectory, over 2 GB in size.

divan commented 5 years ago

Any workaround for this? I have it on the latest MacOS X 10.14.3 and latest Julia 1.1.0.

Julia Output
$ julia riemann_sum.jl
2019-03-03 16:45:27.929 julia[28988:737043] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7fdfcd7dfcc0
2019-03-03 16:45:27.936 julia[28988:737043] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7fdfcd7dfcc0'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff438edded __exceptionPreprocess + 256
    1   libobjc.A.dylib                     0x00007fff6f9b9720 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff4396b195 -[NSObject(NSObject) __retain_OA] + 0
    3   CoreFoundation                      0x00007fff4388fa60 ___forwarding___ + 1486
    4   CoreFoundation                      0x00007fff4388f408 _CF_forwarding_prep_0 + 120
    5   libtk8.6.dylib                      0x000000013635531d TkpInit + 413
    6   libtk8.6.dylib                      0x00000001362ad17e Initialize + 2622
    7   _tkinter.cpython-37m-darwin.so      0x00000001335e7a0f _tkinter_create + 1183
    8   libpython3.7m.dylib                 0x000000010f97c2b9 _PyMethodDef_RawFastCallKeywords + 681
    9   libpython3.7m.dylib                 0x000000010f97c3bd _PyCFunction_FastCallKeywords + 45
    10  libpython3.7m.dylib                 0x000000010fa85c4b call_function + 603
    11  libpython3.7m.dylib                 0x000000010fa7ce45 _PyEval_EvalFrameDefault + 7893
    12  libpython3.7m.dylib                 0x000000010fa7aad9 _PyEval_EvalCodeWithName + 3305
    13  libpython3.7m.dylib                 0x000000010f97b1b1 _PyFunction_FastCallDict + 481
    14  libpython3.7m.dylib                 0x000000010f97cba4 _PyObject_Call_Prepend + 164
    15  libpython3.7m.dylib                 0x000000010f9e45aa slot_tp_init + 298
    16  libpython3.7m.dylib                 0x000000010f9edca7 type_call + 295
    17  libpython3.7m.dylib                 0x000000010f97be73 _PyObject_FastCallKeywords + 691
    18  libpython3.7m.dylib                 0x000000010fa85cb4 call_function + 708
    19  libpython3.7m.dylib                 0x000000010fa7cf6c _PyEval_EvalFrameDefault + 8188
    20  libpython3.7m.dylib                 0x000000010f97ba6e function_code_fastcall + 254
    21  libpython3.7m.dylib                 0x000000010fa85cbe call_function + 718
    22  libpython3.7m.dylib                 0x000000010fa7ce45 _PyEval_EvalFrameDefault + 7893
    23  libpython3.7m.dylib                 0x000000010fa7aad9 _PyEval_EvalCodeWithName + 3305
    24  libpython3.7m.dylib                 0x000000010f97b1b1 _PyFunction_FastCallDict + 481
    25  libpython3.7m.dylib                 0x000000010f97cba4 _PyObject_Call_Prepend + 164
    26  libpython3.7m.dylib                 0x000000010f97e5b4 method_call + 36
    27  libpython3.7m.dylib                 0x000000010f97c5b6 PyObject_Call + 246
    28  libpython3.7m.dylib                 0x000000010fa7d1b5 _PyEval_EvalFrameDefault + 8773
    29  libpython3.7m.dylib                 0x000000010fa7aad9 _PyEval_EvalCodeWithName + 3305
    30  libpython3.7m.dylib                 0x000000010f97b1b1 _PyFunction_FastCallDict + 481
    31  ???                                 0x000000010f556b75 0x0 + 4552223605
    32  ???                                 0x000000010f5652cc 0x0 + 4552282828
    33  ???                                 0x000000010f56a4f8 0x0 + 4552303864
    34  libjulia.1.1.dylib                  0x0000000109d39b03 jl_fptr_trampoline + 51
    35  ???                                 0x000000010f56a406 0x0 + 4552303622
    36  libjulia.1.1.dylib                  0x0000000109d4a312 jl_f__apply + 1218
    37  libjulia.1.1.dylib                  0x0000000109d4a68f jl_f__apply_latest + 63
    38  ???                                 0x000000010f56a145 0x0 + 4552302917
    39  ???                                 0x000000010f56846c 0x0 + 4552295532
    40  ???                                 0x000000010f568584 0x0 + 4552295812
    41  ???                                 0x000000010f539d2c 0x0 + 4552105260
)
libc++abi.dylib: terminating with uncaught exception of type NSException

signal (6): Abort trap: 6
in expression starting at /Users/divan/src/test/julia/riemann_sum.jl:11
__pthread_kill at /usr/lib/system/libsystem_kernel.dylib (unknown line)
Allocations: 10715487 (Pool: 10714032; Big: 1455); GC: 23
Abort trap: 6
[mac@julia ]$ julia --version
julia version 1.1.0
stevengj commented 5 years ago

The workaround is to use a different matplotlib backend as mentioned above.

ctkelley commented 5 years ago

"I then removed PyPlot from Julia" is something I've tried and failed to do. Pkg.rm does not seem to do the job. I've been starting over with a new .julia for the last couple days.

Your comment of Nov 7 is above my skill level, since I am no Python user and have no idea where the system-wide python lives, unless it's in the anaconda directory somewhere. I need PyPlot to work (or something else close to the matlab commands) and it was doing fine until recently. If the anaconda directory is ok, how do I sent the PYTHON environmental variable? "anaconda3/xx/yy"?

Thanks. You seem to have this figured out better than most.