This topic describes methods of calling items declared in another unit with Python code.
To reference another unit from the current unit, add the following line at the beginning of the current script (before any functions and variable declarations):
# To reference a single unit
from UnitName import *
# To reference multiple units
from UnitName1 import *
from UnitName2 import *
# To reference a single routine
from UnitName1 import RoutineName
# To reference an item explicitly
Once an external unit is linked to the current one, all the routines, as well as global variables and constants of the external unit, can be called in the current unit. It is recommended, though not obligatory, to prefix the names of external routines, variables and constants with the unit's name. That is, in general, calling items from another unit with any of the following code instructions is correct:
# To call items explicitly
from MyExtUnit import *
# To call items implicitly
from MyExtUnit import *
However, you must specify the name of the referenced unit in the following cases:
When the current unit already contains a routine, global variable or global constant with the same name. Without prefixing, the routine, variable or constant of the current unit will be called.
When the name of an external routine is the same as the name of an external unit. Calling such a routine without a prefix will cause an exception.
When you refer to another unit using the
import code instruction. This feature is determined by the Python naming system and cannot be affected. For more information about Python specifications, see Python Reference.
To call a routine defined in an external unit that is linked to your current unit, you can also pass a string that specifies the target routine’s full name (that is, "unit_name.routine_name") to the
eval function. If the external routine you want to call takes parameters, include them into the string parameter you pass to the eval functions.
# [UnitA] import UnitB def Test(): eval("UnitB.TestFunction()") eval("UnitB.TestFunction(1, 'string', True)") result = eval("UnitB.TestFunction()")
To make a variable or constant visible to other units, declare it as global, that is, outside any routines.
Python allows script units to refer to each other with circular references. For instance, if you need to call scripts declared in UnitA from UnitB, and to call scripts declared in UnitB from UnitA, you must add references to both units.
USEUNIT statement does not import, share or merge project items. So, these elements remain available only for the project the unit is imported from. To be able to work with elements that reside within external projects, you may need to import them to or share them with the current project that uses the unit. For more information about this, see Sharing Project Items and Their Child Elements Among Several Projects.
If you reference a script from another project, and that script uses object names from Name Mapping, you may need to merge the external Name Mapping file with your local Name Mapping file. For instructions, see Merge Name Mapping Files.