Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When importing a function it runs the whole script?

Tags:

python

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.

like image 419
Karl Avatar asked Sep 11 '14 13:09

Karl


People also ask

Does importing a Python script run it?

When you import a module in Python, all the code in it will be run, and all the variables in that module will be stuck on that module object.

What happens when you import a function in Python?

Importing inside a function will effectively import the module once.. the first time the function is run. It ought to import just as fast whether you import it at the top, or when the function is run. This isn't generally a good reason to import in a def.

What is import in Python script?

Import in python is similar to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way. import module_name.

What is import function in computer?

The import function imports text data by converting from external text representation to the internal format. The export function exports text data by converting from the internal format to the external text representation.


2 Answers

importing 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 direct execution, but not on import
    # This is because direct execution assigns `'__main__'` to `__name__` while import of any way assigns the name under which it is imported.

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.
like image 55
glglgl Avatar answered Oct 09 '22 02:10

glglgl


You can check if the module is imported or executed with the __name__ attribute. If the script is executed the attribute is '__main__'.

It is also good style to define a main function that contains the code that should be executed.

def main()
    # do something
    pass

if __name__ == '__main__'
    main()
like image 44
code monkey Avatar answered Oct 09 '22 03:10

code monkey