在run运行模式下可以正常跑,但是不能debug
报错如下:
C:\Windows\system32\wsl.exe
....
Could not connect to 172.17.192.1: 49429
Traceback (most recent call last):
File "/mnt/d/JetBrains/PyCharm 2021.2.3/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_comm.py", line 458, in start_client
s.connect((host, port))
socket.timeout: timed out
Traceback (most recent call last):
File "/mnt/d/JetBrains/PyCharm 2021.2.3/plugins/python/helpers/pydev/pydevd.py", line 2173, in <module>
main()
File "/mnt/d/JetBrains/PyCharm 2021.2.3/plugins/python/helpers/pydev/pydevd.py", line 2055, in main
dispatcher.connect(host, port)
File "/mnt/d/JetBrains/PyCharm 2021.2.3/plugins/python/helpers/pydev/pydevd.py", line 1826, in connect
self.client = start_client(self.host, self.port)
File "/mnt/d/JetBrains/PyCharm 2021.2.3/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_comm.py", line 458, in start_client
s.connect((host, port))
socket.timeout: timed out
Process finished with exit code 1
原因:win10没有打开防火墙
解决方式:
reference:
python - WSL 2 : Pycharm debugger connection time out - Stack Overflow
https://youtrack.jetbrains.com/issue/PY-36563#focus=streamItem-27-3745830.0-0
Run PowerShell as Administrator, and do (assume that Ubuntu1804 is your WSL2 distro):
PS C:\WINDOWS\system32> $myIp = (Ubuntu1804 run "cat /etc/resolv.conf | grep nameserver | cut -d' ' -f2")
PS C:\WINDOWS\system32> $myIp
172.21.0.1
PS C:\WINDOWS\system32> New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -LocalAddress $myIp -Action AllowSee https://github.com/microsoft/WSL/issues/4585
See Add "allow" rule to Windows firewall for WSL2 network · Issue #4585 · microsoft/WSL · GitHub