【问题标题】:Can not import wxPython (Mac)无法导入 wxPython (Mac)
【发布时间】:2014-02-18 20:06:53
【问题描述】:

我以为我会从使用 Tkinter 转到 wxPython,但我遇到了一些麻烦。我得到的只是这个:

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

我已经安装了 wxPython。当我运行卸载程序时,我清楚地看到已安装 wxPython:

  1.  wxPython3.0-osx-cocoa-py2.7                 3.0.0.0

Enter the number of the install to examine or 'Q' to quit: 

当我启动 Python 时,我发现我的版本应该与 wxPython 的版本匹配:

Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

我使用的是 OS X 10.7.5 32 位。

任何帮助都会很棒! :)

【问题讨论】:

  • 使用该帖子中给出的命令运行 python 并不能解决我的问题。
  • Mac 上的 wxpython 有点麻烦,因为苹果预装了 python。你确定你正在运行你安装 wx 的 python 实例吗?

标签: python python-2.7 wxpython osx-lion


【解决方案1】:

Mac 可以安装多个版本的 Python。您确定您为使用解释器调用的同一 python 安装了 wxPython?

试试which python,并确保此版本的python 在site-packages 中有一个指向wxPython 安装的wxredirect.pth 文件。 (如果没有搜索到wxredirect.pth。)

这是我系统上的一个版本...

> which python2.6
/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6

> more /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/wxredirect.pth
import site; site.addsitedir('/usr/local/lib/wxPython-3.0.0.0/lib/python2.6')
#import site; site.addsitedir('/usr/local/lib/wxPython-2.9.1.1/lib/python2.6')
#import site; site.addsitedir('/usr/local/lib/wxPython-unicode-2.8.12.1/lib/python2.6')

在这里,顺便说一句,我可以注释掉wxredirect.pth 中的行来选择我要使用的wxPython 版本。

【讨论】:

  • 感谢您的回答。我在哪里可以找到站点包?我在finder中搜索但没有找到这样的文件。我对整个编码这件事真的很陌生,而且我一生都在使用 Windows。
  • site-packages 是一个标准目录,其中放置了标准发行版中未包含的软件包(顺便说一句,您还应该在 Windows 中有一个 site-packages 目录)。我的出现在查找器中,但这可能是由于查找器设置。查看类似于我的路径:使用which python,从该目录备份(到bin 上方的目录)并进入lib,等等。
【解决方案2】:

我只是发现了同样的问题。 我使用brew install wxpython 将其安装在mac 上。 我尝试了上面的方法,但没有用。 我在安装另一个软件包时碰巧找到了解决方案,如下所示:

   brew install tbb
   Downloading https://homebrew.bintray.com/bottles/tbb-2017_U7.sierra.bottle.t
   ############################################################ 100.0%
   ==> Pouring tbb-2017_U7.sierra.bottle.tar.gz
   ==> Caveats
   Python modules have been installed and Homebrew's site-packages is not
   in your Python sys.path, so you will not be able to import the modules
   this formula installed. If you plan to develop with these modules,
   please run:

   mkdir -p /Users/ningrongye/.local/lib/python2.7/site-packages
   echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-        
   packages")' >> /Users/ningrongye/.local/lib/python2.7/site-
   packages/homebrew.pth`

这是自制软件所说的,我只是尝试一下,它就可以了。

宁荣

【讨论】:

    【解决方案3】:

    我有 Python 2.7 和 3.7 。 在/usr/local/bin/ 中有 2.7 和 3.7 Python 版本的符号链接以及 pip 的符号链接。

    我已经用 pip3 安装了 wxPython

    pip3 install -U wxPython
    

    然后我检查了 Python3 的安装

    myname$ python3
    Python 3.7.2 (v3.7.2:9a3ffc0492, Dec 24 2018, 02:44:43) 
    [Clang 6.0 (clang-600.0.57)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import wx
    >>> wx.version()
    '4.0.4 osx-cocoa (phoenix) wxWidgets 3.0.5'
    

    安东尼奥

    【讨论】:

      【解决方案4】:

      在我的情况下,它通过将 brew 环境重置为 2.7 来工作:

      brew link --overwrite python@2
      

      【讨论】:

        猜你喜欢
        • 2012-05-27
        • 2018-10-29
        • 2015-08-17
        • 1970-01-01
        • 2014-09-23
        • 2016-04-20
        • 2017-10-19
        • 2020-07-23
        • 2016-08-26
        相关资源
        最近更新 更多