Why I need to declare import as global in function

2005-11-28 Thread didier . doussaud
I have a stange side effect in my project :

in my project I need to write "gobal" to use global symbol :

...
import math
...
def f() :
   global math # necessary ?? else next line generate an error
message  ?
   print math.pi

(the problem is for all global module symbol)

I have certainly change somthing in my project, but I can't find what ?

(just a small program wort fine without this global of course)

Can anybody help me : where can I search the mistake in my project ?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why I need to declare import as global in function

2005-11-29 Thread didier . doussaud
the error message :

EXCEPTION RAISED::

 Traceback (most recent call last):
   File "../tu.py", line 21, in run_tu
 execfile( filename )
   File "TU_05_010.py", line 8, in ?
 import TU_05_tools
   File "./TU_05_tools.py", line 4, in ?
 f()
   File "./TU_05_tools.py", line 2, in f
 print math.pi
 NameError: global name 'math' is not defined

I have remarq that this problem is raised when I execute code in an
imported module (during importation)

I think I will be able to isolate it and have a simple sample soon 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why I need to declare import as global in function

2005-11-29 Thread didier . doussaud
You're right, the problem is around the usage of "execfile".

But I have still difficulties to get a simple sample and have no
enough  time to work on it until end of week.

I will post if I resolve my problem or if I can get a simple sample.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why I need to declare import as global in function

2005-11-30 Thread didier . doussaud
I think I understand my problem, but first the sample code extracted to
my project.

Rq : it's an automatic run of unitary test, the names of unitary test
are parameter of main program "imported" file via the "execfile"
function (an usage of "import" instead may be a solution) :

file run.py :


def run_ut( test ) :
# to have the problem the execfile MUST be in a function
execfile( test )
run_ut( "ut_00.py" )


file ut_00.py :

import math

def f() :
## global math # <-- just decomment this line to avoid error
print "Second access :"
print "\t",math.pi # ERROR

print "First access :"
print "\t",math.pi # OK
f()

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why I need to declare import as global in function

2005-11-30 Thread didier . doussaud
lot's of solutions proposed in the discussion works fine :

file run.py :

def run_ut( test ) :
# to have the problem the execfile MUST be in a function
# execfile( test ) # ERROR
execfile( test, globals() ) # OK
exec "import ut_00" # OK
exec file(test).read() # OK
run_ut( "ut_00.py" )

Thanks a lor

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why I need to declare import as global in function

2005-11-30 Thread didier . doussaud
yes I have imported math in the file I want to use it. But the imported
module "math" is not visible in function without a global instruction.

But the solutions already proposed seems to work file for my sample
program. I will try on my project soon :)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why I need to declare import as global in function

2005-11-30 Thread didier . doussaud
sample and solution posted in another branch of this thread

-- 
http://mail.python.org/mailman/listinfo/python-list