Importing Problem on Windows

Discussion in 'Python' started by brolewis, Jan 10, 2005.

  1. brolewis

    brolewis Guest

    I have a directory that has two files in it:

    parse.py
    parser.py

    parse.py imports a function from parser.py and uses it to parse out the
    needed information. On Linux, the following code works without a
    problem:

    parse.py, line 1:
    from parser import regexsearch

    However, when I run the same command in Windows, I get the following
    error:

    ImportError: cannot import name regexsearch
    Any suggestions on why this would work on Linux but not on Windows?
    brolewis, Jan 10, 2005
    #1
    1. Advertising

  2. What version of Python are you running on Linux vs. Windows?
    Grig Gheorghiu, Jan 10, 2005
    #2
    1. Advertising

  3. brolewis

    Kartic Guest

    It is quite possible that in linux, you launched the python interpreter
    shell from the same directory you stored your parser.py and parse.py
    files.

    On windows, you probably saved the parser*.py files to some place like
    "my documents" and launched the python interpreter or IDLE.

    So, you could probably try this:
    1. Launch command shell.
    2. CD to the directory where you saved the parser*.py files.
    3. Start python.exe from the command prompt (not from the Program
    Files shortcut)
    4. Try importing.

    Easier...copy the parser*.py files into the Libs folder of your python
    installation in your windows machine.

    Thanks,
    --Kartic
    Kartic, Jan 10, 2005
    #3
  4. brolewis

    brolewis Guest

    Sorry. 2.4 in both locations
    brolewis, Jan 10, 2005
    #4
  5. I normall set PYTHONPATH to the parent directory of my module directory
    tree. If I have my module files in C:\home\mymodules and below, then I
    set PYTHONPATH to C:\home. This way, I can do "import mymodules" in my
    code.
    Grig Gheorghiu, Jan 10, 2005
    #5
  6. brolewis

    brolewis Guest

    I launched the interpreter shell from the same directory in both
    Windows and Linux before posting. That's what sent the red flag up for
    me.
    brolewis, Jan 11, 2005
    #6
  7. brolewis

    Peter Otten Guest

    brolewis wrote:

    > I have a directory that has two files in it:
    >
    > parse.py
    > parser.py
    >
    > parse.py imports a function from parser.py and uses it to parse out the
    > needed information. On Linux, the following code works without a
    > problem:
    >
    > parse.py, line 1:
    > from parser import regexsearch
    >
    > However, when I run the same command in Windows, I get the following
    > error:
    >
    > ImportError: cannot import name regexsearch
    > Any suggestions on why this would work on Linux but not on Windows?


    This has nothing to do with Linux vs. Windows. Depending on sys.path and
    your current working directory either your own parser.py or the parser.py
    module in the library is imported.
    If you want to play it safe, rename your parser.py to myparser.py or another
    name that is not already used by the standard library and avoid the
    confusion between the two modules. If you do that, don't forget to delete
    the compiled module parser.pyc or parser.pyo, too.

    Peter
    Peter Otten, Jan 11, 2005
    #7
  8. brolewis

    John Machin Guest

    brolewis wrote:
    > I have a directory that has two files in it:
    >
    > parse.py
    > parser.py
    >
    > parse.py imports a function from parser.py and uses it to parse out

    the
    > needed information. On Linux, the following code works without a
    > problem:
    >
    > parse.py, line 1:
    > from parser import regexsearch
    >
    > However, when I run the same command in Windows, I get the following
    > error:
    >
    > ImportError: cannot import name regexsearch
    > Any suggestions on why this would work on Linux but not on Windows?


    Hint for the future: use the -v argument (python -v yourscript.py
    yourarg1 etc) to see where modules are being imported from.

    Example (I don't have a module named parser anywhere):

    python -v
    [big snip]
    >>> from parser import regexsearch

    import parser # builtin <<<<==== aha!
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    ImportError: cannot import name regexsearch
    >>>
    John Machin, Jan 11, 2005
    #8
  9. brolewis

    John Machin Guest

    brolewis wrote:
    > I have a directory that has two files in it:
    >
    > parse.py
    > parser.py
    >
    > parse.py imports a function from parser.py and uses it to parse out

    the
    > needed information. On Linux, the following code works without a
    > problem:
    >
    > parse.py, line 1:
    > from parser import regexsearch
    >
    > However, when I run the same command in Windows, I get the following
    > error:
    >
    > ImportError: cannot import name regexsearch
    > Any suggestions on why this would work on Linux but not on Windows?


    Hint for the future: use the -v argument (python -v yourscript.py
    yourarg1 etc) to see where modules are being imported from.

    Example (I don't have a module named parser anywhere):

    python -v
    [big snip]
    >>> from parser import regexsearch

    import parser # builtin <<<<==== aha!
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    ImportError: cannot import name regexsearch
    >>>
    John Machin, Jan 11, 2005
    #9
    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. Peter
    Replies:
    0
    Views:
    285
    Peter
    Oct 21, 2004
  2. David Hughes

    Importing win32api with Windows 95

    David Hughes, Feb 19, 2004, in forum: Python
    Replies:
    0
    Views:
    279
    David Hughes
    Feb 19, 2004
  3. Replies:
    1
    Views:
    353
    David Fraser
    Nov 26, 2004
  4. plb
    Replies:
    2
    Views:
    340
  5. Brian Quinlan
    Replies:
    2
    Views:
    277
    Roger Upole
    Apr 27, 2006
Loading...

Share This Page