Matthias Runge
Matthias Runge

Reputation: 51

Mac Mountain Lion / Macports / Python27: Installed, activated but ImportError


I installed Macports and Python27. Actived it, but it does not work? Any clue what went wrong? I even deleted anything like brew and a previous port installation and rebooted before I installed a fresh copy again.

Thanks in advance.
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

Upvotes: 2

Views: 2249

Answers (2)

stefans
stefans

Reputation: 21

The problem is due to bash cashing the path to previously launched commands.

The short fix is:

$ hash -d python
$ python

The longer story: for efficiency, bash caches the path to previously launched commands. After installing Python through macports, if your system python has already been cached by bash, calling python will still call the system python (despite which python outputting the macports python version)

# 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

Upvotes: 2

SunSparc
SunSparc

Reputation: 1922

I was having the same problem. What finally worked for me was simply to uninstall and reinstall python through MacPorts.

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

This installed Python 2.7.5 (default, Aug 1 2013, 01:01:17).

After uninstalling and reinstalling, make sure to log out of your shell and open new shells. Old shells will still see the same problem.

Upvotes: 1

Related Questions