Home > Enterprise >  Is it possible to import a function from another script without importing the variables from such fi
Is it possible to import a function from another script without importing the variables from such fi

Time:04-07

I have two scripts 1 and 2, I would need these two script to work independently from one another, but I also need script 2 to be able to use the function in script 1 if needed. I tried to automate as much as possible, so each script has a number of variables inside already defined.

Here is an oversimplified example of the scrips that I am talking about.

Script 1:

def plustwo(n):
    out = n   2
    print(out)

m=3
plustwo(m)

Result:
5

Script 2:

from file import plustwo

z=5
plustwo(z)

Result: 
5
7

As you can see, when I import the function from script 1, even if I use from file import plustwo, also the variable is imported, this causes script 2 to return two results, one with variable m (5) and another with variable z (7).

Do I necessarily need to create a third script, identical to script 2 but without the m variable? Is there a way to exclude such variable from the import? (I just want the function from script 2, so that I can use script 1 with the variables it already has.)

CodePudding user response:

In script 1, you can add if __name__ == '__main__':, so that it only executes

m=3
plustwo(m)

if the program is run directly from script1.py.

In the end, you only need to modify script 1 a little bit:

def plustwo(n):
    out = n   2
    print(out)

if __name__ == '__main__':
    m=3
    plustwo(m)

CodePudding user response:

No, it is not.

An import statement will always execute the whole target module the first time it runs. Even when the import is written to cherry pick just a few functions and names from the target file, the file is still executed from the first to last line - this is the way the language works.

The "module object" created in this execution (basically a namespace with all the functions, classes and variables defined globally in the module), is them made available on the sys.modules dictionary. On a second import statement referencing the same file, it will not run again: the cached version is used to pick any function or variables from it.

The practice for when a module file has direct side-effects on itself, and it still desirable that parts of it should be importable, is to put the code that you don't want to execute upon importing in a block that only runs if the automatic variable __name__ contains the string "__main__". Python does that so a module can "know" if a module is running as the main program, or has been imported as part of a greater system.

The idiom for this is ubiquous in Python projects and usually look like:


...

if __name__ == "__main__":
    # this block only runs when the coded is executed as the main program
    m = 3
    plustwo(m)

(This comes, for obvious reasons, at the end of the file, after the function has been defined)

Otherwise, the only possible workaround for that would be to read the module file as a text file, parse and re-generate another source file dynamically, containing only the lines one is interested in, which would then be imported. And even so, the functions or classes imported in this way would not possibly address any other function, class or variable in the same module, of course.

  • Related