Install wxPython With Solved ImportError

This post describes how to install wxPython on ubuntu system and helps with solved build errors while installation. This post assumes you have bare Ubuntu 18.04.3 LTS system and it is newly installed. wxPython is a opensource, cross platform, GUI toolkit for the Python programming.

This post describes in detail for how to install wxpython ubuntu 18.04 for python2 and python3. wxPython allows Python programmers to create programs highly functional graphical

user interface, simply and easily. Being cross platform with little changes it works on Microsoft windows and Linux systems. So if you have GUI application that requires to be executed on both windows and Linux wxPython is right choice for python programmers.

‘pip’ is required as a prior condition for installation of wxPython, install ‘pip’ by running following command

$ sudo apt install python-pip

As per How to install wxPython wiki guide, to install wxPython using ‘pip’ you will need to execute following command

$  pip install -U \
-f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-18.04 \
wxPython

This command after successful execution says –

Installing collected packages: six, numpy, pillow, wxPython
Successfully installed numpy-1.16.5 pillow-6.1.0 six-1.12.0 wxPython-4.0.6

Now to test the installation execute below commands

$python2
>>> import wx
>>> wx.VERSION_STRING
'4.0.1'

After executing ‘import wx’ it gives following error

Python 2.7.15+ (default, Nov 27 2018, 23:36:35) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/bhagwat/.local/lib/python2.7/site-packages/wx/__init__.py", line 17, in <module>
from wx.core import *
File "/home/bhagwat/.local/lib/python2.7/site-packages/wx/core.py", line 12, in <module>
from ._core import *
ImportError: libpng12.so.0: cannot open shared object file: No such file or directory

Problem is with above installation it is pointing to ‘ubuntu-16.04’ and we are using ‘ubuntu-18.04’, so to fix this issue just execute below command and it will install for ‘ubuntu-18.04’

$ pip install -U  -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-18.04/ wxPython

Verify installation

$ python
Python 2.7.15+ (default, Nov 27 2018, 23:36:35) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>> wx.VERSION_STRING
'4.0.6'
>>> 

Install wxPython for python 3

Above steps works for Python 2, but how to install wxPython on python 3 or higher version. so, It seems wxPython is not fully ported for python 3 and above. But, First you will need ‘pip3’ to install wxPython for python 3.

$ sudo apt install python3-pip

So, This will install python3.6-dev and python3-dev, and once ‘pip3’ is installed simply execute following command

$  pip3 install wxPython

This command is failing with following error

    File "build.py", line 30, in <module>
import pathlib2
ModuleNotFoundError: No module named 'pathlib2'
Command '"/usr/bin/python3" -u build.py build' failed with exit code 1.
----------------------------------------
Failed building wheel for wxPython
Running setup.py clean for wxPython
so install 'pathlib2' by running following command
$ pip3 install pathlib2
If above command fails use below command to upgrade 'wxPython' 
$ python3 -m pip install --upgrade --pre -f https://wxpython.org/Phoenix/snapshot-builds/ wxPython

Now if you got following errors:
python-config : not found
Checking for library python3.7m in LIBDIR : not found
Checking for library python3.7m in python_LIBPL : not found
Checking for library python3.7m in $prefix/libs : not found
Checking for library python3.7m in LIBDIR : not found
Checking for library python3.7m in python_LIBPL : not found
Checking for library python3.7m in $prefix/libs : not found
Checking for library python37 in LIBDIR : not found
Checking for library python37 in python_LIBPL : not found
Checking for library python37 in $prefix/libs : not found
Checking for header Python.h : Distutils not installed? Broken python installation? Get python-config now!
The configuration failed

Install new python3.7.4 if above error still there then uninstall wxpython.

$sudo apt-get install python-dev
$pip uninstall wxpython
$sudo apt install libgtk-3-dev

Still, If you are getting following error:
File “/home/.local/lib/python3.8/site-packages/wx/core.py”, line 12, in <module>
from ._core import *
ImportError: libSDL2-2.0.so.0: cannot open shared object file: No such file or directory
To solve:

$sudo apt-get install git curl libsdl2-mixer-2.0-0 libsdl2-image-2.0-0 libsdl2-2.0-0
$pip install -U -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-18.04 wxPython

Thats it happy to help you during install wxpython ubuntu 18.04 for python2 and python3. Keep reading and comment out your experiences.