【问题标题】:Mac Mountain Lion / Macports / Python27: Installed, activated but ImportErrorMac Mountain Lion / Macports / Python27:已安装、激活但 ImportError
【发布时间】:2013-07-28 18:58:15
【问题描述】:


我安装了 Macports 和 Python27。激活它,但它不起作用?任何线索出了什么问题? 我什至删除了诸如 brew 和以前的端口安装之类的东西,并在再次安装新副本之前重新启动。

提前致谢。
M

macbook-pro-15:~ MR$ sudo port select --list python
    Available versions for python:
        none
        python25-apple
        python26-apple
        python27 (active)
        python27-apple
    macbook-pro-15:~ MR$ python
    Traceback (most recent call last):
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 548, in <module>
        main()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 530, in main
        known_paths = addusersitepackages(known_paths)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 266, in addusersitepackages
        user_site = getusersitepackages()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 241, in getusersitepackages
        user_base = getuserbase() # this will also set USER_BASE
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 231, in getuserbase
        USER_BASE = get_config_var('userbase')
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sysconfig.py", line 516, in get_config_var
        return get_config_vars().get(name)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sysconfig.py", line 449, in get_config_vars
        import re
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 105, in <module>
        import sre_compile
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_compile.py", line 14, in <module>
        import sre_parse
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_parse.py", line 17, in <module>
        from sre_constants import *
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_constants.py", line 18, in <module>
        from _sre import MAXREPEAT
    ImportError: cannot import name MAXREPEAT

【问题讨论】:

  • 您同步到最新版本了吗?自修复以来,2.7 版本存在一个问题,该问题完全显示该错误。
  • Martijn,我该如何同步?我刚刚用 sudo port install python27 安装了一个新版本。
  • 补充:我跑了 sudo port -d selfupdate 和 sudo port upgrade outdated。没提升。但是我可以运行python2.7。它的工作原理比。

标签: python macos macports


【解决方案1】:

问题是由于 bash 兑现了先前启动的命令的路径。

简短的解决方法是:

$ hash -d python
$ python

更长的故事:为了提高效率,bash 缓存了先前启动的路径 命令。通过macports安装python后,如果你的系统python已经被bash缓存了,调用python还是会调用系统python(尽管which python输出的是macports python版本)

# Before installing macports
$ python -c 'print "Hello World!"'
Hello World!
$ which python
/usr/bin/python

# Install python27 via macports
sudo port install python27
sudo port select --set python python27

# The shell will still invoke the system python, despite the output of `which`
$ which python # macports
/opt/local/bin/python
$ hash -t python # system
/usr/bin/python
$ python # the shell invokes the executable from the cache, and this gives the error

# clear the cache
$ hash -d python 

# now python should work
python

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。最终对我有用的只是通过 MacPorts 卸载并重新安装 python。

    > sudo port uninstall python27
    > sudo port install python27
    > sudo port select --set python python27
    

    这安装了Python 2.7.5 (default, Aug 1 2013, 01:01:17)

    卸载并重新安装后,请确保退出您的 shell 并打开新的 shell。旧的 shell 仍然会遇到同样的问题。

    【讨论】:

    • 谢谢! make sure to log out of your shell and open new shells 简直不敢相信这么简单。
    猜你喜欢
    • 2013-04-13
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多