5

I am trying to use the following python module:

import re
test = 'some text'
find = re.findall(r'text', test)
print(find)

When i try to run it in sublime, it writes:

AttributeError: module 're' has no attribute 'findall'

If i try to run it in Cygwin, there is a message:

AttributeError: 'module' object has no attribute 'findall'

But if i use attibute "findall" in python console, it works without any problem. I really don't undestand, what's wrong. In Sublime i use python 3.5.1, cygwin uses python 3.4.3, as i remember.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
mrser
  • 107
  • 1
  • 4
  • 12

2 Answers2

24

If you have a file called re.py, then when you try and import re, Python may look inside that file instead of inside the standard module re. So don't name a file re.py (or the name of any other module you want to import).

khelwood
  • 55,782
  • 14
  • 81
  • 108
0

I had the same problem. do not name any file or pragram as same as module name.

ex. re.py (do not name any file with this name)

krish
  • 17
  • 3
  • This is unclear if not outright misleading. There is no particular reason to save anything specifically in the home directory, though obviously if your script is poorly named, saving it in a directory other than the current directory can be a workaround. A better fix is to avoid shadowing system resource names. – tripleee Jun 20 '20 at 09:30
  • I completely agree with @tripleee , that was my mistake, I had another file named re.py due to which my program was working in the home directory instead of that directory. Thanks – krish Jun 21 '20 at 11:24