tags:

views:

63

answers:

1

Python came pre-installed on my macbook and I have been slowly getting acquainted with the langauge. However, it seems that my configuration of the re library is incorrect, or I simply misunderstand something and things are amiss. Whenever I run a python script with "import re", I recieve the following error:

Traceback (most recent call last):
  File "regex.py", line 2, in <module>
    import re
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py", line 4, in <module>
    # re-compatible interface for the sre matching engine
AttributeError: 'module' object has no attribute 'compile'

What gives!

+3  A: 

Pretty mysterious problem, given that line 4 in that file (and many other lines around that line number) is a comment (indeed the error msg itself shows that comment line!-) so even with the worst misconfiguration I'd be hard put to reproduce the problem as given.

Let's try to simplify things and check how they may (or may not) break. Please open a Terminal, mkdir a new empty directory somewhere and cd into it (so we know there's no filename conflict wrt modules etc), at the bash prompt unset PYTHONPATH (so we know for sure that isn't interfering), unset PYTHONSTARTUP (ditto); then type the command:

$ python -c'import re; print re.__file__'

It should emit the line:

/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.pyc

does it? If so, then we can keep rooting around to understand what name clash (or whatever) caused your original problem. If the problem persists under such "clean" conditions then your system is jinxed and I would reinstal Mac OS X Leopard if I were in your shoes!

Alex Martelli
Hey thanks for the in depth comment! I've tried the above and gotten an error I assumed I would get. re._file_ should be some sort of file, no? Typing that as is, yields File "<string>", line 1, in <module>AttributeError: 'module' object has no attribute '_file_'
Chris
Alex Martelli
Chris, you need to use *two* underscores before and after `file` here to follow Alex's instructions. Can you repost the result with `__file__` rather than `_file_`?
Daniel Roseman