Sunday 22 June 2014

Metasequoia Script – Import one of your own modules

Sometimes a Metasequoia Python script may contain many lines of code.

To keep the script simple you can split the script into several smaller scripts.

For instance you could have all your function definitions in a script of their own.

To access those functions you would import the script containing your functions into your main script the same as we have been doing for Python’s inbuilt modules such as “math”.

The script you import must have a filename that is a valid Python identifier so cannot start with a number and use only letters or numbers or the underscore, ‘_’.

The script file must have the extension ".py".

The script you import must not have any executing code or only have executing code in an “if __name__== “__main__”:” block.

For Metasequoia 4 the script or scripts you import must be in the same folder with the main script.

If you use Metasequoia’s MQSystem classes in the script you import, in that script you must explicitly import the MQSystem module as follows or you will get errors.

import MQSystem


Here is a script that defines a function named “adder” to add two numbers and then print the sum to the output.

'''
Metasequoia Python script


type a description here

'''

# imports


# global variables
doc = MQSystem.getDocument()


# function definitions
def mqprint(message):
 try:
  print(message)
 except:
  MQSystem.println(repr(message))
 return
 
def adder(x, y):
 return x + y
 
# main script function
def main():
 a = 3
 b = 7
 total = adder(a, b)
 mqprint(total)
 return 
 

if __name__ == "__main__":
 # clear output window
 MQSystem.clearLog()
 
 # run main function
 main()
 
 # let user know script finished
 mqprint("Script finished")

The output from the script is shown below.

10
Script finished

We can write the “adder” function in another Python script file named “mymodule.py”.

# mymodule.py

def adder(x, y):
 return x + y

We delete the “adder” function definition from the main script and instead import it from “mymodule.py”.

To use the function notice how we have to prefix it with “mymodule” and then a full stop (dot) ‘.’.

'''
Metasequoia Python script


type a description here

'''

# imports
# import mymodule module to use adder function 
# when importing a module omit the .py extension
import mymodule ## new

# global variables
doc = MQSystem.getDocument()


# function definitions
def mqprint(message):
 try:
  print(message)
 except:
  MQSystem.println(repr(message))
 return
 
# main script function
def main():
 a = 3
 b = 7
 total = mymodule.adder(a, b) ## new
 mqprint(total)
 return 
 

if __name__ == "__main__":
 # clear output window
 MQSystem.clearLog()
 
 # run main function
 main()
 
 # let user know script finished
 mqprint("Script finished")

The output from the new main script is the same as before.

10
Script finished

Remember to import Python modules such as "math" in the script you import if it needs them.

This post [link] explains some of the basics of using Metasequoia’s Script Editor.

UPDATE  6 June 2018:

So Python can find your module to import, the path to your module has to be made known to Python. Metasequoia does this automatically but gets the path only from the first script you open and run for the session.

To see which folders Python knows to search for modules use the command print(sys.path).

If you open another script that imports a module from a different folder than your first script, Python will be unable to find that module and gives a "module not found" error. This behaviour may be corrected in Metasequoia 4.6.6 but if you are using an earlier version you should add the following code to the first line of your script that imports another module.

sys.path.append("C:/../path")

Where you replace C:/../path with your actual path to your imported python file. Use forward slashes for the path or escaped backslashes, e.g C:\\..\\path.

Metasequoia's script editor automatically imports the sys module so you don't have to import it explicitly.

Note that adding this line of code isn't portable, it will need to be changed if import module is moved to a different folder.

A more portable solution if the imported modules are in the same folder as the main script is to add the following code before you import your module.

import os
sys.path.append(os.getcwd())











No comments:

Post a Comment