When importing a function it runs the whole script?
Question: I'm new to python and doing an assignment. It's meant to be done with linux
but as I'm doing it by myself on my own computer I'm doing it on windows.

I've been trying to do this test system that we use looking like this:

    
    
    >>> import file
    >>> file.function(x)
    "Answer that we want"
    

Then we run it through the linux terminal. I've been trying to create my own
way of doing this by making a test file which imports the file and runs the
function. But then on the other hand of just running the function it runs the
whole script. Even though it's never been called to do that.

    
    
    Import file
    file.function(x)
    

That's pretty much what I've been doing but it runs the whole "file". I've
also tried `From File Import` function; It does the same.

What kind of script can I use to script the "answer that I want" for the
testfile? As when we run in through linux terminal it says if it has failed or
scored.


Answer: `import`ing a file is equivalent to running it.

When you `import` a file (module), a new module object is created, and upon
executing the module, every new identifier is put into the object as an
attribute.

So if you don't want the module to do anything upon importing, rewrite it so
it only has assignments and function definitions.

If you want it to run something only when invoked directly, you can do

    
    
    A = whatever
    
    def b():
        ...
    
    if __name__ == '__main__'
        # write code to be executed only on diret execution, but not on import
    

This holds no matter if you do `import module` or `from module import
function`, as these do the same. Only the final assignment is different:

`import module` does:

  * Check `sys.modules`, and if the module name isn't contained there, import it.
  * Assign the identifier `module` to the module object.

`from module import function` does

  * Check `sys.modules`, and if the module name isn't contained there, import it. (Same step as above).
  * Assign the identifier `function` to the module object's attribute `function`.

