gpt4 book ai didi

ipython - 从 Python 脚本中启动 IPython 内核

转载 作者:行者123 更新时间:2023-12-04 12:29:04 27 4
gpt4 key购买 nike

假设我有以下文件 t.py :

from IPython import get_ipython

if __name__ == '__main__':
ip = get_ipython()
if ip is not None:
print('Found IPython')
这是我运行它时会发生的情况,同时使用 PythonIPython :
% python t.py
% ipython t.py
Found IPython
请注意,仅当使用 ipython 运行它时, 是 get_ipython()没有。
有没有办法从脚本中启动 IPython 内核,这样即使我以 python t.py 运行它,然后 get_ipython()不会是 None?

最佳答案

IPython 启动新的交互式 shell,即您的代码将被挂起,直到 IPython shell 终止。
您可以在单独的文件中使用启动器,launcher.py :

import IPython

if __name__ == '__main__':
IPython.start_ipython(['t.py'])
% python launcher.py
Found IPython
有关嵌入 IPython 的其他选项,请参阅文档 https://ipython.readthedocs.io/en/stable/interactive/reference.html#embedding-ipython

关于ipython - 从 Python 脚本中启动 IPython 内核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68072937/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com