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

标签 python bash windows-10 python-webbrowser w3m

Windows 10 上 Ubuntu 上的 Bash 使用 w3m在终端内打开 URL 而不是打开 Chrome 或 Edge。我怎样才能让它启动 Windows 默认网络浏览器?

例如下面的代码

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 方面没有任何问题。

最佳答案

轻松修复:

如果您使用当前 Windows Insider Preview 慢速 channel 版本 (#14986) 附带的 Ubuntu 16.04 运行最新的 WSL,则此简单的解决方法有效。您不必在后台运行 xserver,您可以启动您选择的 Windows 浏览器!

您需要做的就是将以下行添加到您的 ~/.bashrc 文件(下面列出了 Chrome 的默认安装位置作为示例。请随意将地址放入您想要的浏览器):

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

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

更难(Hackier)修复:

基本上,适用于 Linux 的 Windows 子系统 (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.

关于python - 为什么 bash (WSL) 使用 w3m 作为默认浏览器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41404536/

相关文章:

python - 如何在python中预处理非常大的数据

python - 自定义模板中的 Django ManyToManyField?

linux - Bash 脚本 - 根据模板转换时间

sql - 如何在 bash 中将二进制数据插入到 sqlite3 数据库中?

cygwin - 在 Windows 10 Python 3 上安装 Fasttext 时出错

python - 将我的 CSV 与日志文件 : Length of values does not match length of index using pandas 相匹配

python - int() 与 .astype ('int' ) Python

狂欢 : Piping Find into Grep

visual-studio - 构建示例驱动程序时出错:找不到与WDK版本“8.1”相对应的SDK

git: 'remote-ttps' 不是 git 命令