I am relatively new to python. I am calling a a function from another script (imported) from the python shell. However, when I call a variable created in that function (after calling it) the variable hasn't been defined. I thought it was a global issue so I made the variable global in the function but it still is not defined after i call the function. When I put the function right into the python shell it works and the variable is defined. How can I access a variable created in a function in an imported script?

9 Years
Discussion Span
Last Post by bikehike90

Let's use the cmath module for an example.

If you import cmath, there are two variables: e and pi.

If you type: >>> print pi You get the following error in IDLE:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    print pi
NameError: name 'pi' is not defined

What you need to do to get pi to print is to type the following:

>>> import cmath
>>> print cmath.pi

With the output: 3.14159265359 Hope that answers your question. You need to put the name of the module in front of the variable with a '.' separating them.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.