1

I have been trying to execute the below code snippet in python 3.6 IDLE by executing the below command, it works for the first time and displays the output

import circle

>>> import circle
Circumference of the circle:  21.99113
Area of the circle:  153.93791
>>> import circle

circle.py:

PI = 3.14159
r = 7
print('Circumference of the circle: ', 2 * PI * r)
print('Area of the circle: ', PI * r * r)

Expected: The output of the file should always be displayed when i import the. file

Actual: It works for the first time but not again.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Hi martineau, i have pasted the code of circle.py above, pasted it again below FYR: PI = 3.14159 r = 7 print('Circumference of the circle: ', 2 * PI * r) print('Area of the circle: ', PI * r * r) – saurabh maggo Feb 09 '19 at 14:57
  • saurabh: Oh, OK...sorry, that wasn't clear when I posted my comment. `(` but it is now `;¬)` – martineau Feb 09 '19 at 15:20

1 Answers1

2

Calling import a second time doesn't do what you expect. This is to prevent initialization code such as yours being executed more than once when the same module is imported from different places.

If you want to execute the initialization code in an imported module more than once, reload the module, as explained in this answer.

BoarGules
  • 16,440
  • 2
  • 27
  • 44