Can't run function from another module

I got this code in a module:

BIGNUM = 1000
def add_big(x):
    return x + BIGNUM

When I run my main module I can’t get the code to run the function add_big().
This is the code I want to be working:

from bignum import BIGNUM
b = BIGNUM.add_big(10)
print(b)

If I import it this way it works:

import bignum
r = bignum.add_big(10)
print(r)

But why doesn’t it work when I import it the first way?

the first approach will add the function to the global namespace without the module prefix, so then you get:

b = add_big(10)

I tried it before but I get the error “add_big” is not defined.

In your first snippet you use the form from x import y. In this case load the module x but only have y accessible in your current namespace.

In this case you only have a reference to BIGNUM from the module but not the function add_big. So you can use BIGNUM but not the function. Did you perhaps intend to import the function instead (from bignum import add_big) ?

The second method import bignum would let you use both with the module prefix. bignum.BIGNUM and bignum.add_big would then be the names used.

1 Like

WOW I thought both methods were the same but now I get it. Thanks for explaining this to me!