import module unbelieveable behaviour

P

Peter Fodrek

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./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
 
D

Diez B. Roggisch

Peter said:
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./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
 
C

Carl Banks

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
 
P

Peter Fodrek

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.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
 
P

Peter Fodrek

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
 
C

Carl Banks

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
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
 
P

Peter Fodrek

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
 
P

Peter Fodrek

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
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top