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?

Recommended Answers

All 2 Replies

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.

Thanks! More questions are forthcoming. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.