global name 'sqrt' is not defined

Diez B. Roggisch deets at nospam.web.de
Thu Feb 5 21:57:52 CET 2009


Nick Matzke schrieb:
> 
> 
> Scott David Daniels wrote:
>> M.-A. Lemburg wrote:
>>> On 2009-02-05 10:08, Nick Matzke wrote:
>>>> ..., I can run this in the ipython shell just fine:
>>>> a = ["12", "15", "16", "38.2"]
>>>> dim = int(sqrt(size(a)))
>>>> ...But if I move these commands to a function in another file, it 
>>>> freaks out:
>>> You need to add:
>>>
>>> from math import sqrt
>> or:
>>     from cmath import sqrt
>> or:
>>     from numpy import sqrt
> 
> 
> 
> 
> 
> The weird thing is, when I do this, I still get the error:
> 
> ============
> nick at mws2[phylocom]|27> a = ["12", "15", "16", "38.2"]
> nick at mws2[phylocom]|28> from LR_run_functions_v2 import 
> make_half_square_array
> nick at mws2[phylocom]|24> d = make_half_square_array(a)
> ---------------------------------------------------------------------------
> NameError                                 Traceback (most recent call last)
> 
> /bioinformatics/phylocom/<ipython console> in <module>()
> 
> /bioinformatics/phylocom/_scripts/LR_run_functions_v2.py in 
> make_half_square_array(linear_version_of_square_array)
>    1548         from numpy import sqrt
>    1549         a = linear_version_of_square_array
> -> 1550         dim = int(sqrt(size(a)))
>    1551
>    1552
> 
> NameError: global name 'sqrt' is not defined
> nick at mws2[phylocom]|25>
> ============
> 
> Is there some other place I should put the import command?  I.e.:
> 1. In the main script/ipython command line
> 
> 2. In the called function, i.e. make_half_square_array() in 
> LR_run_functions_v2.py
> 
> 3. At the top of LR_run_functions_v2.py, outside of the individual 
> functions?

The latter. Python's imports are always local to the module/file they 
are in, not globally effective.


Diez



More information about the Python-list mailing list