0

I have a serie of scripts that run one after the other. In one of them i try to import variable from the first one. The problem is that when that variable is imported the whole first script gets executed. In the first script i have:

various commands
x = result of these commands

On the other script i have:

from first script import x
various other commands 
y = z + x

When this line is called the first script get executed..
Why is that? Is this technically wrong??

1
  • python is a scripting language. as juliend said your first script has lines not enclosed in functions so they will be executed. you should have only one main program in the entirety of your program. if you want to run the first script and get the value of x, you should encapsulate the calculations for x in functions and call those functions in your main script. Commented May 7, 2016 at 16:38

1 Answer 1

1

What is wrong is your first script, which shoud encapsulate the code in functions/classes, and call the main function, say run(), only if the script is called directly, with

if __main__ == '__main__':
    run()

See __main__ .

Sign up to request clarification or add additional context in comments.

Comments

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.