old pyc with new py

Steve Holden sholden at holdenweb.com
Tue Nov 6 10:46:11 EST 2001


"Giorgi Lekishvili" <gleki at gol.ge> wrote in message
news:3BE880FD.B604E77A at gol.ge...
> Hello!
>
> When I find an error i my scripts, and improve them, recompilation has
> no effect:
>
> ++++++++++++++
> Traceback (innermost last):
>   File "T:\tests\init.py", line 8, in ?
>     d=PyPLS2DataSet(fi)
>   File "D:\Python20\ChemoPyc\PyPLS2DataSet.py", line 20, in __init__
>     PyPLSDataSet.__init__(self, filename, 3, format)
>   File "D:\Python20\ChemoPyc\PyPLSDataSet.py", line 11, in __init__
>     PyChemDataSet.__init__(self, filename, pr, format)
>   File "D:\Python20\ChemoPyc\PyChemDataSet.py", line 42, in __init__
>     self.comment, self.Npred, self.Nobj, self.flags, self.ObjID,
> self.ObjNames, self.Mdata, self.headers=get_data(filename)
>   File "D:\Python20\ChemoPyc\auxil.py", line 20, in get_data
>     ObjID=None
> NameError: There is no variable named 'NONE'
> +++++++++++++++++++
>
> as you see, the correct keyword is set(None instead of NONE), but python
> applies the old stuff. Note, that I delete the *.pyc and *.pyo files
> before invoking the sourcfiles like follows:
> from mylib import myfunc

Giorgi:

The problem here is because a repeated import does not cause the module to
be executed a second time. To force this you have to use the reload()
function. Given a module tm1.py:

#
# tm1 version 1
#
def myfunc():
    return NONE

I get an error after I import the function and run it. If I then change the
module's code, and repeat the statements which caused the error, I see:

>>> from tm1 import myfunc
>>> myfunc()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "tm1.py", line 5, in myfunc
    return None
NameError: global name 'NONE' is not defined

It picks up the right source line, but it's still executing the old code.
Very confusing!

What you need to do is:

>>> reload(tm1)
<module 'tm1' from 'tm1.py'>
>>> from tm1 import myfunc
>>> myfunc()
>>>

The module reload() forces the "from tm1 import myfunc" to pick up the
iupdated compiled version.

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list