Show matplotlib plots (and other GUI) in Ubuntu (WSL1 & WSL2) - python

So it seems on ubuntu for windows (windows subsystem for linux) people are suggesting we need to use Agg backend and just save images, not show plots.
import matplotlib
matplotlib.use('Agg') # no UI backend
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)
plt.title('About as simple as it gets, folks')
#plt.show()
plt.savefig("matplotlib.png") #savefig, don't show
How could we get it to where plt.show() would actually show us an image? My current option is to override plot.show() to instead just savefig a plot-148123456.png under /mnt/c/Users/james/plots/ in windows and just have an explorer window open viewing the images.
I suppose I could host that folder and use a browser.
My goal is to be able to run simple examples like the code above without changing the code to ftp the images somewhere etc. I just want the plot to show up in a window.
Has anyone figured out a decent way to do it?

Ok, so I got it working as follows. I have Ubuntu on windows, with anaconda python 3.6 installed.
Download and install VcXsrv or Xming (X11 for Windows) from sourceforge(see edit below)
sudo apt-get update
sudo apt-get install python3.6-tk (you may have to install a different python*-tk depnding on the python version you're using)
pip install matplotlib (for matplotlib. but many other things now work too)
export DISPLAY=localhost:0.0 (add to ~/.bashrc to make permanent. see WSL2 below)
Anyways, after all that, this code running in ubuntu on wsl worked as is:
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)
plt.title('About as simple as it gets, folks')
plt.show()
result:
Maybe this is better done through a Jupyter notebook or something, but it's nice to have basic command-line python matplotlib functionality in Ubuntu for Windows on Subsystem for Linux, and this makes many other gui apps work too.
For example you can install xeyes, and it will say to install x11-apps and installing that will install GTK which a lot of GUI apps use. But the point is once you have your DISPLAY set correctly, and your x server on windows, then most things that would work on a native ubuntu will work for the WSL.
Edit 2019-09-04 : Today I was having issues with 'unable to get screen resources' after upgrading some libraries. So I installed VcXsrv and used that instead of Xming. Just install from https://sourceforge.net/projects/vcxsrv/ and run xlaunch.exe, select multiple windows, next next next ok. Then everything worked.
Edit for WSL 2 users 2020-06-23
WSL2 (currently insider fast ring) has GPU/docker support so worth upgrade. However it runs in vm. For WSL 2, follow same steps 1-4 then:
the ip is not localhost. it's in resolv.conf so run this instead (and include in ~/.bashrc):
export DISPLAY=`grep -oP "(?<=nameserver ).+" /etc/resolv.conf`:0.0
Now double-check firewall: Windows Security -> Firewall & network protection -> Allow an app through firewall -> make sure VcXsrv has both public and private checked. (When Launching xlaunch first time, you might get a prompt to allow through firewall. This works too. Also, if VcXsrv is not in list of apps, you can manually add it, eg from 'C:\program files\vcxsrv\vcxsrv.exe')
Launch VcXsrv with "Disable access control" ticked
Note: a few WSL2 users got error like couldn't connect to display "172.x.x.x:0". If that's you try to check the IP address stored in DISPLAY with this command: echo $DISPLAY. If the showed IP seems to be wrong (i.e. "8.8.8.8" or another not working IP address) you need to change the code in ~/.bashrc showed in the point 5 to something that will get your instance's ip address. One user said this worked: export DISPLAY=$(ifconfig | grep inet | awk '{print $2}' | head -n 1 | awk '{print $0":0"}'). However for some others it did not work. YMMV, but just find your IP and use if for DISPLAY. For most WSL2 users, the command in #5 works.
Edit for Windows 11 : if MS convinced you to throw out your old computer and buy one with a TPM and so you got Windows 11, you get GUI for free. I hope they add upgrade path to do that on Windows 10 because Win10 will be like XP and last a long time since MS decided you need recent computer even though Win11 would work fine on old computers.

Plots are also viewable on WSL using Visual Studio Code, which as of the June 2019 release, includes a "Plot Viewer".
In a regular .py file, inserting a comment string #%% marks the code as a Jupyter (IPython) cell and a code lens shows options to Run Cell. In addition, right-clicking inside a Python file includes the option:
Run Current File in Python Interactive Window
From the VS Code June 2019 release:
The June 2019 update included a brand-new Plot Viewer that can be used
to manipulate any image plots, such as the popular matplotlib plots.
You can try it out by double-clicking on the plots or clicking on the
“expand image” button that is displayed when you hover over plot
images in the Python Interactive Window:
With the plot viewer, you can pan, zoom in/out, navigate through plots
in the current session, and export plots to PDF, SVG, or PNG formats.

For WSL2 using Ubuntu Focal Fossa release, I downloaded and installed VcXsrv on Windows. I use this configuration:
I don't know for Native OpenGL but it seems important to disable access control.
Now, you need to export the env variable DISPLAY in WSL.
I found the correct address in /etc/resolv.conf, but the one found in the ifconfig result isn't. So the following command doesn't work for me:
export DISPLAY=$(ifconfig | grep inet | awk '{print $2}' | head -n 1 | awk '{print $0":0"}')
That IP address can also be found in the command ipconfig into CMD.exe command context. Search for vEthernet (WSL), that should be the one.
Now, you can test that everything is working by simply executing xcalc in your WSL environnement. If xcalc is not present, install it:
sudo apt-get update && sudo apt-get install x11-apps
xcalc command should open a application looking like this on Windows:
If it is working that means that the connection to your X-server is possible but you maybe need to install some extra package for Python like:
sudo apt-get install python3.x-tk
Change .x according to your python version.

To get matplotlib to work with GTKAgg on Bash on Ubuntu on Windows, I:
installed VcXsrv under Windows (but things should work just the same with Xming)
set DISPLAY as noted above [export DISPLAY=localhost:0.0 (add to ~/.bashrc to make permanent)]
executed sudo pip uninstall matplotlib
followed by sudo apt install python-matplotlib
updated matplotlibrc to read backend : GTKAgg (rather than backend : agg)
I also ran sudo apt-get install python-gtk2-dev, but this may not be necessary.
Uninstalling the pip-installed matplotlib and reinstalling it via apt appear to be necessary because pip does not include the C extensions needed to run GTK, but the apt version does.

With Windows 11 22000, Linux GUI apps are officially supported out of the box provided pre-requisites are met. No twaeks, No hacks, No firewall changes etc.
Pre-Requisites:
Windows 11 Build 22000 or higher
Installed driver for vGPU (Intel/Nvidia/AMD)
Then run the commands
wsl --update
wsl --shutdown
Done !!
Official Source : https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps

In my case (Windows 10, WSL2, Ubuntu 20.04 with miniconda3 and virtual environment where I installed all the required Python 3.6 packages using conda command, Windows-based Visual Studio Code with 'Python' and 'Remote - WSL' extensions) I installed VcXsrv under Windows, and added
export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
to .bashrc file in Ubuntu. Also, before running Python code from WSL, we have to launch XLaunch and select 'Disable access control' (I left 'Native opengl' checked as well). In project folder, do not name your Python file as matplotlib.py (some related issues are reported here).

In two seperate instances (wsl1 & wls2 with ubuntu 20.04) while using PyQt5 I was missing several libxcb libraries (e.g. libxcb-icccm4) which was not printed when trying to plot. I used export QT_DEBUG_PLUGINS=1 to find out which ones and then simply installed them. Basically the steps here.

had similar problem so simply installing the supported gpu driver solved my problem. Check this out!

I found the best approach is to install Jupyter on Windows Subsystem for Linux (WSL) by following
sudo apt update && upgrade
sudo apt install python3 python3-pip ipython3
Now you can install matplotlib
pip3 install matplotlib
And Jupyter Notebook
pip3 install jupyter
Check this link if you need more info Python setup on the Windows subsystem for Linux (WSL)

Related

Can I install Anaconda (not miniconda) on window nano-server docker?

I'm trying to install Anaconda distribution on Windows docker based nano-server.
This is my dockerfile (based on 4 yeras old example I found in GitHub)
# escape= `
# Use the latest Windows Server Core 2022 image.
FROM mcr.microsoft.com/windows/servercore:ltsc2022 AS base
RUN powershell (New-Object System.Net.WebClient).DownloadFile('https://repo.anaconda.com/archive/Anaconda3-2022.05-Windows-x86_64.exe', 'Anaconda3.exe')
RUN powershell Unblock-File -Path Anaconda3.exe
RUN Anaconda3.exe /InstallationType=JustMe /RegisterPython=1 /S /D=C:\Python
FROM mcr.microsoft.com/windows/nanoserver:ltsc2022-amd64
COPY --from=base C:\Python C:\Python
ENV PATH="C:\Python;C:\Python\Library\mingw-w64\bin;C:\Python\Library\usr\bin;C:\Python\Library\bin;C:\Python\Scripts;C:\Python\bin;C:\Python\condabin;C:\Windows;C:\Windows\System32;"
CMD ["cmd"]
The docker looks ok, I can launch Python, but once I try to import numpy or sklearn I got errors (fail to load some DLLs). I could make numpy work by reinstalling it (pip install --force-reinstall numpy), but this workaround didn't work for other libraries.
I also tried to install only Miniconda, then use conda install to add libraries, but trying to run conda in the nanoserver fails (pythoncom error: coinitializeex failed).
Trying a similar docker but on top of server core (servercore:ltsc2022) works flawlessly.
The reason I struggle to use Anaconda on nanoserver is to minimize its size - the docker size of the container based on servercore adds ~7GB to the size on the nanoserver.
Is it possible to run Anaconda on nano-server?
thanks,
Adi

How to add Python Interpreter to PyCharm?

I followed a lot suggestions to add the interpreter to PyCharm, but they're simply not working. I want to figure out what the exact problem is here.
First, it said no interpreter is found, so I tried using Python 3.10 in Anaconda. But it seems "pip install packages" don't get used for Anaconda's Python. So, I used Terminal to get a different Python PATH: open -e .bash_profile and to see which python versions
I have using python -v, python3 -v, which python, which python3. So, I wanted to input /usr/local/bin/python3 or /usr/bin/python for the interpreter path.
The problem is that I can't add them at all... "Add Python Interpreter" doesn't let me click 'ok'.
Need to Add Python Interpreter
I watched lots of YouTube tutorials and went through relevant websites for suggestions, but nothing works. I even got rid of Python from all the folders manually (except the ones that didn't allow me to trash) and using Terminal as instructed (How to uninstall Python 2.7 on a Mac OS X 10.6.4?): sudo rm -rf Python, sudo rm -rf "/Applications/Python 2.7",
cd /usr/local/bin/
ls -l /usr/local/bin | grep '../Library/Frameworks/Python.framework/Versions/2.7' | awk '{print $9}' | tr -d # | xargs rm
Then, I tried reinstalling them using pip install python3. It says Python 3.9 already exists in some Frameworks location, and I am unsure where to go about from now since nothing seems to be working.
Any suggestions would be great. Thank you.
I'm new to programming and Stack Overflow (came here a couple of times for solutions but only recently signed up and posted), so if my questions should be posed another way, please let me know.
The interface shown in your image is for creating a new python environment (i.e. virtual environment). It sounds like you want to use an existing environment. So click on the Existing environment radia button in your image instead of New Environment. Then navigate to the python executable you want.
The prompts might make more sense to you if you click System Interpreter on the left hand side, but that shouldn't be necessary.

Python universal install with conda

I recently got a new M1 MacBook and have been trying to get my Python Dev environment setup. I’m new to conda (previously used virtualenv) and I’m wondering if there’s a way to use the Python 3.9.4 universal2 install. 3.9.2 is the highest version listed, and I can’t see how to distinguish it from the Intel/ARM installations, but running platform.machine() outputs x86_64, so it seems I have the Intel install by default. Any way to manage this?
Step 1: Come here
Scroll down to the line Anaconda 2019.03 for macOS Installer.
You need to know your Python version. To do this, write in the terminal
And select Command Line Installer
Step 2: installing via command line
After the installation is complete, go to the terminal.
If you have Python 3.7, then run:
bash ~ / Downloads / Anaconda3–2019.03-MacOSX-x86_64.sh
For Python 2.7:
bash ~ / Downloads / Anaconda2–2019.03-MacOSX-x86_64.sh
Attention: if you downloaded Anaconda not in the Downloads folder, then in the commands you need to change the path to the distribution kit.
Review the license agreement and press Enter. Finally, after a million Enter hits, type yes if you accept the license agreement.
(If you do not accept the agreement in time, then this clue will constantly catch your eye)
if you are satisfied with the installation path, then press Enter. You can change the location or abort the installation by pressing CTRL-C.
Then you will be asked: Do you wish the installer to initialize Anaconda3 by running conda init? Write yes, of course.
When the installation is complete, close a terminal window and open a new one - this will take effect your changes. Want to make sure everything works? Type conda list and see what happens. If everything works, you will see something like this:
If everything works, then congratulations! Now you can use Anaconda, Jupyter Notebooks, Spyder and other useful things.

How to run Anaconda Python on sudo

Currently using AWS to run some tests on a machine learning project. I would like to run Python scripts without internet (via root) because the internet bandwidth is extremely limited. I try to run the convnets.py script by doing
sudo python convnets.py >> output
But that does not work, as Anaconda does not use PYTHONPATH, making it impossible for root to find the Anaconda Python environment. So errors like "cannot import" and "module not found" are thrown.
How do I set this up so I can get Anaconda and sudo to play fair together?
Because using sudo uses a different PATH than your typical environment, you need to be sure to specify that you want to use Anaconda's python interpreter rather than the system python. You can check which one is being run with the following command
sudo which python
To fix this, and point to Anaconda's python interpreter, specify the full path to the correct interpreter.
sudo /path/to/anaconda/bin/python convnets.py >> output
If you do this, you should be able to access all of the modules managed by anaconda.
On the other hand, if you have an Anaconda environment created
conda create --name $ENVIRONMENT_NAME python
You can activate it prior to running your command
sudo source activate $ENVIRONMENT_NAME && python convnets.py >> output

iruby notebook only runs python

I have a new laptop and have installed python 2.7.9 and ipython 3.0.0. I then installed iruby.
Unfortunately, the iruby server runs fine with $ iruby notebook -- but the Jupyter notebook only runs python kernels.
I'm sure there's some subtle bug in the interaction of the newest Jupyter version of the notebook engine, but I haven't been able to sort it out. Does anyone know the incantation to make ths work?
Update
As #cel comments below, this may be simply that pip ipython[all] installs, by default the new iPython/Jupyter server and iRuby doesn't talk to it. I shall try installing an earlier iPython and see what happens.
I found this dockerfile, and followed the commands, which installed an older version of ipython, and now iruby works also.
In my case I observed that iruby tries to run python2-version of jupyter notebook (which is wrong in my case - by default I run python3-based version).
Solution was pretty simple - change default ipython to run python3-based jupyter (I assume that all jupyter binaries are in your /usr/local/bin):
# backup old ipython, is is not symlink!
sudo mv /usr/local/bin/ipython /usr/local/bin/ipython2
# provide ipython symlink pointing to ipython3
sudo ln -s /usr/local/bin/ipython3 /usr/local/bin/ipython

Categories