2


我安装了 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
4

2 回答 2

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
于 2013-11-12T15:52:06.330 回答
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。旧的 shell 仍然会遇到同样的问题。

于 2013-09-18T21:56:57.963 回答