Mac Mountain Lion / Macports / Python27:安装,激活但是ImportError

时间:2013-07-28 18:58:15

标签: python macos macports


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

提前致谢。
中号

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 个答案:

答案 0 :(得分:2)

问题是由于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

答案 1 :(得分:1)

我遇到了同样的问题。最终对我有用的只是通过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。旧壳仍然会出现同样的问题。