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

Export to .py should add 'import's #54

Closed
InonS opened this issue Jul 3, 2015 · 5 comments
Closed

Export to .py should add 'import's #54

InonS opened this issue Jul 3, 2015 · 5 comments
Milestone

Comments

@InonS
Copy link

InonS commented Jul 3, 2015

The .ipnb to .py translation works wonderfully, except that some imports are taken for granted when working in an IPython notebook. For the exported .py script to work properly, the packages and modules used should be explicitly 'imported' at the top of the .py file.

I'm pretty sure this issue has to do with Jupyter more than IPython, seeing that nbconvert under the IPython repo has been inactive for a couple of years

@Carreau
Copy link
Member

Carreau commented Jul 3, 2015

There is no hidden import in notebook, nor in terminal. Do you have a custom config ?
What import do you have in mind ?

(the ipython/nbconvert is indeed legacy code with have for historical reasons)

@InonS
Copy link
Author

InonS commented Jul 3, 2015

Hi Matthias!

Thanks for your response.
I had to add the following, after giving up on trying to use IPython
magics from inside my python script (in particular, my notebook had
"%pylab inline"):

import cProfile import datetime from functools import reduce import itertools import math import pstats, io from subprocess import check_output, call import sys import time import warnings import cairocffi

from IPython.utils.encoding import DEFAULT_ENCODING
from scipy.optimize import curve_fit
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np

The first 10 or so are definitely core python packages (I admit that
some of these I loaded by hand, e.g. cProfile). The bottom 5 are
probably loaded by the %pylab magic.
Are none of the imports above loaded implicitly??

Thanks,

Inon Sharony
ינון שרוני
+972(3)6407634
http://www.TAU.ac.IL/~inonshar
Please consider your environmental responsibility before printing this e-mail.

Quoting Matthias Bussonnier notifications@github.com:

There is no hidden import in notebook, nor in terminal. Do you have
a custom config ?
What import do you have in mind ?

(the ipython/nbconvert is indeed legacy code with have for
historical reasons)


Reply to this email directly or view it on GitHub:
#54 (comment)

@Carreau
Copy link
Member

Carreau commented Jul 3, 2015

Well, no, none of the above packages are imported implicitely by default.

you import them by doing %pylab inline, and this is one of the reason %pylab is undocumented and deprecated. %pylab is not ment to do inline graph. %matplotlib inline is ment to do that. If you do not know what %pylab does, then you probably should not use it, or it will bite you in other places.

Magics are non python syntax, so no magic will ever be valid in Python script.
Though, most magics are "just" syntactic sugar for Python expression, that hist -t can show you:

In [1]: %matplotlib inline
UsageError: Invalid GUI request 'inline', valid ones are: wx, osx, qt, qt5, glut, gtk, tk, pyglet, gtk3
In [2]: hist -t
get_ipython().magic('matplotlib inline')
get_ipython().magic('hist -t')

It does not really help you though as most of the magic need access to a ipython shell to work, but can at least make the syntax valid.

Feel free to continue asking if you have more questions,
closing the bug for now.

@Carreau Carreau closed this as completed Jul 3, 2015
@InonS
Copy link
Author

InonS commented Jul 3, 2015

Wow, Matthias, thanks for a wonderful explanation!
I really appreciate that you took the time. I'm sure someone will
probably benefit from it in the future (all user errors reproduce
after a while... :-)
I was just a bit thrown off by [the
  documentation explaining how to embed](https://ipython.org/ipython-doc/dev/interactive/reference.html) an instance of IPython
in a regular python script, so I thought I might do that. Since
invoking get_ipython() did not work for me after a reasonable
effort, I decided to give up on IPython for non-interactive
computations. 
As for *pylab*, I'll heed your advice and switch to *%matplotlib inline*
for my notebook plotting. Again, I just took existing tutorials and
such which almost always begin with *%pylab inline*. I'll have to
figure out which of the other imports I'll have to do explicitly in
the notebook.
IMHO all of the points you discussed are worth mentioning in some
highly visible place on the project website and/or documentation, to
save people the trouble of figuring these things out the hard way.

Thank you, again, 
and have a wonderful weekend!

@Carreau
Copy link
Member

Carreau commented Jul 4, 2015

Here is (roughly) what %pylab does

import numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot
from IPython.core.pylabtools import figsize, getfigs
from pylab import *
from numpy import *

The problem with writing loud and clear what not to do, is that people will not remember why it's wrong and what the issues are, they will just remember they can do it.

@takluyver takluyver modified the milestone: 4.0 Jul 30, 2015
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

No branches or pull requests

3 participants