Weird issue when running Python code with data-science libraries - python

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.

Related

Create a mess I need to clean up, while trying to solve conflicts between Macbook Pro M1 and Tensorflow library

I have a 2021 Macbook pro M1.
It is a well recognized problem that it conflicts with Tensoflow library, see here or here; in particular this last one was exactly the issue I experienced: when I tried to import tensorflow in jupyter notebook via the command
import tensorflow as tf
then the message
The kernel appears to have died. It will restart automatically.
appeared. Then searching in the above linked discussions, I have the feeling that the suggestion given at some point, which points at this link, SEEMS to be useful.
FIRST QUESTION: is this a/the solution for the M1-Tensorflow conflict?
I say "it seems" since before trying that I have been into the kind of tornado of desperate attempts leading a beginner like me to search for hints all around the web and then copy-paste commands taken here and there into the Terminal without understanding them all properly.
On one hand it sounds dumb, I admit, on the other the cost of understanding everything goes well beyond my humble intentions of learning some ML.
So, the final result is that I have a complete mess in my computer; the old libraries like numpy don't work anymore (when I import them inside a Python3 page opened with jupyter notebook with the command import numpy as np, the message
ModuleNotFoundError: No module named 'numpy'
appears), then the pip command doesn't work, if I use the pip3 to install, nothing changes. I read somewhere to use a virtual enviroment, and I followed the instructions even if I wasn't really aware of what I was doing; I downloaded XCode, miniforge3...
Well, I guess that there is somebody out there who can relate with this.
SECOND PROBLEM: I would like to clean-up everything dealing with Python/pip/anaconda and so on and install everything from scratch, possibly following the above link to solve the M1-tensorflow conflict...if it is correct. How can I do that?
Can somebody help me, please? Thanks

Apps not popping up on macOS Big Sur 11.0.1

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"

Matplotlib image not coming up in Visual Studio Code on Mac

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.

Using Matplotlib & Abaqus 6.12

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.

Importing a module into python with a period in the path

I'm having trouble importing numpy into my script because of some path-name issues.
I'm running my script with python 2.7, rather than the default 2.6 on my server because I need some of the updates in the Collections module. I cant seem to import numpy like:
from numpy.random import poisson
So I am trying to use the python2.7 specific links to numpy on my server, which are installed in:
/opt/lib/python2.7/numpy
But this period in the path is really making this difficult. I cannot change the path in anyway.
I've found a similar problem here, but frankly the code just doesn't make enough sense to me for me to feel safe using it (plus several commenters seemed to suggest it was a bad idea.). If someone has another suggestion, or if you can give me a good explanation of the code there I would appreciate it.
Try setting PYTHONPATH to point to /opt/lib/python2.7, after which import numpy et cetera should pull libraries from there.
$ PYTHONPATH=/opt/lib/python2.7 python27 my_script.py

Categories