从ipzope调用时如何将类路径传递到ipython的笔记本?

2024-07-01 08:04:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用ipzope(buildout)中的iypthon有一段时间了,它可以正常工作。在

现在我试着用ipython的笔记本,但我无法正确设置。在

当我创建一个新的笔记本时,它用一个ImportError停止IPython的内核(见下文)。 我猜IPython.html.notebook.start()中创建的线程打开webbrowser,而没有从调用进程传递{}。在

我的解决方法是将ipzope中的路径添加到PYTHONPATH。在

当我添加所有路径时,ipython的笔记本就完美地工作了,我可以调试和操作Plone。在

如果我只在ipythonpyzmqJinjatornado添加到PYTHONPATHipython的笔记本可以工作,但它不能访问ipzope变量(apputils等)

问题:有什么提示可以在不使用^{的情况下将路径传递到ipython的笔记本?在

我从ipzope开始使用ipython的笔记本

sys.argv[1:1] = "notebook --ip=192.168.45.135 --profile=zope".split()

重要的是:

^{pr2}$

Tags: 路径htmlipythonbuildout笔记本线程内核start
1条回答
网友
1楼 · 发布于 2024-07-01 08:04:05

我引用Min RKIPython developer

"When you start plain terminal IPython, the shell is created in the same process. That means that IPython executes in the same context as the changes you have made to sys.path, so the changes have the desired effect. In the notebook, only the notebook server exists in that context. Kernels are started as subprocesses, and thus recreate sys.path at startup, following the standard procedure for a Python process, and do not inherit any runtime changes to sys.path that may have happened in parent processes." (https://github.com/ipython/ipython/issues/5420)

我的解决方案

因此,将路径传递到notebook的唯一方法是通过PYTHONPATH。在

我现在的解决方法是在脚本中设置os.environ['PYTHONPATH'] = ':'.join(sys.path)。这样,您就不需要弄乱系统的PYTHONPATH(如果有的话),并确保将所有必要的路径传递给内核。在

os.environ作为Popenenv参数传递给launch_kernel.../ipython-1.2.1-py2.7.egg/IPython/kernel/launcher.py)中的子进程。在

如果您需要使用ipython的笔记本并希望buildout生成脚本,请将以下内容添加到您的构建中(例如在.../Plone-4.3.2/zeocluster/develop.cfg中)

parts +=
...
    ipzopenb
...
[ipzopenb]
recipe = zc.recipe.egg
eggs =
    ipython
    pyzmq
    tornado
    Jinja2
#following for nbconvert
    Pygments
    Sphinx
    ${client2:eggs}
initialization =
    import sys, os
    os.environ["INSTANCE_HOME"] = "${client2:location}"
    os.environ['PYTHONPATH'] = ':'.join(sys.path)
    sys.argv[1:1] = "notebook  ip=192.168.45.135  profile=zope".split()
scripts = ipython=ipzopenb
...
[versions]
Jinja2 = 2.7.2
Pygments = 1.6
Sphinx = 1.2.2

相关问题 更多 >

    热门问题