2

I'm trying to install wxPython on Ubuntu 16.04. I'm aware of this question and related answer. In particular, if I run a .py file that uses wxPython python3: wxPython_HelloWorld.py (copy-pasted from the introductory page to wxPython)

I get the error message:

Traceback (most recent call last):
  File "wxPython_HelloWorld.py", line 2, in <module>
    import wx
ImportError: No module named 'wx'

But python-wxgtk3.0 is installed. Indeed, running the installation command sudo apt-get install python-wxgtk3.0 returns:

Reading package lists... Done
Building dependency tree
Reading state information... Done
python-wxgtk3.0 is already the newest version (3.0.2.0+dfsg-1build1).
0 upgraded, 0 newly installed, 0 to remove and 79 not upgraded.

Does anybody have a guess about what the problem could be?

Giovanni De Gaetano
  • 1,322
  • 2
  • 11
  • 17

1 Answers1

3

You are trying to run the following Python script:

#!/usr/bin/env python
import wx
app = wx.App(False)
frame = wx.Frame(None, wx.ID_ANY, "Hello World") 
frame.Show(True) 
app.MainLoop()

The Python used for the REPL is not the same as the version of Python the script is being run in ( python ). In particular I was able to duplicate the error in your question with python3 as follows:

python3
>>> import wx
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'wx'

The solution was to run the script in Python 2.x, which in my Ubuntu 16.04 is Python 2.7.12.

karel
  • 114,770
  • Thank you very much! This is precisely the case. Indeed, trying to install wxPython via pip gives Requirement already satisfied: wxpython in /usr/lib/python2.7/dist-packages/wx-3.0-gtk2 . And the package is therefore installed for python2.7. Now the question becomes how to use wxPython in combination with python3. But I'll try to solve it by myself. – Giovanni De Gaetano Feb 22 '17 at 12:18
  • 1
    I got wxPython phoenix for python3 by running the command: sudo pip3 install -U --pre \ -f https://wxpython.org/Phoenix/snapshot-builds/linux/gtk3/ubuntu-16.04 \ wxPython_Phoenix which I got by browsing the official page https://wxpython.org/. – Giovanni De Gaetano Feb 23 '17 at 10:36