Python/Import

From EventScripts Community Encyclopedia



Contents

import

Python Module: core
Class: Na
Within ES2 package: yes

Description

This is a part of the core Python syntax and does not require importing a module to use

Import is a special Python keyword used to import other modules into your current script. It should be placed at the head of scripts. Import is NOT a method so uniquely does not have a bracketed syntax. There are a couple of variations on the syntax that allow special modes of import

Syntax

import <module>

Imports all the methods and classes of a module, in the script they become available via: <module>.<method / class>()

  • module: The name of the module you wish to import.
from <module> import <method /class OR *>

Imports only a specific method or class form a named module, it is then available directly (without reference to the module). Alternatively using * imports all the methods / classes from the named module for direct reference in the script.

  • module: The name of the module you wish to import from.
  • method / class OR *: The method or class to import or * to import all of them (can also be a list of methods etc.).
from <module> import <method / class> as <name>
import <module> as <name>

Works the same as the first 2 but assigns the module, class or method (depending on the variation) to name

  • name: The name you wish to assign your import too

Examples

import es
from es import server_var
import time as waffles
 
# es is imported so we can call methods in it
es.msg("Hello World!")
# As we specifically imported server_var method we can call it directly here
svar = server_var("a_var")
# And finally methods in the time module can be accessed via the waffles keyword
# Sleeps for 10 seconds...
waffles.sleep(10)

Notes

  • Remember you need to do all of your imports at the head of the script
blog comments powered by Disqus