Monday, April 18, 2022

Matploblib does not show interactive plots on SciView in PyCharm

I encounter a problem when I plot an interactive plot using Matplotlib in PyCharm. The problem is that it does not show the interactive plot on SciView as it usually does. Although it could be due to the idiomatic way of  my creating python plot, I look for a "cheap" way to fix this since the code successfully creates interactive plots when I run it fron the console outside of PyCharm. The solution tends out to be use a different Matploblib backend. 

In PyCharm's python console, we can determine which Matploblib backend is currently using, e.g.,


>>> import matplotlib
>>> matplotlib.get_backend()
'module://backend_interagg'

>>>

In an OS console, we can determine which Matploblib backend is currently using, e.g.,


>>> import matplotlib
>>> matplotlib.get_backend()
'TkAgg'

>>>

Based on the observations in the above, the solution is to set the Matplotlib backend as the one used by Matplotlib on the OS console, e.g.,



import matplotlib


if __name__ == '__main__':
    matplotlib.use('TkAgg')
    # ...
    # more code ...
    # ...



Saturday, April 16, 2022

Setting up X2Go

If you wish to have Windows Remote Desktop-like experience when we access a Linux hosts, X2GO is probably the best solution. X2GO runs on the NX or NoMachine protocol. Contrast to the solution that runs a X server on a Linux host, and run a client application on another host, we can leave a session and resume it with NX or X2GO, which offers the experience similar to Windows Remote Desktop. 

Below is a "minimal" setup for a Fedora Linux server and a Windows client. We call it a "minimal" setup because we do not install a full Linux desktop, rather, we install a graphical terminal like xterm. 

  1. Install the X2GO server on Fedora Linux
    
    sudo dnf install x2goserver x2goserver-xsessions xterm
        
  2. Install the X2GO client on Windows. For this, we download the client installation package, and run the installation package.
  3. Now we can use X2GO -- nothing needs to be done on the Linux server
    1. Start the X2GO client on the Windows host
    2. Create a new sessiono if we have not done so
    3. When creating the new session, we specify the Linux host we connect to and the user name we log on
    4. It is important to note that we should select the "Session Type" as "Single Application" and enter the command as "/usr/bin/xterm" since we did not install a full Linux Deskop and only installed xterm
    5. To connect to the Linux server and run xterm, we open the session.
    6. We can close the session. Next time when we open it, it resumes from where we close it

Friday, April 8, 2022

SSH cannot connect to any non-local SSH servers on WSL 2

Recently, I encountered a strange problem. That is, I cannot connect to any non-local SSH servers from a WSL 2 terminal using the ssh client from WSL 2. However, I would not have any problem to connect to these SSH servers using ssh.exe from Windows and PuTTY or other SSH clients on Windows. Here is what I observe when I try to connect Github.com,


ssh -vvv -T ssh@github.com
OpenSSH_7.9p1 Debian-10+deb10u2, OpenSSL 1.1.1d  10 Sep 2019
debug1: Reading configuration data /home/hui/.ssh/config
debug1: /home/hui/.ssh/config line 17: Applying options for github.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "github.com" port 22
debug2: ssh_connect_direct
debug1: Connecting to github.com [140.82.113.3] port 22.

The process hangs on "Connecting to ..." until times out. I attempted all of the methods discussed in this threads and the method of adding a Firewall rule for WSL, but to no avail in my case. The solution finally works for me is to restart the Windows Container Networking service.

Using PowerShell, we can restart the service on command line as follows,


PS C:\WINDOWS\system32> Restart-Service -Force -Name hns

The solution came to me because I wanted to restart the networking stack of WSL and to see if it would resolve this issue. The method to do it comes from this Github gist. To restart the networking stack of WSL, run the following commands,


# Restart the Host Network Service 
Restart-Service -Force -Name hns
# Restart the Windows Subsystem for Linux Manager
Restart-Service LxssManager
# Restart the WSL Network adapter
Restart-NetAdapter -Name "vEthernet (WSL)"

In my case, restarting the Windows Container Networking service (hns) is sufficient for me to resolve the problem.

Reference

  1. Reset your WSL network connection trying to fix WSL2 media disconnected error
  2. WSL2 SSH can't connect to any public SSH server
  3. SSH connections hanging from WSL2

Thursday, April 7, 2022

Failed to start Android Studio on Windows

Android Studio refuses to start due to the error,


Internal error. ...

java.util.concurrent.CompletionExecution: java.net.BindExcetion: Address already in use: bind
...

Apparently, Windows sometimes does release TCP ports completely for some reason, and the solution is to restart the winnat service. 

  1. Open Windows Command Prompt as an Administrator 
  2. Run the following to stop and restart winnat service
    net stop winnat
    net start winnat
    

Alternatively, open PowerShell as an Administrator, and run


Restart-Service -Name winnat