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

Discussion in 'Python' started by jay graves, Aug 31, 2006.

  1. jay graves

    jay graves Guest

    ddtl wrote:
    > 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
     
    jay graves, Aug 31, 2006
    #1
    1. Advertising

  2. On Thu, 2006-08-31 at 17:38, ddtl wrote:
    > 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
     
    Carsten Haese, Aug 31, 2006
    #2
    1. Advertising

  3. At Thursday 31/8/2006 18:38, ddtl wrote:

    >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
     
    Gabriel Genellina, Aug 31, 2006
    #3
  4. jay graves

    John Machin Guest

    ddtl wrote:
    > 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
     
    John Machin, Aug 31, 2006
    #4
  5. jay graves

    ddtl Guest

    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.
     
    ddtl, Aug 31, 2006
    #5
  6. jay graves

    ddtl Guest

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

    ddtl.
     
    ddtl, Sep 1, 2006
    #6
  7. ddtl schrieb:
    > 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?

    --
    Dr. Sibylle Koczian
    Universitaetsbibliothek, Abt. Naturwiss.
    D-86135 Augsburg
    e-mail : -Augsburg.DE
     
    Sibylle Koczian, Sep 4, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Umut Tezduyar
    Replies:
    4
    Views:
    2,313
    Scott Allen
    Nov 17, 2005
  2. Bo Peng
    Replies:
    1
    Views:
    493
    Bo Peng
    Jan 9, 2006
  3. Nagaraj
    Replies:
    1
    Views:
    913
    Lionel B
    Mar 1, 2007
  4. Jason Smestad
    Replies:
    3
    Views:
    962
    Jack Klein
    Feb 1, 2008
  5. Joe Ardent
    Replies:
    1
    Views:
    362
Loading...

Share This Page