re.compile() doesn't work under Windows?

J

jay graves

ddtl said:
My script uses re.compile() function, and while it rans without errors
under Linux, when I ran that script under Windows I get the following
error:
Traceback (most recent call last):
File "C:\a\projects\re.py", line 4, in ?
import re
File "C:\a\projects\re.py", line 95, in ?
main()
File "C:\a\projects\re.py", line 37, in main
s_exp = re.compile(op['-s'])
AttributeError: 'module' object has no attribute 'compile'

What is the problem here? re module is installed and is on the path -
for example, the following code works and doesn't cause any errors:

The traceback has the answer. It seems your file is named 're.py' so
it is trying to import itself rather the the 're' python module.

I don't know why it ran OK on Linux. Maybe your script is named
differently on Linux or the sys.path order is different between Windows
and Linux.

HTH.
....
jay
 
C

Carsten Haese

Hello everybody.

My script uses re.compile() function, and while it rans without errors
under Linux, when I ran that script under Windows I get the following
error:

Traceback (most recent call last):
File "C:\a\projects\re.py", line 4, in ?
import re
File "C:\a\projects\re.py", line 95, in ?
main()
File "C:\a\projects\re.py", line 37, in main
s_exp = re.compile(op['-s'])
AttributeError: 'module' object has no attribute 'compile'

What is the problem here? re module is installed and is on the path -
for example, the following code works and doesn't cause any errors:

import re
re.compile('a')

What else could cause such an error?

Your script is called re, so "import re" is making the script import
itself instead of the re module from the library.

-Carsten
 
G

Gabriel Genellina

My script uses re.compile() function, and while it rans without errors
under Linux, when I ran that script under Windows I get the following
error:

Traceback (most recent call last):
File "C:\a\projects\re.py", line 4, in ?
import re
File "C:\a\projects\re.py", line 95, in ?
main()
File "C:\a\projects\re.py", line 37, in main
s_exp = re.compile(op['-s'])
AttributeError: 'module' object has no attribute 'compile'

What is the problem here? re module is installed and is on the path -
for example, the following code works and doesn't cause any errors:

import re
re.compile('a')

What else could cause such an error?

Your *own* module is called re.py, right?
Try another name...



Gabriel Genellina
Softlab SRL





__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 
J

John Machin

ddtl said:
Hello everybody.

My script uses re.compile() function, and while it rans without errors
under Linux, when I ran that script under Windows I get the following
error:

Traceback (most recent call last):
File "C:\a\projects\re.py", line 4, in ?
import re
File "C:\a\projects\re.py", line 95, in ?
main()
File "C:\a\projects\re.py", line 37, in main
s_exp = re.compile(op['-s'])
AttributeError: 'module' object has no attribute 'compile'

What is the problem here? re module is installed and is on the path -
for example, the following code works and doesn't cause any errors:

import re
re.compile('a')

What else could cause such an error?

Change the name of your script file from re.py to
not_the_name_of_a_module.py -- you are importing your script, not the
re module. This is shown in the traceback: import re executes the
main() function in your script.

Worked on Linux? Maybe the script wasn't called re.py on Linux.
Alternatively:

(1) In Windows at least, the current directory is placed first on the
Python module search path. I would have expected the same to happen on
*x.

(2) Did you run it using an IDE on Linux? An IDE may fiddle with
sys.path.

Bottom line: however you ran it on Linux: insert
import sys
print "sys.path is", sys.path
at the top of your script and see what it produces. Note: '' (empty
string) means current directory.

Also it's a good idea to make scripts guard against inappropriate code
being executed when the script is imported (whether deliberately or
accidentally). The standard idiom is something like this:

if __name__ == "__main__": # being run as script, not imported
def main():
do_something()

HTH,
John
 
D

ddtl

Hello everybody.

My script uses re.compile() function, and while it rans without errors
under Linux, when I ran that script under Windows I get the following
error:

Traceback (most recent call last):
File "C:\a\projects\re.py", line 4, in ?
import re
File "C:\a\projects\re.py", line 95, in ?
main()
File "C:\a\projects\re.py", line 37, in main
s_exp = re.compile(op['-s'])
AttributeError: 'module' object has no attribute 'compile'

What is the problem here? re module is installed and is on the path -
for example, the following code works and doesn't cause any errors:

import re
re.compile('a')

What else could cause such an error?


ddtl.
 
D

ddtl

Thanks everybody for pointing out the problem.
And indeed, the script was named differently on Linux.

ddtl.
 
S

Sibylle Koczian

ddtl said:
Thanks everybody for pointing out the problem.
And indeed, the script was named differently on Linux.

ddtl.

And because I just spent a day searching all the wrong corners: you
remembered to delete or rename the "re.pyc" that the first import
probably left in the same directory you had "re.py" in?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top