0

I want to import variables from another program using the from import function. Sample Code:

script1:
c=1
print('hello')

script2:
from script1 import c

When I run this my response is:

hello
10

I wanted to know if it is possible to make this run with only the variable c being shown.

A Bo
  • 35
  • 2
  • 4

2 Answers2

5

Move any code you don't want to run on import into the if __name__ == '__main__' block.

Daniel Roseman
  • 567,968
  • 59
  • 825
  • 842
0

You don't, that's how it works. If you want some code to not run, then move it to the section __main__:

import statements
code_that_runs()

if name == '__main__':
    code_that_won't_run_from_import()
tglaria
  • 5,338
  • 2
  • 12
  • 17