尽管安装了wxPython,但Python导入错误

时间:2015-06-03 17:12:19

标签: python wxpython ubuntu-14.04

我已按照此answer中的说明使用Ubuntu 14.04中的以下命令安装了wxPython

sudo apt-get install python-wxgtk2.8 python-wxtools wx2.8-doc wx2.8-examples wx2.8-headers wx2.8-i18n

一切都安装妥当。

但是当我在代码中执行import wx时,我收到以下错误。

Python 2.7.6 (default, Mar 22 2014, 22:59:38) 
[GCC 4.8.2] on linux2
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

我是否必须安装任何东西才能在我的代码中使用wxPython?

修改

安装wxPython的目录位于sys.path

$ pip show wxPython-common
---
Metadata-Version: 1.1
Name: wxPython-common
Version: 3.0.0.0
Summary: Cross platform GUI toolkit for Python
Home-page: http://wxPython.org/
Author: Robin Dunn
Author-email: Robin Dunn <robin@alldunn.com>
License: wxWidgets Library License (LGPL derivative)
Location: /usr/lib/python2.7/dist-packages
Requires: 

从上面的输出中我们可以看到它已安装在/usr/lib/python2.7/dist-packages中,当我打印sys.path时,会包含此路径。

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:38) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-i386-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

EDIT2

我还确保使用正确版本的python。

~$ which python
/usr/bin/python
~$ ll /usr/bin/python
lrwxrwxrwx 1 root root 9 Dec 21  2013 /usr/bin/python -> python2.7*

3 个答案:

答案 0 :(得分:1)

您是否安装了python-wxversion软件包?它包含一个文件,如果您安装了多个版本,将选择默认的wxPython,因此它是可选的,因此可以选择其他版本。这样做不再那么多,但是如果你想让唯一安装的wxPython可以导入而不进行sys.path修改,那么仍然需要该文件。

答案 1 :(得分:1)

我解决了这个问题,用pip安装它。也许brew的问题是关于路径和pip安装是成功和顺利的。

试试这个:

sudo pip install wxpython

sudo在我的案例中是必要的。

答案 2 :(得分:0)

也许问题是带有wx的文件夹的路径不在Python的sys.path中?

您可以使用

进行检查
import sys
print sys.path