为什么bash(WSL)使用w3m作为默认浏览器?

时间:2016-12-31 01:03:58

标签: python bash windows-10 python-webbrowser w3m

Windows 10上的Ubuntu上的Bash使用w3m打开终端内的网址,而不是打开Chrome或Edge。如何启动Windows默认Web浏览器?

例如,以下代码

webbrowser.open_new("http://www.youtube.com/watch?v=dQw4w9WgXcQ")

看起来像这样:

webpage displayed as text in terminal

这与使用

打开页面完全相同
w3m http://www.youtube.com/watch?v=dQw4w9WgXcQ

这是一个全新的Win 10安装,在我启用了Bash(通过适用于Linux的Windows子系统)并完成以下操作后:

$ sudo apt update && sudo apt upgrade && sudo apt dist-upgrade
$ sudo apt install build-essential
$ sudo apt install python-pip python-dev
$ sudo pip install --upgrade pip

然而

print webbrowser._tryorder

给出

['www-browser', 'w3m']

www-browser只是w3m的另一个别名,为什么windows-default未列出?

以前,相同的Python 2.7代码在运行Git-Bash的32位Windows 10机器上运行正常,所以我认为python方面没有任何问题。

1 个答案:

答案 0 :(得分:8)

简易修复:

如果您使用当前Windows Insider预览慢速通道构建(#14986)附带的Ubuntu 16.04运行最新的WSL,则可以使用这种简单的解决方法。您不必在后台运行xserver,并且可以启动所选的Windows浏览器!

您需要做的就是将以下几行添加到~/.bashrc file中(以下列出的Chrome默认安装位置为例。您可以随意将地址添加到所需的浏览器中):

export DISPLAY=:0
export BROWSER=/mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe

注意:在处理空格或()等地址中的复杂字符时,必须使用“\”转义它们。

更难(Hackier)修复:

基本上,Windows子系统Linux(WSL)不正式支持基于GUI的程序。但是,有一种解决方法可以使用xserver从WSL运行一些基于GUI的Linux程序,请参见:How to Run Graphical Linux Desktop Applications from Windows 10’s Bash Shell

如果这对您不起作用,您可能还需要设置BROWSER环境变量,这是我在发现GUI hack时尝试做的事情,请参见此处:Google Earth and $BROWSER environment variable

注意:

即使这个“有效”,它会在启动时抛出一些错误(例如下面的例子),只需等待,很快就会在xserver窗口中启动。对于我的一个python项目,它停止了执行。为了解决这个问题,我可能会写一些错误处理。我仍然有兴趣听听有人在旧的Windows 10系统上有一个更优雅的解决方案。

Nathaniel@DESKTOP-NAGL0DJ:~$ libkmod: ERROR ../libkmod/libkmod-module.c:1619 kmod_module_new_from_loaded: could not open /proc/modules: No such file or directory
Error: could not get list of modules: No such file or directory
[144:144:0103/123322:FATAL:render_sandbox_host_linux.cc(40)] Check failed: 0 == shutdown(renderer_socket_, SHUT_RD). shutdown: Invalid argument
#0 0x7f41e1522a2e base::debug::StackTrace::StackTrace()
#1 0x7f41e153cf87 logging::LogMessage::~LogMessage()
#2 0x7f41e153d1a9 logging::ErrnoLogMessage::~ErrnoLogMessage()
#3 0x7f41df5ace45 content::RenderSandboxHostLinux::Init()
#4 0x7f41df36c65a content::BrowserMainLoop::EarlyInitialization()
#5 0x7f41df36f7ff <unknown>
#6 0x7f41df368e1d content::BrowserMain()
#7 0x7f41df2d73d4 <unknown>
#8 0x7f41df2d6611 content::ContentMain()
#9 0x7f41e1cf98e8 <unknown>
#10 0x7f41d4681f45 __libc_start_main
#11 0x7f41e1cf97ba <unknown>

Aborted (core dumped)
Sandbox: unexpected multithreading found; this prevents using namespace sandboxing.

(firefox:30): GConf-WARNING **: Client failed to connect to the D-BUS daemon:
Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.