简体   繁体   中英

install wxpython for python2 and 3 on ubuntu

I am running Ubuntu 16 and have both python 2 and 3. I have downloaded wxpython and it works with the python2 interpreter but not 3. I get

Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>> 

and

Python 3.5.2 (default, Sep 14 2017, 22:51:06) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'wx'

What do I need to do to get installed for python 3?

A similar situation arises on Fedora 25, on which I was able to solve this. Your mileage may vary on Ubuntu.

First note that wxPython is available in two major versions, let's call them wx3 and wx4. You can identify your running version through import wx; print(wx.version()) import wx; print(wx.version()) . The version string on Fedora 25 reads '3.0.2.0 gtk3 (classic)' , ie a brand of wx3. On sourceforge these versions are known as 'wxPython' and 'wxPython4', and wxpython.org calls wx4 'phoenix'.

Inspecting the source code of wx3 you will note that wx3's syntax is incompatible with python3. Conversely, wx4 is compatible both with python2.7 and python3.

wx4 doesn't seem to be available on Fedora 25, therefore python3 can't run any wx out-of-the-box. Ubuntu may or may not have the same issue.

The following worked for me to install wx4 in a python3 virtual environment:

pip install -U -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/fedora-26 wxPython

I presume the answer to your question would be

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

In case you need to port a (py2, wx3) application to python3, you would be wise creating an intermediate step: (py2, wx3) -> (py2, wx4) -> (py3, wx4), noting that (py3, wx3) is an impossibility.

To create the (py2, wx4) environment was more cumbersome for me, because the above pip install command fails to find header files when run using pip2.

What ended up working for me was to download the 4.0.0b2 source https://pypi.python.org/packages/bc/6f/f7bb525517557e1c596bf22ef3f242b87afaeab57c9ad460cb94b3b0714e/wxPython-4.0.0b2.tar.gz#md5=2e3716205da8f52d8039095d14534bf7

and follow the build instructions https://github.com/wxWidgets/Phoenix/blob/master/README.rst , from which I only used the build command python build.py dox etg --nodoc sip build .

After building, you need to tell your python2 where to find the wx4 library. I ended up doing that by creating a virtualenv, and creating a symbolic link like so:

/home/user/venv/lib/python2.7/site-packages/wx -> /home/user/downloads/wxPython/wxPython-4.0.0b2/wx

That latter directory cointaining the result of the build.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM