如何判断是否从 jupyter notebook 调用了一个函数?
How to tell whether a function is being called from a jupyter notebook or not?
当我尝试学习一些数据科学编码时,我在 Spyder 和 jupyter notebooks 之间切换了一下。因此,我想找到一种方法来判断一个函数是从一个还是另一个调用,这样我就可以停用脚本中仅供笔记本使用的部分。当我是 运行 来自 Spyder 的代码时,我认为像下面这样的东西可以省略 %matplotlib inline
部分:
if __name__ != '__main__':
%matplotlib inline
print('Hello, jupyter')
else:
print('Hello, Spyder')
但是在这两种情况下 __name__ = _main__
,并且保持 %matplotlib inline
不变也会在 Spyder 中引发错误建议。
我已经测试了这里的建议:。这行得通,但我有点困惑,因为我也是 Spyder 中的 运行 一个 IPython 控制台。另外,我希望你们中的一些人可能有其他建议!
谢谢!
似乎没有 正确的 或 future-proof 方法来实现这一点,但我会使用这种模式:
import os
if "JPY_PARENT_PID" in os.environ:
print('Hello, jupyter')
else:
print('Hello, Spyder')
它比给定的答案 更具体一些,side-effects 更少。它适用于 jupyter notebook 和 jupyter lab,所以我认为可以安全地假设它 future-proof 会持续一段时间。
告诉我它是如何为你工作的。
更新:
上面的解决方案只适用于spyder >3.2
但是,下面的解决方案可能适用于所有版本的 jupyter notebook 或 spyder。基本情况与上面的 if else 循环相同,但我们只是测试 os.environ
是否存在 spyder 东西。
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')
这能解决您的问题吗?
我想我找到了一个更简单的解决方案:
def test_ipkernel():
import sys
which = 'IS' if 'ipykernel_launcher.py' in sys.argv[0] else 'IS NOT'
msg = f'Code *{which}* running in Jupyter platform (notebook, lab, etc.)'
print(msg)
如果我运行 JuyterLab 中的函数,输出是:
Code *IS* running in Jupyter platform (notebook, lab, etc.)
在 VS Code 中,它是:
Code *IS NOT* running in Jupyter platform (notebook, lab, etc.)
当我尝试学习一些数据科学编码时,我在 Spyder 和 jupyter notebooks 之间切换了一下。因此,我想找到一种方法来判断一个函数是从一个还是另一个调用,这样我就可以停用脚本中仅供笔记本使用的部分。当我是 运行 来自 Spyder 的代码时,我认为像下面这样的东西可以省略 %matplotlib inline
部分:
if __name__ != '__main__':
%matplotlib inline
print('Hello, jupyter')
else:
print('Hello, Spyder')
但是在这两种情况下 __name__ = _main__
,并且保持 %matplotlib inline
不变也会在 Spyder 中引发错误建议。
我已经测试了这里的建议:
谢谢!
似乎没有 正确的 或 future-proof 方法来实现这一点,但我会使用这种模式:
import os
if "JPY_PARENT_PID" in os.environ:
print('Hello, jupyter')
else:
print('Hello, Spyder')
它比给定的答案
告诉我它是如何为你工作的。
更新:
上面的解决方案只适用于spyder >3.2
但是,下面的解决方案可能适用于所有版本的 jupyter notebook 或 spyder。基本情况与上面的 if else 循环相同,但我们只是测试 os.environ
是否存在 spyder 东西。
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')
这能解决您的问题吗?
我想我找到了一个更简单的解决方案:
def test_ipkernel():
import sys
which = 'IS' if 'ipykernel_launcher.py' in sys.argv[0] else 'IS NOT'
msg = f'Code *{which}* running in Jupyter platform (notebook, lab, etc.)'
print(msg)
如果我运行 JuyterLab 中的函数,输出是:
Code *IS* running in Jupyter platform (notebook, lab, etc.)
在 VS Code 中,它是:
Code *IS NOT* running in Jupyter platform (notebook, lab, etc.)