0

I want to import only one or more variables from different files but if I write

in file1:

a=4
print('Hello in file1)

and in file2:

from file1 import a

in the file2 (the second file), the compiler runs the whole code of file1. Why I can't obtain only the value of a? And, there's a way to save in a the value of input and call from file2 the value of a?

There's my idea: file1:

a=input()

and file2:

from file1 import a
print(a)

Is it possible?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Rayan16
  • 19
  • 3
  • When you import anything from another file in Python, the imported module will be executed automatically first. If you don't want that to happen and still want to access your variable, you can wrap your print statement with a function that will prevent Python from executing the statement implicitly. – Redowan Delowar Dec 16 '20 at 16:37
  • 1
    Thank you so much, you give me an idea. – Rayan16 Dec 17 '20 at 09:36

0 Answers0