Solved

problem with re

Posted on 2006-11-08
9
303 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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 strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

863 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

23 Experts available now in Live!

Get 1:1 Help Now