1

I have been playing around with the improved regex module for Python by Matthew Barnett and found a strange error (behaviour? bug?).
Consider the following code:

import regex as re
string = "liberty 123, equality 123, fraternity 123"
rx = r'\d+(?=,|$)'
results = re.findall(rx, string)
print (results)

When invoked from the command line on my Mac (python regex.py), I get the error AttributeError: 'module' object has no attribute 'findall', while when I copy and paste the exact same code to the python shell, it correctly outputs

['123', '123', '123']

Can somebody enlight me please? Is this some obvious thing I am missing here?

4
  • 7
    Is regex.py your file? Did you name it the exact same name as the python package you're trying to import? I'm guessing that it's actually importing itself, which doesn't contain findall() Commented Feb 10, 2016 at 21:33
  • @BrendanAbel: Yes, is this a problem? Commented Feb 10, 2016 at 21:33
  • 5
    You file name is confusing the interpreter, as that name is clashing with the module regex that you are importing. Change the filename, and you should be good. Commented Feb 10, 2016 at 21:34
  • @BrendanAbel: You were the first, this solved it. Put it as an answer and I will accept it. Commented Feb 10, 2016 at 21:37

1 Answer 1

3

You must not name your modules identically to the system modules. Rename your file regex.py to something else, like my_regex.py, then delete the file regex.pyc, if it exists.

Sign up to request clarification or add additional context in comments.

1 Comment

Damn it, that was it :-) Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.