Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't enable pylab mode, when matplotlib is not importable #2681

Merged
merged 1 commit into from Feb 21, 2013

Conversation

tomspur
Copy link
Contributor

@tomspur tomspur commented Dec 14, 2012

On a headless server, matplotlib is a unnecessary dependency and the ipython
console will crash badly, when tring to enable %pylab there.
In that case, just don't enable %pylab and ask the user to install matplotlib.

Original bug report: https://bugzilla.redhat.com/show_bug.cgi?id=872176

except ImportError:
print("pylab mode doesn't work as matplotlib could not be found." + \
"\nIs it installed on the system?", file=sys.stderr)
return
shell = self.shell
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below, another error is catch and log a message to the console as well as showing the traceback.
Maybe we could conform to that and do the same, it would allow for better debugging guide the user when they have an error like that.
What do you think ?

Same for other file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, makes sense. Then the user directly sees that matplotlib could not be imported in the file IPython/core/pylabtools.py.

Changed locally. I have just some problems in the other file with logging...

@Carreau
Copy link
Member

Carreau commented Dec 14, 2012

Looks fine to me,

Just a small comment that have to be discussed.
I know that this is not really "user-friendly" to show tracebacks, but we do it when possible.

@@ -20,7 +20,8 @@
# Imports
#-----------------------------------------------------------------------------

from __future__ import absolute_import
from __future__ import absolute_import, print_function

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't use the print function here, there's a logger attached to instances of InteractiveShellApp, please use that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does log.warn write to the terminal that wrote the process and print to the actual frontend ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log.warn only writes to the terminal where the process was launched from, but has the advantaged that it can be easily redirected to a file, or also selectively disabled based on the loglevel that is set (which can also be set from the command line)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately 'TerminalInteractiveShell' object has no attribute 'log', so I'm unsure, how to get access to a logger in this file.
Changed in the other file through.

How is it possible to log from a TerminalInteractiveShell object?

@ellisonbg
Copy link
Member

Pinging the status of this one...

@tomspur
Copy link
Contributor Author

tomspur commented Jan 24, 2013

The remaining problem is, that I don't know how to log properly from a 'TerminalInteractiveShell' object.

See the comments above for the file: IPython/core/shellapp.py

@Carreau
Copy link
Member

Carreau commented Jan 24, 2013

TerminalInteractiveShell inherit from Configurable, and might maybe inherit from logging configurable.
But most Instances should inherit also from Application which itself have a log.warn/debug if I grep correctly through the sources.

@tomspur
Copy link
Contributor Author

tomspur commented Feb 3, 2013

TerminalInteractiveShell inherits from InteractiveShell, which inherits from SingletonConfigurable.
I updated the commit to use shell.log to show, that it doesn't have a log/warn attribute.

I have still no clue, how to properly log/warn the user, except directly printing to the console...

@minrk
Copy link
Member

minrk commented Feb 4, 2013

Typically in the Shell we use IPython.utils.warn.warn/info/error to present messages to the user (as seen throughout IPython.core). This is old, legacy code from before there was any logging, but it's still used a lot. One important difference between Application logging and those methods is that the user will see those methods, whereas a log message will always go to the terminal (i.e. user won't see it in the notebook or qtconsole, and it might be suppressed by the log level).

#shell.logger.log_write("pylab mode doesn't work as matplotlib could not be found." + \
# "\nIs it installed on the system?")
shell.showtraceback()
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all callers of this function able to handle a None return value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment!
When verifying this, I realized that enable_pylab is the only function that calls this function (modulo some tests).

I just undid this change again and catched the ImportError in the enable_pylab function instead.

On a headless server, matplotlib is a unnecessary dependency and the ipython
console will crash badly, when tring to enable %pylab there.
In that case, just don't enable %pylab and ask the user to install matplotlib.

Original bug report: https://bugzilla.redhat.com/show_bug.cgi?id=872176
@tomspur
Copy link
Contributor Author

tomspur commented Feb 20, 2013

As enable_pylab is the caller of pylab_activate, I catch the ImportError exception there and report to the user, when it failed to import matplotlib.

This looks now much nicer than the solution before.

except ImportError:
self.log.warn("pylab mode doesn't work as matplotlib could not be found." + \
"\nIs it installed on the system?")
self.shell.showtraceback()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to call showtraceback here but not in interactiveshell?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log could be suppressed so the traceback makes sense here (See the pull request and the explanation by @minrk).

Furthermore there is no IPython.utils.warn.showtraceback() to be called in the InteractiveShell and not showing a traceback there is conform with the rest of the code there.

@ellisonbg
Copy link
Member

This is much cleaner, I left one inline comment, but this is almost ready to go.

@ellisonbg
Copy link
Member

OK, thanks for the explanation. I am going to merge.

ellisonbg added a commit that referenced this pull request Feb 21, 2013
Don't enable pylab mode, when matplotlib is not importable
@ellisonbg ellisonbg merged commit df58e39 into ipython:master Feb 21, 2013
@tomspur tomspur deleted the remove_matplotlib_dep branch February 24, 2013 10:41
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
Don't enable pylab mode, when matplotlib is not importable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants