It is always risky to upgrade your operation system. It is likely you will encounter some compatibility issue. I took the risk to upgrade my macOS from Catalina to the newest Big Sur. After that, the display in the new OS looks pretty, but all my PyQt5 apps could not be launched in this new OS. The GUI window does not pop up as usual, and there is no error message showing in the terminal. I spent the whole day trying to figure out what makes this problem. I found the solution but in a weird way which I feel confused.
It turns out that the apps comes back to normal after I add the following three lines in the main script.
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
It seems to me the new OS has some compatibility issue with Qt5Agg back-end. But the strange thing is that this solution also works for one of the Pyqt5 app, where I don't use matplotlib at all.
The Python version I used is 3.8.4, and the PyQt5 version I have is 5.15.1.
I hope somebody could explain to me what happen under the hood that makes this solution work. Also I hope this temporary solution can help somebody with the same problem.
A reply to the PyQt mailing list pointed out that setting this env var works:
QT_MAC_WANTS_LAYER=1
Found via Is there any solution regarding to PyQt library doesn't work in Mac OS Big Sur? and https://forums.macrumors.com/threads/pyqt5-and-big-sur.2260773/?post=29243620#post-29243620
I can confirm that matplotlib.use('TkAgg') followed by matplotlib.use('Qt5Agg') makes things work for me, too. I whittled it down to this as also working:
# from matplotlib.backends import _tkagg
import _tkinter
import matplotlib.pyplot as plt
plt.figure()
So it's something about the compiled _tkinter module. Maybe the inputhook?
As #Eric said, just add the following on the very start of your code, before the PySide2 import:
import os
os.environ["QT_MAC_WANTS_LAYER"] = "1"
Then import PyQt5/PySide2.
I followed the solution here and downgraded to PyQt 5.13. This solved my issue and allowed my compiled apps to run on Big Sur.
pip install PyQt5==5.13
for me the suggested solution brought a crashes on a breackpoints in pycharm ... the only thing helped :
https://forums.macrumors.com/threads/pyqt5-and-big-sur.2260773/
all worked as a magic ...
hope QT will fix it soon
I am using macOS Big Sur Version 11.2.2
As suggested by Eric, enter the following line in the terminal before launching your application:
export QT_MAC_WANTS_LAYER=1
This fixed the issue for me!
Adding this to my python program worked for me
import os
os.environ["QT_MAC_WANTS_LAYER"] = "1"
Related
I use Python data science libraries like Pandas, Matplotlib, Numpy. I like working with these. And for ease of use, I also have Anaconda installed which makes creating environments a breeze. But today when running some matplotlib code I face a really weird issue that I didn't encounter ever before. This issue occurs right when I try to import Pyplot.
$ python
>>> import matplotlib.pyplot as plt
!▓KH═⌐-▌ª╚E├╚Hü⌠>┴┘UÜWF♀ε½░±-Sz;½¼öC¿Æ→$√
)ì⌠┐→⌐V¿U!└φÑ^~π┘╠⌡╗♦{ò►ßq╢ê<_│☺♀àV╣oY}ër╡╫╞╟lBîyû,πƒ=♂▒-Öδbùh♂RΣ}Ω¼╘zâ╫♫┘▀\φ→H
$Æ⌡«ε╖#{╤╧↕ƒÅ▄\.╔~s)╥τ♣E:,-Ut⌠↓R↑J¬τpg┐±`&╟╡▓ ▓û
TS1Å`a↑╚+╬╚?└D¿e£▀z¼i♥☼·s&⌐
When I try to import Pyplot I hear three dots (like morse code) and then this gibberish comes up and my program ends. I have never seen anything like this before.
It didn't give me any ModuleNotFoundError or any sort of errors at all. The program just literally stops and the gibberish is printed. I have copied exactly what gets printed.
I have encountered this issue with Matplotlib and also NLTK. Trying to import NLTK shows the same error. I don't know if this issue occurs with any other library but I can confirm it occurs with Matplotlib and NLTK.
I was thinking about re-installing Anaconda but was curious to know more about this really weird issue. I would also like to say that the issue is with my base (root) conda environment, NLTK doesn't have any issues in my other machine-learning environment. Any help is absolutely appreciated.
I'm running some basic code in the Visual Studio Code editor on MacOSX:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show()
...and can't seem to get the png/svg file image to come up after running this. This also doesn't stop executing and I have to manually terminate the process. However, if I run this directly in the Terminal (each line of code line for line) I get the resulting image. One work-around is to just save the file (plt.savefig('foo.png')). This seems to work - the image is saved in the specified file location. However, it would be good to just see the image come up after running the code.
When running matplotlib codes from the terminal, I experience the same kind of hanging of the application after saving the image to a file. In this case, one 'workaround' that has always worked for me is to turn off blocking. Basically alter your code in this way:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show(block=False)
input('press <ENTER> to continue')
It's not perfect, but the image is saved correctly and the application stops after you hit ENTER in the terminal. Hope this helps.
I am having a similar issue and I think it is a problem with the exact version of python that vs code is using to run the code.
For reference, I have vscode version 1.52.1 on a mac os Catalina. I installed python via anaconda and created a new environment for python 2.7 (tried with python 3.8 too). I open VSCode by calling code . from the folder I have my simple python codes saved in.
Like OP, I could reproduce the figure if I were to run the code from a python instance called from terminal but not from vscode.
MY SOLUTION:
From vscode, select as python interpreter the one found in /usr/bin/python not the one in ~/opt/anaconda3/env/python27/bin/python
But this is weird, because from a separate terminal window which python returns ~/opt/anaconda3/env/python27/bin/python. This suggests me (though I am no python expert) that there is an issue within vscode about linking the right interpreter with the libraries. Frankly being new to vscode, I find the need to pay attention to these details quite concerning.
I got the same problem, which was driving me nuts. The image was displayed when using Jupyter Notebooks, but not always when using VS Code. I just added one last line_ plt.show() - IMHO unnecessarily - but this worked well.
import matplotlib.pyplot as plt
import matplotlib.image as img
im = img.imread('myimage.png')
plt.imshow(im)
plt.show() # <- added this line and now the image shows every time!
I faced the same issue and here's what I did to solve it.
Since the code runs without any error but also does not generate any plot and needs to be terminated manually, it's difficult to figure out what's going on. I tried running python2.7 test.py
This works, plot is generated but python3 test.py does not work.
So, here's what you need to do -
Run, pip install matplotlib --upgrade to upgrade the matplotlib. This does not resolve the issue but now the error is printed.
"RuntimeError: Python is not installed as a framework" ......
So, finally, to solve the problem, refer to Working with Matplotlib on macOS
Since, I am using Anaconda, all I need to do is conda install python.app and then use pythonw to run all scripts. I hope you also find the solution to your particular case from the FAQ.
Overall, it's a Matplotlib problem, so upgrading (or reinstalling) and trying with different Python versions should get you going.
I'm currently making a plug-in for abaqus using python. I've searched high and low for a solution to this problem. Ive managed to install matplotlib 1.3.1 into abaqus and its relevent modules (Six, numpy, dateutil, pytz and pyparsing) by simply copying them into the abaqus site-packages folder. It seems Abaqus 6.12 has numpy 1.4 inbuilt but this needs to be updated to 1.5 or later...I simply deleted and replaced it with 1.8.0. I encountered the 'Tkinter' error and resolve it using the following:
import sys
sys.path.append("C:\Python26\Lib\lib-tk")
import Tkinter
import matplotlib.pyplot as plt
Now I can seem to use matplotlib. However whenever I use the plt.plot() function in my plug-in, abaqus freezes. When I shut abaqus down the plot opens soon after. Im not really sure what is going on here, has anyone else experienced this problem or have a potential solution??
I hope this is clear, please ask if I need to elaborate on anything else.
I updated my python distribution yesterday to EPD 7.3-2 (64-bit). I am working on a mac with snow leopard.
Now the plot device of matplotlib is broken in at least two ways:
the "save" button doesn't work and makes the terminal or ipython crash and
the only way to see the figure is to have it in front of you, there is no python figure icon in the dock.
I did my homework and these same problems were reported here and here.
I tried to follow the instructions to fix this given in here, but this is the error that I get:
$python install_pythonw.py `which python`/../..
/Library/Frameworks/EPD64.framework/Versions/Current/.Python does not exist; exiting.
Indeed, I looked at the given folder and I could not find a .Python file. I added a comment at the answer to this problem but so far no one has replied to it :( :(
Any idea of how to fix this?
thanks!
I have seen this problem a few times, and it seems to be a problem in some backends. Also, it doesn't seem normal that your session crashes after 4 or 5 plots. In particular, the MacOSX backend seems buggy.
As you installed the EPD, I think it's less likely that your installation is broken.
The solution seems to be using a different backend. You can try with ipython --pylab a few backends, try their features and see if the save button works. You can try the following:
ipython --pylab=wx
ipython --pylab=tk
ipython --pylab=osx
The last one is the option that you're probably using right now, so perhaps not the best. If you just call ipython --pylab, it will use the default backend from your ~/.matplotlib/matplotlibrc file. Once you find a working backend you can change the default by editing that file. Look for a line like this:
backend : MacOSX
(your version may have a different backend.) Just change that setting to WXAgg, TkAgg, or Qt4Agg. With the --pylab option the names are slightly different, they don't have the Agg part. My favourite backend for OSX is the Qt4Agg backend, but I don't think it ships with EPD and the save button also doesn't work! But either WXAgg or TkAgg should work fine.
Other ways of changing the backend in a script are:
import matplotlib
matplotlib.use('WXAgg')
or
matplotlib.rcParams['backend'] = 'WXAgg'
I'm not sure exactly what is going on under the hood, but here is my setup, example code, and problem:
setup:
snow leopard (10.6.8)
Python 2.7.2 (provide by EPD 7.1-2)
iPython 0.11 (provided by EPD 7.1-2)
matplotlib (provided by EPD 7.1-2)
example code:
import numpy as np
import pylab as pl
x=np.random.normal(size=(1000,))
pl.plot(x)
problem:
I can't use the standard Mac OS X shorcuts to access the window generated by the plot command.
For example, I can't Command-Tab to the window. Thus, if the window is behind some other window, I need to mouse over to it! Command-W doesn't close it.
Obviously, this is unacceptable. It seems like perhaps running Lion instead of Leopard might fix this, but i haven't upgraded yet. I feel like the problem has something to do with iPython generating windows that aren't fully Cocoa-aware in some sense, but I really know very little so I'm not particularly confident in this hypothesis.
Thus, any ideas on how to either resolve or get around this issue would be much appreciated.
From the description on the iPython page, it looks like Python uses Qt to generate
UI. This means that the windows it generates are definitely not Cocoa windows and will not act like them.
There's not likely to be an easy solution to this issue.
I experienced the same annoyance with my Anaconda installation of Python 2.7.10 on Mac OS X Yosemite 10.10.5. One solution I found was to change the backend to Mac OS X or Qt4Agg by creating a ~/.matplotlib/matplotlibrc file with the line:
backend: MacOSX
or
backend: Qt4Agg
Now I can easily get to the plot window with Application switcher using Command - Tab and close it with Command - W .