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

init_session() on ipython startup file a different mainloop #11419

Closed
elvis-sik opened this issue Jul 21, 2016 · 5 comments · Fixed by #14443
Closed

init_session() on ipython startup file a different mainloop #11419

elvis-sik opened this issue Jul 21, 2016 · 5 comments · Fixed by #14443

Comments

@elvis-sik
Copy link
Contributor

I am trying to have a startup script for an IPython profile that calls init_session(). It seems doing this starts a different IPython mainloop. I can exit it with C-d and then get to the IPython prompt.

I think this has to do with wrongly assuming we are not inside IPython during startup:

/home/elvis/miniconda3/envs/sci/lib/python3.5/site-packages/sympy/interactive/session.py in init_session(ipython, pretty_print, order, use_unicode, use_latex, quiet, auto_symbols, auto_int_to_Integer, str_printer, pretty_printer, latex_printer, argv)
    472     if not in_ipython:
    473         mainloop(message)
--> 474         sys.exit('Exiting ...')
    475     else:
    476         ip.write(message)

SystemExit: Exiting ...
@asmeurer
Copy link
Member

It must be from this line. Why is that there? It causes the check below to never be run.

@asmeurer
Copy link
Member

Oh nevermind, I misread the next line (confused the in_ipython and ipython variables).

@asmeurer
Copy link
Member

Is the problem here? It assumes that get_ipython() is defined when we are in IPython. Maybe that isn't the case for your scenario?

@elvis-sik
Copy link
Contributor Author

It seems this is indeed the case. I did some pdb investigation and that seems to be it. Also, it seems to be expected behavior for IPython: ipython/ipython#9791

If you have a script ex.py with just a single line: get_ipython() and you are inside IPython and you call run ex.py, it will go smoothly. If, rather, you are calling from the command line: $ ipython ex.py it will throw a NameError.

This is expected behavior for them. When we call code inside the IPython REPL, the code is called with all the variables that are defined in scope. When we are not inside it, such as when it is executing the startup script, they are not.

The fact that this had not come up yet suggests to me that it is, at most, a very uncommon issue. Also, reading the source of init_session it is clear that it can be replaced by a few lines of code on the startup script. So I think this is an acceptable workaround.

If what I say sounds reasonable to you, I think this can be closed.

@elvis-sik
Copy link
Contributor Author

If you feel this should be fixed, they suggested a method relying only on public API that should work even in situations in which get_ipython has not been added to the builtins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants