I'm having trouble getting an exe generated by PyInstaller on one PC (PC-Good) working on another (PC-Bad).
The exe is created on PC-Good, and executes as expected on PC-Good
Both PCs are running Windows 10
Issue occurs on PC-Bad upon calling most of the matplotlib.pyplot methods (such as subplots() or plot())
Issue persists even when matplotlib is in non-interactive mode
No error is reported upon failure. exe simply exits
even when matplotlib verbosity is changed to 'debug'
even when the problem statement is captured in try-exception
See below for code:
print('Start')
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
print("Import of matplotlib & pyplot successful")
plt.set_loglevel("debug")
x = [0, 1, 2, 3, 4]
y = [4, 3, 2, 1, 0]
print('list creation successful')
try:
fig, ax = plt.subplots(1, 1, figsize=(12, 6))
except Exception as exception:
print(exception.__class__.__name__ + ": ", + exception.message)
finally:
print('run subplots() successful')
plt.scatter(x, y)
print('plot creation successful')
plt.savefig('saved_plot.png')
print('code complete')
Output on PC-Good:
Start
C:\tools\miniconda3\envs\bcht\lib\site-packages\PyInstaller\loader\pyimod03_importers.py:623: MatplotlibDeprecationWarning:
The MATPLOTLIBDATA environment variable was deprecated in Matplotlib 3.1 and will be removed in 3.3.
exec(bytecode, module.__dict__)
Import of matplotlib & pyplot successful
list creation successful
run subplots() successful
plot creation successful
DEBUG:matplotlib.font_manager:findfont: ...
(and many more matplotlib DEBUG messages)
code complete
Output on PC-Bad:
Start
C:\tools\miniconda3\envs\bcht\lib\site-packages\PyInstaller\loader\pyimod03_importers.py:623: MatplotlibDeprecationWarning:
The MATPLOTLIBDATA environment variable was deprecated in Matplotlib 3.1 and will be removed in 3.3.
Import of matplotlib & pyplot successful
list creation successful
Since I get no error output, I'm quite lost as to where to go next. Matplotlib documentation doesn't offer any additional tips on getting more granular debug messages for methods like plot or subplots. Has anyone else observed such an issue or know of a fix? Or does anyone know if there's a way to get Matplotlib to tell me more?
There can be multiple reasons for this.
Pyinstaller didn't include modules "completely" : pyinstaller tries to reduce size by including only those parts of modules which are used by your application. It might not have detected the use of some vital part of module and not included it. Try using pyinstaller in one directory mode. Then you will be able to see the module selected by pynstaller. If something is missing then next time you can specify to include that module.
Try putting checkpoints like print("reached some function") in your code for debugging purpose. It will make it easier to find where is the problem.
Also try to run the code in a normal python on target computer.
I found a workaround for this issue, which I can't call a fix yet due to the fact that neither PyInstaller nor MatPlotlib provides any debug message to further the investigation for rootcause. Nevertheless, it gets rid of the issue that I was seeing.
TL;DR
Issue is due to incompatibility when using PyInstaller to generate executable with Python codes with matplotlib 3.1.3 and numpy 1.18.1. This incompatibility does not cause the python script itself to error out. This incompatibility does not produce any error message or symptoms that would point to itself, thus, making it difficult to rootcause.
If anyone observes a frozen executable quitting silently without any error message. It's worthwhile to investigate whether this is due to library incompatibility. If the quitting occurs during execution of mpl methods, then look into numpy immediately.
Now the TL:
It started with a suspicion that the issue was due to virtual environment or conda meddling with the freezing process (they are not) so I used another computer to install a clean python (3.7.9), and matplotlib 3.1.3 which populated all its dependencies automatically (let's call this new environment "lean env" and the Source PC in the problem statement "full env"). I replicated the bundling/freezing process on lean env and everything works fine. At this point, I noticed that a number of libraries in the lean env are newer than the full env. So I upgraded them all on the full env, including:
numpy from 1.18.1 to 1.20.0
kiwisolver from 1.3.0 to 1.3.1
After this, the full env is also able to generate a bundle that works perfectly fine when run on both Source PC and Target PC. So the problem is likely to be the "silent" incompatibility between matplotlib 3.1.3 and one of those two.
To narrow things further, I reverted numpy to 1.18.1 in the full env and reproduced the old problem I saw. There it is.
The frustrating aspect of this is that neither numpy nor matplotlib generated any discernable error (nor Pyinstaller). In addition, matplotlib itself does not have any method to produce any useful verbose debug message to further the investigation. (I did turn on the log_level of matplotlib to debug, but only got some info indicating the fonts being used.) For this reason, I will not proceed any further to investigate. The workaround is sufficient for my use. If anyone else is interested, feel free to proceed by interrogating mpl. Neither will I conduct further interop between pyinstaller, matplotlib, and numpy. All I'll say is:
Good: matplotlib==3.1.3, numpy==1.20.0, pyinstaller==3.6
Bad: matplotlib==3.1.3, numpy==1.18.1, pyinstaller==3.6
Related
I am using PyCharm on my MacBook to code and now I wanted to make a simple plot. When I run the file via the usual 'Run' command (do not know what it is called), it nicely shows my plots, but when I run the file in the Python console (which I find more convenient because you can access your variables) it does not show anything. On the other hand, when I just type it in the Python console afterwards, it does work.
I have read some things about backends and other 'solutions' as I am apparently not the only one with this issue. Mine says macosx and gives the command: "Backend MacOSX is interactive backend. Turning interactive mode on." after running the file in the Python console. I tried changing the backend:
import matplotlib
# matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
but that does not work (no plot pops up). And yes, I use plt.show() after my plotting section :)
I also tried with 'QtAgg' but then I get: "ImportError: Failed to import any qt binding"
Now I am completely new to this backends stuff (I think it has to do with this), so I could really use some clear directions on how I can solve this issue.
Thanks in advance!
I am not sure we can solve this bug with some adjustment. I think you need to fresh start. I suggest you to start a new clean venv and install a new matplotlib there.
There are questions referring to this same error message, but the crucial difference is that I am not (deliberately) using Qt. I don't actually know what it is, and I am trying to run my program on cmd, not through any IDE. I have been using matplotlib without any problems until just now, when i suddenly got the error message:
This application failed to start because it could not find or load the Qt platform plugin "windows"
in "".
Available platform plugins are: direct2d, minimal, offscreen, windows.
Reinstalling the application may fix this problem.
here is a sample program which produces this error, although I suspect it is a consequence of the package being used, not the program
import matplotlib.pyplot as plt
nums = [1,2,3,4]
labels = ["one","two","three","four"]
colours, one, two = plt.pie(nums, labels=labels, autopct='%1.1f%%')
plt.legend(colours, labels, loc="best")
plt.axis('equal')
plt.show()
again, I installed matplotlib years ago, and have been using it just fine until now. Any help would be greatly appreciated.
edit: python version 3.7.1, matplotlib version 3.1.1
Ok, I just fixed it by upgrading matplotlib to version 3.2.1
not sure why that worked but hey
The question of using matplotlib with MacOS is a tricky one which has already been thoroughly reviewed by a number of discussions (see below). The problem is the following:
using MacOS Mojave 10.14.3
using python 3.7.2 in a conda environment
using matplotlib 3.0.3
Here is the simplest code snippet I came up with which allows reproducing the issue:
from matplotlib import pyplot as plt
x = [1, 2, 3]
y = [1, 2, 3]
plt.plot(x, y)
plt.show()
This throws the following error:
2019-03-22 12:25:43.429 python3.7[22209:554135] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7f85866b9de0
2019-03-22 12:25:43.431 python3.7[22209:554135] \*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7f85866b9de0'
*** First throw call stack:([...])
libc++abi.dylib: terminating with uncaught exception of type NSException
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
The issue is documented here. One solution is to install the PyQt5 package to your Python installation and to add the following lines at the beginning of your script:
import matplotlib
matplotlib.use("Qt5Agg")
While this works perfectly well, I am wondering why other backends fail to provide similar behavior.
Indeed I tried using MacOSX backend :
import matplotlib
matplotlib.use('MACOSX')
Which yields to the error:
from matplotlib.backends import _macosx
ImportError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.
The issue is documented here, there and in plenty of other threads.
Two solutions came out :
install python.app(conda install python.app) and launch your script with pythonw instead of python
use TKAggbackend
Using the first one works well but I wonder:
why do we need to call pythonw instead of python ?
what exactly is the python.app package ?
how can we make this solution work using an IDE (let say PyCharm for instance) ?
As for the second one, it does "work" up to a certain point: when running matplotlib using TkAgg, the plot window is really buggy. Indeed, it often needs several clicks on the "zoom", "pan" or "home" buttons to get them to actually work. It really is a great pain to use it. I asked several colleagues or friends using matplotlib with TkAgg and they all have the same issue.
Does anyone know the reason for this state of fact? Or if there is any workaround to avoid this issue (apart from installing pyqt5)?
Using the first option is your best bet since you are already working with a virtual environment.
According to matplotlib, there are two variants of python:
A Framework build - Quite important for GUI stuffs in MacOXs
A regular build.
Matplotlib in this case would want to interact natively with OSX and for this, it needs the Framework build this is the reason why installing the python.app type of python is important.
More information can be gotten from Matplotlib FAQ.
Check this link for more about the need for a framework build python.
I'm going to make some assumptions. If they're wrong I apologize.
You installed Python with Anaconda.
Personally, I've never had any problems on mac with matplotlib. My setup is: Mojave, Python3.7.3 in a venv using the python built in module (python3 -m venv), and matplotlib 3.0.3.
I can't answer your question on how to fix your problem, but I'm kind of trying to answer your "is there any workaround" question. Personally, I've always had issues with using Anaconda/Spyder/Conda for Python. I've always felt installing it as its own binary/app on the system leads to the fewest errors.
Now, I'm not saying you have to download and install by hand though. I use homebrew and it saves me headaches everyday I assume (such as upgrading applications and packages). That's the "work around" I'd suggest. Because isn't installing via Anaconda/Spyder already a workaround to installing Python properly? I've always felt performing one work around requires more workarounds for full functionality. Such as having to specify the matplotlib backend when it should be detected by default.
Obviously, I'm a little biased against that tool and that may be reflected in this answer, so take it with a grain of salt. Even though Conda is a legitimate tool that I think is useful, I find it annoying having to use both pip and conda when conda doesn't contain the packages I want.
There are two things you can try.
You can read Matplotlib info page on that, https://matplotlib.org/3.1.0/faq/osx_framework.html,
The default python provided in (Ana)conda is not a framework build. However, a framework build can easily be installed, both in the main environment and in conda envs: install python.app (conda install python.app) and use pythonw rather than python.
And follow the instructions.
Or simply follow the error message you get when you try %matplotlib inline,
(...)
UnknownBackend: No event loop integration for 'inline'. Supported event loops are: qt, qt4, qt5, gtk, gtk2, gtk3, tk, wx, pyglet, glut, osx
I did %matplotlib osx and have plt.imshow(myimg) working just fine afterwards.
When running the code
import matplotlib.pyplot as plt
fig = plt.figure()
I get the error
How can I stop this message from appearing?
I have seen similar errors occurring in a program due to problems of compatibility of a new program with an old graphics driver. Right-click in the pythonw.exe and check the compatibility mode, see if there is something different checked there, such as "run in 256 colors" or "run in compatibility mode". Anything should be unchecked, so that you run the most recent version of the drivers. You might also try to reinstall your python distribution or the matplotlib library. Try using pip install --upgrade matplotlib.
I'm new to Python and matplotlib. A simple script I wrote is crashing and I was able to reproduce the crash with the following code:
import matplotlib.pyplot as plt
plt.figure(1)
plt.figure(2)
#plt.show()
The error is python.exe has stopped working. If I uncomment the plt.show(), it still crashes depending on the order I close the plots (no crash if 2 is closed first, crash if 1 is closed first). I'm using Windows 7, Python 3.4, and I installed the individual modules from www.lfd.uci.edu/~gohlke/pythonlibs/. Do I have something configured incorrectly or a misunderstanding of how to use matplotlib?
You need to set the TkAgg backend explicitly. With the following code, the problem is resolved.
import matplotlib
matplotlib.use("TkAgg")
from matplotlib import pyplot as plt
Note that setting the TkAgg backend after importing pyplot does not work either; it crashes too. You need to set it before importing pyplot.
I was having this issue, I thought it was some line in my code causing the bug, but in fact the very act of importing matplotlib.pyplot was killing my program. I solved it by first running it in verbose mode:
python -v [programname].py
This shows the last action the importer does before crashing. For me, the last line of this was:
import 'PyQt5' # <_frozen_importlib_external.SourceFileLoader object at 0x000001F8EC9C0908>
This suggested to me that the dependent library PyQt5 was causing issues, so I ran pip install PyQt5, and magically everything started working.
This could be issue with python 3.x
I have tried with python 2.7 on my windows machine and it works perfectly fine!
You can either downgrade your python to 2.7 or if you feel its too late to do why dont you give it a try to call close()
Import matplotlib
matplotlib.use('wxAgg')
Import matplotlib.pyplot as plt
# your scripts
plt.close('all')
I had a similar issue in OSX when I updated to Python 3.4. IDLE was also crashing and there was a warning telling me the version was unstable.
I solved it by following the prompts and updating the version of Tcl/Tk (8.5.9) - http://www.python.org/download/mac/tcltk .
For macOS, just make sure that
~/.matplotlib/matplotlibrc contains:
backend: MacOSX
You don't need the other backends unless you specifically want them. Alternatively, perhaps you can do:
import matplotlib
matplotlib.use("MacOSX")
though I have not tested that.