使用选定的浏览器启动 IPython 笔记本

2022-01-18 00:00:00 python windows subprocess ipython

问题描述

我正在尝试使用非默认浏览器(在我的情况下为 Firefox)启动 IPython并认为我可以复制 in this blog

I am trying to start IPython with a non default browser (in my case Firefox) and thought I could replicate the replicate the script given in this blog

我使用的是 Windows 7

我把下面的代码放在一个文件中,比如module.py"

I put the following code in a file say "module.py"

import subprocess
subprocess.call("ipython notebook --no-browser", shell=True)
subprocess.call([r'C:Program Files (x86)Mozilla FirefoxFirefox.exe', '-new-tab', 'http://127.0.0.1:8888/'])

但是当我从命令行运行它时

However when I run it from the command line

 python C:UsersmugabalDesktopmodule1.py

它执行第一行但不执行第二行(两行单独工作正常)

It execute the first line but not the second one (both lines work fine individually)

我的问题(更笼统地说)如何启动一个进程并告诉它不要劫持控制台窗口?

My question (in a more general term) how can I launch a process and tell it not to highjack the console window?

如果我监督了一个明显的解释,我提前道歉,但我查看了子流程文档和这个平台

I apologize in advance if I have overseen an obvious explanation but I looked both in the subprocess documentation and on this platform

----- 更新 -----

我应该补充一点,我尝试使用选定的浏览器启动 IPython,但不知道如何让它工作

I should have added that I tried to launch IPython with selected browser but could not figure out how to get it work

>ipython notebook --browser='C:Program Files (x86)Mozilla FirefoxFirefox.exe'
... 
[NotebookApp] The IPython Notebook is running at: http://127.0.0.1:8888/
...
**[NotebookApp] No web browser found: could not locate runnable browser.**

确切地说,Windows 命令提示符窗口中的以下命令按预期工作:

To be precise, the following command in a Windows command prompt window works as expected:

start firefox 

但是

ipython notebook --browser=firefox 

不起作用(与上述相同的错误).

does not work (same error as above).


解决方案

我在 windows 上遇到了同样的问题,然后就这样解决了:

I had the same problem on windows and got it work this way:

  • 使用命令创建配置文件ipython配置文件创建默认

编辑 ipython_notebook_config.py 文件,搜索行

Edit ipython_notebook_config.py file, search for line

#c.NotebookApp.browser =''

替换成

import webbrowser
webbrowser.register('firefox', None, webbrowser.GenericBrowser('C:\Program Files (x86)\Mozilla Firefox\firefox.exe'))
c.NotebookApp.browser = 'firefox'

那么它对我有用.

希望对你有所帮助.

JPG

相关文章