Python 尽管安装了 wxPython 导入错误
Python import error inspite of having wxPython installed
我已经按照 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']
编辑2:
我还确保我使用的是正确版本的 python。
~$ which python
/usr/bin/python
~$ ll /usr/bin/python
lrwxrwxrwx 1 root root 9 Dec 21 2013 /usr/bin/python -> python2.7*
也许问题是 wx 文件夹的路径不在 Python 的 sys.path 中?
你可以用
查看
import sys
print sys.path
您是否安装了 python-wxversion
软件包?它包括一个文件,如果您安装了多个版本,它将 select 默认 wxPython,因此它是可选的,因此可以 selected 其他版本。这已经不多了,但是如果你想让唯一安装的 wxPython 无需 sys.path
修改就可以导入,那么仍然需要这个文件。
只需在程序的开头添加这个,对我有用,我遇到了同样的问题。
import wxversion
wxversion.select('3.0')
import wx
我用 pip 安装解决了这个问题。也许 brew 的问题与路径有关,而 pip 的安装成功且顺利。
试试这个:
sudo pip install wxpython
sudo
对我来说是必要的。
我已经按照 answer 中的说明使用 Ubuntu 14.04
中的以下命令安装了 wxPythonsudo 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']
编辑2:
我还确保我使用的是正确版本的 python。
~$ which python
/usr/bin/python
~$ ll /usr/bin/python
lrwxrwxrwx 1 root root 9 Dec 21 2013 /usr/bin/python -> python2.7*
也许问题是 wx 文件夹的路径不在 Python 的 sys.path 中?
你可以用
查看import sys
print sys.path
您是否安装了 python-wxversion
软件包?它包括一个文件,如果您安装了多个版本,它将 select 默认 wxPython,因此它是可选的,因此可以 selected 其他版本。这已经不多了,但是如果你想让唯一安装的 wxPython 无需 sys.path
修改就可以导入,那么仍然需要这个文件。
只需在程序的开头添加这个,对我有用,我遇到了同样的问题。
import wxversion
wxversion.select('3.0')
import wx
我用 pip 安装解决了这个问题。也许 brew 的问题与路径有关,而 pip 的安装成功且顺利。
试试这个:
sudo pip install wxpython
sudo
对我来说是必要的。