Python3 在 WSL 2 上需要很长时间(超过 6 分钟)才能导入密钥环

Python3 on WSL 2 takes forever (over 6 minutes) to import keyring

我在 WSL2 Ubuntu-20.04 机器上尝试 运行 pip 命令时注意到这个问题,运行 任何东西都需要很长时间.我最终将范围缩小到 pip 导入 keyring 模块的时间。

Python 3.8.5 (default, May 27 2021, 13:30:53)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> def import_keyring():
...     time_start = time.time()
...     import keyring
...     print(f"Keyring took {time.time() - time_start} seconds to load")
...
>>> import_keyring()
Keyring took 400.4930064678192 seconds to load

查看进程资源管理器,似乎有一个正在等待的子进程:

 dbus-launch --autolaunch <32 character hex string> --binary-syntax --close-stderr

dbus-launch命令本身似乎没有任何子进程,所以我用strace看它挂在哪里:

connect(3, {sa_family=AF_INET, sin_port=htons(6000), sin_addr=inet_addr("<my main machine's IP>")}, 16) = 0

它看起来试图连接到主机 windows 计算机上的端口 6000 但失败了?现在我只是不知道发生了什么,也不知道为什么要花这么长时间才能失败。任何帮助表示赞赏!

当我最终点击端口 6000 是什么意思时,我已经准备好了这个问题。很久以前,我尝试在 Windows 上设置 X 服务器供 WSL2 使用。

要么让 X 服务器正常工作,要么取消设置你的 DISPLAY 环境变量,这应该会立即清除它。

export DISPLAY= 

希望这可以帮助其他人 运行 解决这个晦涩的问题!