I'm switching a bit between Spyder and jupyter notebooks when I'm trying to learn some coding for data science. Therefore I'd like to find a way to tell whether a function is being called from one or the other so I can deactivate parts of the script that are meant for notebooks only. I thought something like the following would work to leave out the %matplotlib inline
part when I'm running the code from Spyder:
if __name__ != '__main__':
%matplotlib inline
print('Hello, jupyter')
else:
print('Hello, Spyder')
But __name__ = _main__
in both cases, and leaving %matplotlib inline
as is also raises an error suggestion in Spyder.
I've tested the suggestions here: How to check if you are in a Jupyter notebook. That works, but I am a bit puzzled since I'm running an IPython console in Spyder as well. Also, I'm hoping that some of you may have other suggestions!
Thank you!
Jupyter Notebook can show that documentation of the function you are calling. Press Shift+Tab to view the documentation. This is very helpful as you don't need to open the documentation website every single time.
Your first Jupyter Notebook will open in new tab — each notebook uses its own tab because you can open multiple notebooks simultaneously. If you switch back to the dashboard, you will see the new file Untitled. ipynb and you should see some green text that tells you your notebook is running.
Both ! and % allow you to run shell commands from a Jupyter notebook. % is provided by the IPython kernel and allows you to run "magic commands", many of which include well-known shell commands. ! , provided by Jupyter, allows shell commands to be run within cells.
shift + tab + tab.
There doesn't seemed be a correct or future-proof way to pull this off but I would use this pattern:
import os
if "JPY_PARENT_PID" in os.environ:
print('Hello, jupyter')
else:
print('Hello, Spyder')
It's a little bit more specific and has less side-effects than the answer given here. It works in jupyter notebook as well as jupyter lab so I think it is safe to assume that it will future-proofish for a while.
Let me know how it works for you.
The solution above only works in spyder >3.2
However the solution below should work for all versions of jupyter notebook or spyder, maybe. Basic it's the same if else loop from above but we just test the os.environ
for the presence of spyder stuff.
import os
# spyder_env: was derived in spyder 3.2.8 ipython console running:
# [i for i in os.environ if i[:3] == "SPY"]
spyder_env = set(['SPYDER_ARGS',
'SPY_EXTERNAL_INTERPRETER',
'SPY_UMR_ENABLED',
'SPY_UMR_VERBOSE',
'SPY_UMR_NAMELIST',
'SPY_RUN_LINES_O',
'SPY_PYLAB_O',
'SPY_BACKEND_O',
'SPY_AUTOLOAD_PYLAB_O',
'SPY_FORMAT_O',
'SPY_RESOLUTION_O',
'SPY_WIDTH_O',
'SPY_HEIGHT_O',
'SPY_USE_FILE_O',
'SPY_RUN_FILE_O',
'SPY_AUTOCALL_O',
'SPY_GREEDY_O',
'SPY_SYMPY_O',
'SPY_RUN_CYTHON',
'SPYDER_PARENT_DIR'])
# Customize to account for spyder plugins running in jupyter notebook/lab.
n = 0
if "JPY_PARENT_PID" in os.environ:
# Compare the current os.environ.keys() to the known spyder os.environ.
overlap = spyder_env & set(os.environ.keys())
if len(overlap) == n:
print('Hello, jupyter')
# This could be a more specific elif statment if needed.
else:
print('Hello, Spyder')
Does that solve the problem for you?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With