Spyder - disable warnings inside Spyder Console - python-3.x

I use Spyder (4.1.5) as my python editor. When I run the code it shows a million workings that make it impossible for me to really focus on the output of the code. I read a lot of posts here on stackoverflow (see for example: Hide all warnings in ipython ) and it seems they all suggest to add
warnings.filterwarnings("ignore")
This for sure does not work.
For example, one of the many warnings printed to console many times is this one:
C:\...\Utils.py:2831: FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be deprecated, use a list instead.
However, I also have this line of code at the very beginning of the script:
warnings.filterwarnings("ignore", category=FutureWarning)
Still, no luck in disabling it.

import warnings
warnings.filterwarnings("ignore")
worked for me using Spyder 5 and Python 3.8.10

Related

plotnine: UserWarning: Starting a Matplotlib GUI outside of the main thread will likely fail

I am trying to use plotnine to generate some graphs. I import the required libraries:
from plotnine import *
from plotnine.data import mpg
And then, if I run the following code in PyCharm I get a Warning message, the window plot
shows a "No answer" message and I am forced to restart the python terminal:
(ggplot(mpg) # defining what data to use
+ aes(x='class') # defining what variable to use
+ geom_bar(size=20) # defining the type of plot to use
)
<ggplot: (150517199824)>
C:\Users\alvaromc317\miniconda3\envs\general\lib\site-packages\plotnine\ggplot.py:363: UserWarning: Starting a Matplotlib GUI outside of the main thread will likely fail.
However, If I start a python terminal from windows cmd terminal and run the same script as before, I get no error message and I see the plot with no problem.
What is happening and how do I get to use plotnine in pycharm?
In case it is needed, I work using a Windows 10 machine and python 3x based on miniconda.
I'm having a similar problem, but on MacOS using the commercial addition of PyCharm.
From this github issue and this JetBrains issue, it looks like it might be related to a PyCharm bug.

Pyside to build PyQT designer 4 UI file always results in error

Using Pyside-uic -o pythonfilename.py uifilename.ui to convert my PyQT4 designer GUI to python code. Seems to build just fine with no errors, but when I run the file, it consistently crashes, with type errors I can't solve.
First error gave me trouble because I set a "max size". When I removed the max size, the error changed to be a problem with the palette. I removed the palette settings, and replaced it with style sheets, and now it's giving me the error:
"TypeError: QWidget.setFont(QFont): argument 1 has unexpected type 'PySide.QtGui.QFont'"
I doubt there are this many problems with using Pyside to build PyQT UI files(because it seems to be a pretty standard way of doing it), so that leaves me to believe it's something i'm doing.
**Just a note with a bit more info.
This is software we've been using for some time, and the existing UI was built using this exact method. I inherited the file, and was asked to update the UI, and left with instructions on how to do it. I updated the existing ui file, saved it out, and ran the "Using Pyside-uic -o pythonfilename.py uifilename.ui" command. No errors from the build, but %100 of the time i've tried using this method, it has failed.
I tried googling the answer for hours, and proposed the question to other people.
I solved the issue. After pyside spits out the new python file (built from the UI file), theres a line of code at the beginning that will say something along the lines of "from PySide import QtCore, QtGui"
"PySide" has to be changed to "PyQt4".

How to suppress graphviz warnings in Python 3?

I'm using the graphviz module to render a network of nodes and links.
I use small circles as node's shape, so the labels are intentionally bigger than the nodes.
As a consequence, I get the following warning:
"Warning: node 'wave', graph 'N' size too small for label"
'Wave' is just an example of a node's label.
I get lots of this warnings because of the high quantity of nodes (screencapture).
So, my question is: How can I suppress warnings like those?
The graphviz command I'm using is:
n.view() # n is my digraph
I have already tried the suggestions from:
How to suppress a third-party warning using warnings.filterwarnings
How to redirect python warnings to a custom stream?
But nothing so far.
Thanks in advance.
Try Eli Bendersky's excellent page: Redirecting all kinds of stdout in Python
After replacing stdout with stderr, Eli's solution worked on graphviz for me.
If you happen to be using Evince, Ubuntu's builtin PDF viewer, see:
https://superuser.com/questions/980237/silence-evinces-warnings-in-ubuntu
For graphviz, as of v0.11, there is a silent option for the .draw() method (see graphviz docs:graphviz.view).
Apparently not on windows.
This applies to those using networkx (which uses pygraphviz) or pygraphviz.
In pygraphviz, the warnings are collected and then pushed using the warnings module (see pygraphviz/agraph.py:1390).
You can therefore silent the warnings specifically when drawing:
warnings docs:Temporarily Suppressing Warnings
import warnings
<create graph etc>
with warnings.catch_warnings():
warnings.simplefilter("ignore")
g.draw()

Can a python module be imported without installing

Is this possible that a module is imported without installing the same. If no then why is my spyder IDE is showing a warning on the line where it is written import nltk even when nltk is not installed
Spyder runs a static analysis on the code you have in the Editor to offer hints and errors about it.
Since the analysis is static, it means Spyder doesn't run your code to perform it. In your case it simply detects that you have a line like
import nltk
but that there's no other use or call to nltk (for example, with a code like nltk.pos_tag(tokens) or something like that).

Python colorama not working with input?

Finally got colorama working today, and it works excellent when printing strings, but I got the common error everyone seems to get when I attempted to use colorama with input.
Here's my code:
launch = input(Fore.GREEN + "Launch attack?(Y/N): ")
Screenshot of output:
I had this same issue (Python 3.5.4) and, just in case it is not too obvious for somebody else looking at this, you can always rely on the workaround of combining print / input calls where you previously had just an input call:
print(Fore.GREEN + "Launch attack?(Y/N): ", end='')
launch = input()
This should produce the exact same output as in your question, with no extra blank lines and with the code coloring working without the need of importing anything else.
The (small?) disadvantage is that you you will end up with two lines of code where you previously had just one.
On my system, input() works with colors if you add
import sphinx.quickstart
to your module.
So here is the full code.
from colorama import Fore
import colorama
import sphinx.quickstart
colorama.init()
launch = input(Fore.GREEN + "Launch attack? (Y/N): ")
(This leads to two questions:
Why does it not work in the first place?
What is the actual reason? – Someone might like to dive into the sphinx source code.)
N.B. if you run python via winpty from Git Bash, set convert.
colorama.init(convert=True)
Otherwise, you do not get color with the current versions.
To get rid of this problem in the starting of the code add
import os
os.system('cls')
This will clear the screen and hence clear all the external factors blocking the colorama in input.
This works 100% you just need to do it once in the starting of the program [or just before the first use of colorama with input] and after that use colorama in any creative way you want to.
I hope this will help all the creative minds trying to make their codes more colourful
just make sure about the 'autoreset' in init()
init(autoreset=True)

Resources