Imports a module and optionally imports names from a module.
#Import Export ModuleName as ModuleAlias { *, Name as Alias, ... }
A module name, path or resource specifier. It must be either a valid identifier or a literal string, but either syntax can be used when importing a module defined by #Module or a file.
Paths can be absolute or relative to any of the search directories.
To load an embedded script as a module, specify an asterisk followed by the resource name. For example: "*RESNAME".
[v2.1-alpha.21+]: To import a sub-module from a different group, use a path-qualified name of the form "Path:ModuleName". Path should identify the main module of the group, which is either the script itself or a file imported by #Import.
Omit Path to select a module in the current group, as in ":ModuleName". The name __Init selects the group's initial module; __Main selects the main script module.
An identifier to use within the current module to refer to the imported module object.
The name of a function, class or global variable in the imported module. This can also be one of that module's imported aliases, whether or not the alias is re-exported for wildcard import.
A named import of an undeclared variable creates an unset variable in a script module or AHK. Other built-in modules only accept members they export.
A #CSharp member's [UserDeclaredName("Name")] supplies its name for imports as well as direct and dynamic access. An imported alias retains the original member's storage or identity.
An identifier to use within the current module in place of Name.
Imports are processed after the main script file has been parsed, in an unspecified order. If ModuleName (excluding quote marks) does not match a module defined by #Module in the current group or a built-in module, #Import attempts to locate a module file and load it. For details, see Search Path.
File lookup begins in the directory of the source file containing the import, including when that source was included with #Include. Imports resolving to the same full file path share one module object and variable store. For example, "Lib" and "./Lib.ahk" refer to the same module when both resolve to the same file. Each imported file has its own group of module names, so separate files can define the same module name.
File paths compare without case on Windows and macOS, and with case on Linux. This is separate from the comparison of identifier names.
Imported names and aliases use ordinal case-insensitive comparison, including Unicode case pairs; Alias, alias and ALIAS refer to the same binding. See Names for the difference from AutoHotkey.
[v2.1-alpha.33+]: An import in a function binds names within that function and its nested functions, rather than adding them to the module's global namespace. Imports are resolved at load time and are available throughout the scope, including before the directive. A function's imported names are not exported by the module, even with the Export keyword.
Keysharp also allows imports in class bodies and ordinary statement blocks. A class-body import is available to that class's methods and properties; an import in a statement block belongs to the surrounding function or module. These placements extend the AutoHotkey syntax.
The module object is bound in the current scope only if ModuleAlias is specified or ModuleName is unquoted.
If * is used, each unassigned and undeclared name in the current scope may implicitly resolve to a name exported from ModuleName. If a name is exported from multiple imported modules, the last applicable import in source order takes precedence, including imports in enclosing functions and the module. Explicit imports and other declarations take precedence over implicit imports, regardless of order.
[v2.1-alpha.31+]: Each module exports all global names which do not begin with an underscore, except for names imported without the Export keyword.
Note: An implicitly imported variable cannot be the target of an assignment, as the presence of an assignment would cause a new variable to be created in the current module.
A load-time error is raised if an explicitly imported name conflicts with another import or declaration in the same scope. Repeating an import of the same binding is allowed. Within a function, parameters and local variables may shadow imported names, while static and global declarations with the same name conflict.
An imported variable is an alias of the original variable, including when the source module imports that name from another module. Reads, assignments and references reach the original storage. For example, &Alias, &%"Alias"% and ModuleObject.__Ref("Alias") return the same reference when each name resolves to that variable. Ordinary object properties remain property references.
An explicit alias can use a built-in variable name, such as #Import Source { Value as A_WorkingDir }. Reads use the alias. At module scope, assignments and explicit references also use the alias. Within a function, assignments and explicit references use the built-in unless the function assumes global scope or explicitly imports the alias in its function scope; declaring only global A_WorkingDir does not select the alias.
The imported names are only exported (accessible to wildcard import in other modules) if the Export keyword is used.
A wildcard Export enables lookup through all of the module's wildcard imports, including ordinary wildcard imports. An ordinary named import still prevents that name from being re-exported. Repeating the same named import uses the last directive's Export flag.
A module cannot wildcard import itself.
A named import from another module resolves indirect source names at load time. If the source name comes from an ordinary wildcard import, it remains available by name but is not re-exported.
Line breaks can be used inside the braces, due to continuation by enclosure.
Classes and functions are available even before the source module has executed, such as if the module which imports them is executing first due to a circular import.
Module loading and execution remain eager in Keysharp. Placing an import inside a function does not defer the imported module's initialization until that function is called.