Solved

problem with re

Posted on 2006-11-08
9
290 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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
While I was waiting for a response, I tried some of that too.  I wonder if this is a 2.5 issue?

Brian
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 11

Expert Comment

by:cjjclifford
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Ding -

We have a winner.  Thanks ramrom.

Brian
0
 
LVL 11

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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. …
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
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 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…

772 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

12 Experts available now in Live!

Get 1:1 Help Now