简体   繁体   English

在ubuntu上为python2和3安装wxpython

[英]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 我正在运行Ubuntu 16,并且同时具有python 2和3。我已经下载了wxpython,它可与python2解释器一起使用,但不能与3一起使用。我得到了

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? 我需要怎么做才能安装python 3?

A similar situation arises on Fedora 25, on which I was able to solve this. 在Fedora 25上也出现了类似的情况,我可以在上面解决此问题。 Your mileage may vary on Ubuntu. 在Ubuntu上,您的里程可能会有所不同。

First note that wxPython is available in two major versions, let's call them wx3 and wx4. 首先请注意,wxPython有两个主要版本,我们将其称为wx3和wx4。 You can identify your running version through import wx; print(wx.version()) 您可以通过import wx; print(wx.version())来标识您的运行版本import wx; print(wx.version()) 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. Fedora 25上的版本字符串显示为'3.0.2.0 gtk3 (classic)' ,即wx3品牌。 On sourceforge these versions are known as 'wxPython' and 'wxPython4', and wxpython.org calls wx4 'phoenix'. 在sourceforge上,这些版本称为“ wxPython”和“ wxPython4”,wxpython.org将wx4称为“ phoenix”。

Inspecting the source code of wx3 you will note that wx3's syntax is incompatible with python3. 检查wx3的源代码,您会注意到wx3的语法与python3不兼容。 Conversely, wx4 is compatible both with python2.7 and python3. 相反,wx4与python2.7和python3都兼容。

wx4 doesn't seem to be available on Fedora 25, therefore python3 can't run any wx out-of-the-box. wx4在Fedora 25上似乎不可用,因此python3无法开箱即用地运行任何wx。 Ubuntu may or may not have the same issue. Ubuntu可能有或没有相同的问题。

The following worked for me to install wx4 in a python3 virtual environment: 以下工作对我来说可以在python3虚拟环境中安装wx4:

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. 如果您需要将(py2,wx3)应用程序移植到python3,则明智的做法是创建一个中间步骤:(py2,wx3)->(py2,wx4)->(py3,wx4),并注意(py3, wx3)是不可能的。

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. 创建(py2,wx4)环境对我来说比较麻烦,因为使用pip2运行时,上述pip install命令无法找到头文件。

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 最终对我有用的是下载4.0.0b2源https://pypi.python.org/packages/bc/6f/f7bb525517557e1c596bf22ef3f242b87afaeab57c9ad460cb94b3b0714e/wxPython-4.0.0b2.tar.gz#md5=2e371620578f14d80d

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 . 并遵循构建说明https://github.com/wxWidgets/Phoenix/blob/master/README.rst ,从中我仅使用了构建命令python build.py dox etg --nodoc sip build

After building, you need to tell your python2 where to find the wx4 library. 构建后,您需要告诉python2在哪里可以找到wx4库。 I ended up doing that by creating a virtualenv, and creating a symbolic link like so: 最后,我通过创建一个virtualenv并创建一个符号链接来做到这一点:

/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. 后一个目录包含构建结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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