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
get_ipython() called from startup script #9791
Comments
ip is injected in the main namespace, though a module is executed by python in it's own namespace we don't have control over. You can check for
It's, of course, private API (because of leading underscore). |
I think we do inject BTW, it is possible to check for an initialised TIS using public APIs: if TerminalInteractiveShell.initialized(): # Bool - True if it's instantiated
ipshell = TerminalInteractiveShell.instance()
|
@takluyver I notice if I simply run from from the command line When we are in a REPL, though, IPython's inclusions should be in scope. This is in contrast with the previous situation. In one case, It seems to me that scripts executed during startup are in a grey area in which we are not yet in a REPL, leading us to expect that But I do not know, just sharing my thoughts. |
I'd be inclined to say that |
I'm more incline to think that even in the repl, getting the current shell should be something like
but I wouldn't be against making things consistent. |
I think we've told people for quite a long time that the existence of |
I've just stepped on this very problem in my code... I have the following in a module I import: try:
# IPython 0.11 and above
_ip = get_ipython()
except NameError:
# IPython 0.10 and below (or normal Python shell)
_ip = __builtins__.get('__IPYTHON__') The problem was picked up with the Maybe I should just drop support for IPython 0.10 and below :-) I mainly use the shell instance for adding tab completion hooks but the latest interface for that is much more convenient. |
This makes get_ipython() available as a builtin when startup files run. Closes ipythongh-9791
#9818 should fix this. |
You can always get the current IPython without relying on any modification of builtins by importing from IPython import get_ipython
ip = get_ipython() This |
Hi, this is maybe more me asking for help than an issue per se.
Inside the startup folder of some IPython profile, I have the following script:
This module
weird
is elsewhere, and it is importable. It is merely:get_ipython()
What happens is: the call to
get_ipython
inside the print statement in the body of the file inside the startup script goes smoothly. However,get_ipython
is not visible inside the moduleweird
. So it throws aNameError
there.Now, of course, after this phase of startup-file loading, I get a traceback and then an IPython REPL. There, I can
import weird
just fine.There is code relying on
get_ipython
to know if it is being runned inside IPython. If I call this code inside my startup script, strange results happen. See here sympy/sympy#11419If this behaviour is not considered a bug, maybe we could find some workaround.
The text was updated successfully, but these errors were encountered: