global name 'sqrt' is not defined

Discussion in 'Python' started by Nick Matzke, Feb 5, 2009.

  1. Nick Matzke

    Nick Matzke Guest

    Hi all,

    So, I can run this in the ipython shell just fine:

    ===========
    a = ["12", "15", "16", "38.2"]
    dim = int(sqrt(size(a)))

    dim
    >2

    ===========


    But if I move these commands to a function in another file, it freaks out:


    =================================
    a = distances_matrix.split('\t')
    from LR_run_functions_v2 import make_half_square_array
    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
    1549 a = linear_version_of_square_array
    -> 1550 dim = int(sqrt(size(a)))
    1551
    1552

    NameError: global name 'sqrt' is not defined

    =================================




    Here's the function in LR_run_functions_v2.py

    ==========================
    def make_half_square_array(linear_version_of_square_array):

    a = linear_version_of_square_array
    dim = int(sqrt(size(a)))
    ==========================



    Any ideas? If I do something like "import math" in the subfunction,
    then the error changes to "global name 'math' is not defined".

    Thanks!
    Nick







    --
    ====================================================
    Nicholas J. Matzke
    Ph.D. student, Graduate Student Researcher
    Huelsenbeck Lab
    Center for Theoretical Evolutionary Genomics
    4151 VLSB (Valley Life Sciences Building)
    Department of Integrative Biology
    University of California, Berkeley

    Lab websites:
    http://ib.berkeley.edu/people/lab_detail.php?lab=54
    http://fisher.berkeley.edu/cteg/hlab.html
    Dept. personal page:
    http://ib.berkeley.edu/people/students/person_detail.php?person=370
    Lab personal page: http://fisher.berkeley.edu/cteg/members/matzke.html
    Lab phone: 510-643-6299
    Dept. fax: 510-643-6264
    Cell phone: 510-301-0179
    Email:

    Mailing address:
    Department of Integrative Biology
    3060 VLSB #3140
    Berkeley, CA 94720-3140

    -----------------------------------------------------
    "[W]hen people thought the earth was flat, they were wrong. When people
    thought the earth was spherical, they were wrong. But if you think that
    thinking the earth is spherical is just as wrong as thinking the earth
    is flat, then your view is wronger than both of them put together."

    Isaac Asimov (1989). "The Relativity of Wrong." The Skeptical Inquirer,
    14(1), 35-44. Fall 1989.
    http://chem.tufts.edu/AnswersInScience/RelativityofWrong.htm
    ====================================================
     
    Nick Matzke, Feb 5, 2009
    #1
    1. Advertising

  2. Nick Matzke

    Rob Guest

    > Any ideas? If I do something like "import math" in the subfunction,
    > then the error changes to "global name 'math' is not defined".


    Presumably ipython does an import so you need to import it yourself
    something like:

    from math import sqrt

    at the top of your source file. I don't know why math would not be
    defined, it's built in -- probably a scoping logic problem in your code.
    Put the form/import statement at the top of your module file (as is
    usually done), not inside a function or other block.

    Rob
     
    Rob, Feb 9, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elmar Haneke

    global name "Result" is not defined

    Elmar Haneke, Mar 30, 2005, in forum: Python
    Replies:
    1
    Views:
    458
    Fredrik Lundh
    Mar 30, 2005
  2. Oodini
    Replies:
    1
    Views:
    1,842
    Keith Thompson
    Sep 27, 2005
  3. Replies:
    4
    Views:
    360
    Pete Becker
    Nov 22, 2005
  4. Replies:
    1
    Views:
    541
    Sion Arrowsmith
    Jul 10, 2008
  5. M.-A. Lemburg

    Re: global name 'sqrt' is not defined

    M.-A. Lemburg, Feb 5, 2009, in forum: Python
    Replies:
    5
    Views:
    988
    Robert Kern
    Feb 6, 2009
Loading...

Share This Page