run_python_script_from_file#

Mechanical.run_python_script_from_file(file_path, enable_logging=False, log_level='WARNING', progress_interval=2000)#

Run the contents a python file inside Mechanical.

It returns the string value of the last executed statement. If the value cannot be returned as a string, it will return an empty string.

Parameters:
file_path

Path for the Python file.

enable_logging: bool, optional

Whether to enable logging. The default is False.

log_level: str

Level of logging. The default is "WARNING". Options are "DEBUG", "INFO", "WARNING", and "ERROR".

progress_interval: int, optional

Frequency in milliseconds for getting log messages from the server. The default is 2000.

Returns:
str

Script result.

Examples

Return a value from a simple calculation.

Contents of simple.py file

2+3

>>> mechanical.run_python_script_from_file('simple.py')
'5'

Return a value from a simple function call.

Contents of test.py file

import math

math.pow(2,3)

>>> mechanical.run_python_script_from_file('test.py')
'8'