Solved

problem with re

Posted on 2006-11-08
9
319 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Loaded data from a remote database into excel spreadsheet 2 123
Python - How does this 2D array not work? 3 89
looping 11 37
Is it true to say that Python is successor of Perl? 7 189
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…
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…

790 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