2

I'm trying to call variable from external file. And for that I wrote this code,

count = 1
while (count <= 3):
   # I want to iterate this line
   # rand_gen is the python file
   # A is the varialbe in rand_gen.py
   # Having this expression A = np.random.randint(1, 100)
   from rand_gen import A

   print('Random number is ' + str(A))
   count = count + 1

But when I run my code it only calls varible A once and prints the same result. See the output of code,

Random number is 48
Random number is 48
Random number is 48

How can I call variable A from file rand_gen.py with updated value, every time it goes into loop? Please help.

2
  • 1
    Reading the value A doesn't change the value, it was assigned the first time you loaded the module. Why aren't you just calling np.random.randint(1, 100). You can force it but it is very unintuitive, e.g. import importlib; importlib.reload(rand_gen) Commented May 29, 2018 at 3:57
  • Alternatively, make A a function and call it, e.g. def A(): return np.random.randint(1, 100) then str(A()) will give you a different value each time. Commented May 29, 2018 at 4:03

2 Answers 2

3

If you assign a random value to a variable, referencing that variable does not make the value change regardless of how the value was obtained.

a = np.random.randint(1, 100)

a # 12
# Wait a little
a # still 12

In the same way, when you imported your module, the module code was execute and a value was assigned to A. Unless the module is reloaded with importlib.reload or you call np.random.randint again, there is no reason for A to change value.

What you might want is to make A a function that returns a random value in the desired range.

# In the rand_gen module
def A():
    return np.random.randint(1, 100)
Sign up to request clarification or add additional context in comments.

Comments

2

This is not how import works in python. Once imported, module in cached in sys.modules as key, value pair of module name and module object. When you try to import the same module again, you simply get the already cached value back. But sys.modules is writable and deleting the key will, cause python to check for module and load again.

Though Olivier's answer is the right way to approach this, for your understanding of import, you can try this:

import sys       # Import sys module

count = 1
while (count <= 3):
   # I want to iterate this line
   # rand_gen is the python file
   # A is the varialbe in rand_gen.py
   # Having this expression A = np.random.randint(1, 100)
   if 'rand_gen' in sys.modules:   # Check if "rand_gen" is cached
       sys.modules.pop('my_rand')  # If yes, remove it
   from my_rand import A           # Import now

   print('Random number is ' + str(A))
   count = count + 1

Output

Random number is 6754
Random number is 963
Random number is 8825

Would recommend to read official Python docs on The import system and The module cache, for thorough understanding.

2 Comments

Thank you @akshat this is what I want. Because my actual code is different and this method works for that. Thank you again.
glad to be of help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.