sczesla / PyAstronomy

A collection of astronomy-related routines in Python
152 stars 35 forks source link

Overrides matplotlib backend setting #53

Closed sazl closed 2 years ago

sazl commented 3 years ago

PyAstronomy currently overrides the chosen matplotlib backend setting on import

https://github.com/sczesla/PyAstronomy/blob/f1d34a37a6075580650000fa7d287c524edb01ac/src/pyaGui/pyaPicker.py#L2

This causes problems when an application attempts to set the matplotlib backend after a PyAstronomy import.

Error trace ```python This call to matplotlib.use() has no effect because the backend has already been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot, or matplotlib.backends is imported for the first time. The backend was *originally* set to 'TkAgg' by the following code: File "asad_main.py", line 6, in import asad.args as args File "/mnt/c/Users/Sami-Laptop/Programming/ASAD_default/asad/args.py", line 9, in import asad.pyasad as pyasad File "/mnt/c/Users/Sami-Laptop/Programming/ASAD_default/asad/pyasad.py", line 21, in from radial_velocity import radial_velocity File "/mnt/c/Users/Sami-Laptop/Programming/ASAD_default/asad/radial_velocity.py", line 3, in from PyAstronomy import pyasl File "/home/sami/.cache/pypoetry/virtualenvs/asad-lTa7gUMb-py2.7/lib/python2.7/site-packages/PyAstronomy/pyasl/__init__.py", line 11, in from PyAstronomy.pyasl.asl import * File "/home/sami/.cache/pypoetry/virtualenvs/asad-lTa7gUMb-py2.7/lib/python2.7/site-packages/PyAstronomy/pyasl/asl/__init__.py", line 25, in _moduleImportStatus[(__name__, m)] = pyaC.pyaimportallfrom(m, "PyAstronomy.pyasl.asl", globals()) File "/home/sami/.cache/pypoetry/virtualenvs/asad-lTa7gUMb-py2.7/lib/python2.7/site-packages/PyAstronomy/pyaC/importCheck.py", line 34, in pyaimportallfrom n = importlib.import_module("." + mn, pak) File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) File "/home/sami/.cache/pypoetry/virtualenvs/asad-lTa7gUMb-py2.7/lib/python2.7/site-packages/PyAstronomy/pyasl/asl/broad.py", line 4, in from PyAstronomy import funcFit as fuf File "/home/sami/.cache/pypoetry/virtualenvs/asad-lTa7gUMb-py2.7/lib/python2.7/site-packages/PyAstronomy/funcFit/__init__.py", line 13, in "matplotlib.pylab", "pyfits", "emcee", "progressbar"]) File "/home/sami/.cache/pypoetry/virtualenvs/asad-lTa7gUMb-py2.7/lib/python2.7/site-packages/PyAstronomy/pyaC/importCheck.py", line 88, in __init__ __import__(module) File "/home/sami/.cache/pypoetry/virtualenvs/asad-lTa7gUMb-py2.7/lib/python2.7/site-packages/matplotlib/pylab.py", line 252, in from matplotlib import cbook, mlab, pyplot as plt File "/home/sami/.cache/pypoetry/virtualenvs/asad-lTa7gUMb-py2.7/lib/python2.7/site-packages/matplotlib/pyplot.py", line 71, in from matplotlib.backends import pylab_setup File "/home/sami/.cache/pypoetry/virtualenvs/asad-lTa7gUMb-py2.7/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 17, in line for line in traceback.format_stack() ```

It would be helpful if this code was moved to an initialization function so a user can control the switching of the backend.

Thanks

sczesla commented 3 years ago

Thanks for reporting. I looked at this and removed the backend switch with a commit. In fact, the change of backend appears unnecessary to me now (I think it was once required to ensure the Navigation bar was there). I am a bit concerned about other side effects, but the on-import global switch is definitely a side-effect to be avoided. Cheers

sazl commented 2 years ago

thank you for the fix, much appreciated 👍🏾 Do you plan on publishing a new version on pip that incorprates this fix?

Thanks

sczesla commented 2 years ago

Sure. In fact, I was just working on this (: Cheers