import module unbelieveable behaviour

Discussion in 'Python' started by Peter Fodrek, Jul 15, 2009.

  1. Peter Fodrek

    Peter Fodrek Guest

    Dear conference!

    I have test Why python based script for HeeksCNC post-processing does not
    work... And I've got unbelievable behavior When importing module module
    manually it works, but same opertaion from script does not
    work as seen

    /opt/HeeksCAD8/HeeksCNC> python
    Python 2.6 (r26:66714, Feb 3 2009, 20:49:49)
    [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import nc.rez
    >>>

    /opt/HeeksCAD8/HeeksCNC> python test.py
    Traceback (most recent call last):
    File "test.py", line 7, in <module>
    import nc.rez
    ImportError: No module named rez


    /opt/HeeksCAD8/HeeksCNC> python ./test.py
    Traceback (most recent call last):
    File "./test.py", line 7, in <module>
    import nc.rez
    ImportError: No module named rez


    Would anyone be helpful for me to get more information about this problem
    because pydb does not show anything usable for me,please?

    I look forward hearing form you

    Yours faithfully

    Peter Fodrek
     
    Peter Fodrek, Jul 15, 2009
    #1
    1. Advertising

  2. Peter Fodrek wrote:

    > Dear conference!
    >
    > I have test Why python based script for HeeksCNC post-processing does not
    > work... And I've got unbelievable behavior When importing module module
    > manually it works, but same opertaion from script does not
    > work as seen
    >
    > /opt/HeeksCAD8/HeeksCNC> python
    > Python 2.6 (r26:66714, Feb 3 2009, 20:49:49)
    > [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    >>>> import nc.rez
    >>>>

    > /opt/HeeksCAD8/HeeksCNC> python test.py
    > Traceback (most recent call last):
    > File "test.py", line 7, in <module>
    > import nc.rez
    > ImportError: No module named rez


    What does

    import nc
    print nc.__file__

    tell you, and is that what you'd expect it to be?

    Diez
     
    Diez B. Roggisch, Jul 15, 2009
    #2
    1. Advertising

  3. Peter Fodrek

    Carl Banks Guest

    On Jul 15, 6:12 am, Peter Fodrek <> wrote:
    > Dear conference!
    >
    > I have test Why python based script for HeeksCNC post-processing does not
    > work...  And I've got unbelievable behavior  When importing module module
    > manually it works, but same opertaion from script does not
    >  work as seen
    >
    > /opt/HeeksCAD8/HeeksCNC> python
    > Python 2.6 (r26:66714, Feb  3 2009, 20:49:49)
    > [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.>>> import nc.rez
    >
    > /opt/HeeksCAD8/HeeksCNC> python test.py
    > Traceback (most recent call last):
    >   File "test.py", line 7, in <module>
    >     import nc.rez
    > ImportError: No module named rez
    >
    > /opt/HeeksCAD8/HeeksCNC> python ./test.py
    > Traceback (most recent call last):
    >   File "./test.py", line 7, in <module>
    >     import nc.rez
    > ImportError: No module named rez
    >
    > Would anyone be helpful for me to get more information about this problem
    > because  pydb does not show anything usable for me,please?



    That's a tricky one, indeed.

    Here's my guess: test.py is a symlink to a file in another directory.

    A major difference between interactive and script mode is the value of
    sys.path[0], which is the directory Python uses for local modules and
    packages. Python uses the current directory when running in
    interactive mode, but it uses the directory where the script is
    located when it is called on a script.

    Normally, when you run python on a script in the current directory,
    the behavior is the same as interactive mode, because the directory
    where the script is located is the current directory. But there's the
    gotcha: when you run python on symlinked file, Python sets sys.path[0]
    to the directory containing the actual file, not the directory
    containing the symlink. Thus even if the symlink is in the current
    directory, sys.path[0] is set to a different one.

    As a workaround, test.py should explicitly add '' to sys.path:

    sys.path.insert(0,'')


    There is another notable difference in behavior between the two
    modes. In interactive mode, sys.path[0] is ''. If you were to change
    the current directory from within Python, using os.chdir(), then
    future imports will occur relative to the new current directory.
    However, if you run Python from a script, sys.path[0] is an actual
    directory name, so that even if you chdir, any future imports will
    still occur relative to the original directory.

    Pretty confusing if you ask me. Unfortunately Python is very
    unPythonic when it comes to importing. :(


    Carl Banks
     
    Carl Banks, Jul 16, 2009
    #3
  4. Peter Fodrek

    Peter Fodrek Guest

    On Wednesday 15 July 2009 17:41:54 Diez B. Roggisch wrote:
    > Peter Fodrek wrote:

    .......
    > What does
    >
    > import nc
    > print nc.__file__


    python
    Python 2.6 (r26:66714, Feb 3 2009, 20:49:49)
    [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import nc
    >>> print nc.__file__

    nc/__init__.pyc

    and as well

    ls nc
    attach.py hpgl2dv_read.py iso_codes.pyc nc_read.py
    emc2.py hpgl3d.py iso.py nc_read.pyc rez.pyc
    emc2_read.py hpgl3d_read.py iso_read.py num_reader.py rez_read.py
    hpgl2d.py __init__.py machines.txt rez_codes.py siegkx1.py
    hpgl2d_read.py __init__.pyc nc.py siegkx1_read.py
    hpgl2dv.py iso_codes.py nc.pyc rez.py


    and module is compiled well
    ls -la nc/rez*
    -rwxr-xr-x 1 peto users 1391 2009-07-15 13:18 nc/rez_codes.py
    -rwxrwxrwx 1 peto users 255 2009-07-15 14:28 nc/rez.py
    -rwxr-xr-x 1 peto users 222 2009-07-15 14:28 nc/rez.pyc
    -rwxr-xr-x 1 peto users 5920 2009-07-15 13:17 nc/rez_read.py

    Thank you

    Peter
     
    Peter Fodrek, Jul 16, 2009
    #4
  5. Peter Fodrek

    Peter Fodrek Guest

    On Thursday 16 July 2009 02:18:52 Carl Banks wrote:
    > On Jul 15, 6:12 am, Peter Fodrek <> wrote:
    > That's a tricky one, indeed.
    >
    > Here's my guess: test.py is a symlink to a file in another directory.


    It is not true guess
    ls -la test.py
    -rw-r--r-- 1 peto users 1990 2009-07-15 14:19 test.py

    But maybe access rights needed to be corrected...

    Thank you

    Peter
     
    Peter Fodrek, Jul 16, 2009
    #5
  6. Peter Fodrek

    Carl Banks Guest

    On Jul 15, 10:16 pm, Peter Fodrek <> wrote:
    > On Wednesday 15 July 2009 17:41:54 Diez B. Roggisch wrote:
    >
    > > Peter Fodrek wrote:

    > ......
    > > What does

    >
    > > import nc
    > > print nc.__file__

    >
    > python
    > Python 2.6 (r26:66714, Feb  3 2009, 20:49:49)
    > [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.>>> import nc
    > >>> print nc.__file__

    >
    > nc/__init__.pyc



    What does it do if you run this from a script?

    Also, what is sys.path[0] in both interactive and script?

    Inside test.py, right before the line that imports nc.rez, add the
    code "import sys; print sys.modules.get('nc')"; what does that output?

    Is there an old nc.py or nc.pyc file anywhere?


    Carl Banks

    Carl Banks
     
    Carl Banks, Jul 16, 2009
    #6
  7. Peter Fodrek

    Peter Fodrek Guest

    On Thursday 16 July 2009 07:38:23 Carl Banks wrote:
    > On Jul 15, 10:16 pm, Peter Fodrek <> wrote:
    > > On Wednesday 15 July 2009 17:41:54 Diez B. Roggisch wrote:
    > > > Peter Fodrek wrote:

    > >
    > > ......
    > >
    > > > What does
    > > >
    > > > import nc
    > > > print nc.__file__

    > >
    > > python
    > > Python 2.6 (r26:66714, Feb 3 2009, 20:49:49)
    > > [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2
    > > Type "help", "copyright", "credits" or "license" for more information.>>>
    > > import nc
    > >
    > > >>> print nc.__file__

    > >
    > > nc/__init__.pyc

    >
    > What does it do if you run this from a script?


    Ouch, I am and python rookie, that was needed to try to edit undocumented
    script from German author

    this answers everything

    /home/opt/HeeksCAD8/HeeksCNC> python ./test.py
    /usr/local/lib/heekscnc/nc/__init__.py

    and it is because begining of the script

    # -*- coding: utf-8 -*-
    import sys
    sys.path.insert(0,'/usr/local/lib/heekscnc/')
    import kurve
    import kurve_funcs
    from nc.nc import *
    import nc
    print nc.__file__
    import nc.rez

    and

    ls -a /usr/local/lib/heekscnc/nc
    .. emc2_read.py hpgl2dv_read.py iso_codes.py nc_read.py
    ... hpgl2d.py hpgl3d.py iso.py num_reader.py
    attach.py hpgl2d_read.py hpgl3d_read.py iso_read.py siegkx1.py
    emc2.py hpgl2dv.py __init__.py nc.py siegkx1_read.py

    Thank you for your help, once more.


    Peter
     
    Peter Fodrek, Jul 16, 2009
    #7
  8. Peter Fodrek

    Peter Fodrek Guest

    On Thursday 16 July 2009 08:38:57 Peter Fodrek wrote:
    > On Thursday 16 July 2009 07:38:23 Carl Banks wrote:
    > > On Jul 15, 10:16 pm, Peter Fodrek <> wrote:
    > > > On Wednesday 15 July 2009 17:41:54 Diez B. Roggisch wrote:


    > sys.path.insert(0,'/usr/local/lib/heekscnc/')


    I was mentioned that this adds system path to multipath list(As $PATH) and it
    replaces path instead. That was problem..

    Peter
     
    Peter Fodrek, Jul 16, 2009
    #8
    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.

Share This Page