There are a few annoyances with WSL2. For me the most important are:
1. You can't connect to a port listening on WSL localhost like in WSL1, you have to figure out the WSL IP address and use that.
2. From WSL you can't connect to a Windows TCP port on localhost, you must figure out the Windows IP (cat /etc/resolv.conf) and use that.
3. The WSL remote interpreter on PyCharm is not working anymore. The suggested workaround is "use SSH remote interpreter" but given #1 you can't connect to localhost (and the WSL IP changes every time you restart it)
In order to use the SSH remote interpreter in my favorite IDE I'm using this script (on Windows):
import subprocess
import re
output = subprocess.check_output('wsl.exe ifconfig')
match = re.search(r"\sinet\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s", output.decode('utf-8'))
if match:
ip = match.group(1)
with open(r"C:\Windows\System32\drivers\etc\hosts") as i:
content = i.read()
new_content = re.sub(r"(172.\d+\.\d+\.\d+)\s+wsl", f"{ip} wsl", content, re.M)
if new_content != content:
try:
with open(r"C:\Windows\System32\drivers\etc\hosts", "w") as o:
o.write(new_content)
except PermissionError:
print(new_content)
First I use the Windows Scheduler to start SSH (wsl service start ssh) on logon. Then this script is executed on logon with elevated privileges to update the hosts file. Instead of "localhost", I use "wsl" and it works well enough for me while I wait for the fixes.
Despite the above, the combo Windows 10 + WSL2 is the best "Linux Desktop" for me.