What's the difference between running a script under command box and interpreter?

jfong at ms4.hinet.net jfong at ms4.hinet.net
Thu Oct 31 23:44:25 EDT 2019


The script test.py is something like this:
-------test.py
from pyeds import fsm
...
...
class Rule_Parse:
    def __init__(self):
        ...
        self.current_char = ''
...
...
def main(input_str):
    for c in input_str:
        ...
        rule.current_char = c
        ...

if __name__ == '__main__':
    input_str = '(NNS(acoustics) & RB(not)) | JJ(muted)'
    rule = Rule_Parse()
    main(input_str)
    ...

-----------
The test.py can run correctly under command box:
D:\Works\Python\pyeds-master\src>py test.py

but fails when running under interpreter:
D:\Works\Python\pyeds-master\src>py
Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from test import *
>>> input_str = "(NNS(acoustics) & RB(not)) | JJ(muted)"
>>> rule = Rule_Parse()
>>> main(input_str)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Works\Python\pyeds-master\src\test.py", line 229, in main
    rule.current_char = c
NameError: name 'rule' is not defined
>>>

I did check the globals using dir() and the 'rule' is there, no doubt.
I also tried "py -m pdb test.py" and step through it, no problem too.

Why?

--Jach


More information about the Python-list mailing list