Solved

problem with re

Posted on 2006-11-08
9
312 Views
Last Modified: 2010-04-16
I have a very small script:

import re

text = open('eq.txt','r').read()
regex = '[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]'
pattern = re.compile(regex)
match = pattern.findall(text)

print ''.join(match)

However, when I try to run it, I get this error:

Traceback (most recent call last):
  File "/Applications/Komodo.app/Contents/SharedSupport/dbgp/bin/pydbgp", line 66, in <module>
    import dbgp.client
  File "/Applications/Komodo.app/Contents/SharedSupport/dbgp/pythonlib/dbgp/client.py", line 44, in <module>
    import traceback, re
  File "/Users/brian/Desktop/pyFun/re.py", line 5, in <module>
    pattern = re.compile(regex)
AttributeError: 'module' object has no attribute 'compile'

I am guessing here, but it looks like re is not being imported properly.  Although I am sure that there is some really stupid coding error here that I can not see.

Thanks,
Brian
0
Comment
Question by:bnblazer
  • 4
  • 4
9 Comments
 
LVL 11

Expert Comment

by:cjjclifford
ID: 17899620
>>File "/Users/brian/Desktop/pyFun/re.py", line 5, in <module>

Don't call the file "re.py" - its importing itself, not the correct "re" module...
0
 
LVL 1

Author Comment

by:bnblazer
ID: 17899696
Changed the file name - didn't work.  I even re-wrote the file just in case.  I do now have a slightly different error:

Traceback (most recent call last):
  File "/Applications/Komodo.app/Contents/SharedSupport/dbgp/bin/pydbgp", line 66, in <module>
    import dbgp.client
  File "/Applications/Komodo.app/Contents/SharedSupport/dbgp/pythonlib/dbgp/client.py", line 44, in <module>
    import traceback, re
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py", line 5, in <module>
    #
AttributeError: 'module' object has no attribute 'compile'

Here is the error outside of Komodo:

Traceback (most recent call last):
  File "reg.py", line 1, in <module>
    import re
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py", line 5, in <module>
    #
AttributeError: 'module' object has no attribute 'compile'

Brian
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 17899776
I tried your code straight on commandline, and it seems to work fine for me, but I've not got access to Python 2.5 at the moment (using the native Python 2.3.5 on OS-X)

I had a look at the 2.5 doc and it looks the same as expected...
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Author Comment

by:bnblazer
ID: 17899833
While I was waiting for a response, I tried some of that too.  I wonder if this is a 2.5 issue?

Brian
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 17899885
Nope, I just installed 2.5 here, and the original code seems to work fine...

Check /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py to make sure it looks OK, especially around the top lines (line 5?) - did it get truncated, or something I wonder...
0
 
LVL 1

Author Comment

by:bnblazer
ID: 17900045
This is really strange - line 5 is a blank comment line with just s #

I just ran this on my SUSE 10.x box with 2.5 installed and it ran fine.

This is getting weird.

Brian
0
 
LVL 17

Accepted Solution

by:
ramrom earned 500 total points
ID: 17900593
On my Windows system (where I have NOT written my own re.py) I see:
>>> import re
>>> re.__file__
'H:\\Python24\\lib\\re.pyc'

I'll bet when you do this (before erasing anything) the file imported will be from your working directory. Why?

When you ran your re.py, Python created re.pyc in your working directory. import re is loading this file, regardless of what you do with the .py files.

You should see re.pyc in your working directory and another in the .../lib/. Delete the one in your working directory, then try the import again.

0
 
LVL 1

Author Comment

by:bnblazer
ID: 17901076
Ding -

We have a winner.  Thanks ramrom.

Brian
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 17904938
ha, yes, forgot the .pyc - still, related to my original answer...
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now