-
Notifications
You must be signed in to change notification settings - Fork 1
Error Checking Troubleshooting
Scripts are now “linted” on load, which means they are checked for validity against a variety of conditions. This means that more mistakes in the code can be caught before the script is run. The script is linted on load and if there are issues the output from g.load_script will look something like:
W: 2: Found indentation with tabs instead of spaces (mixed-indentation) E: 4: Unable to import 'my_file_in_inst' (import-error)
Where the initial W: indicates a warning or E: indicates an error and the number is the line number in the script.
If loading a script produces warnings only, it will still be loaded successfully and can be used thereafter, whereas any errors will prevent a script from being loaded altogether.
We do provide the option to disable linting by setting the check_script argument to false, i.e.:
g.load_script(“some_script.py”, check_script=False)
However, doing this is at your own discretion and with the understanding that you are exposing yourself to issues, either immediately or further down the line. We recommend fixing issues in scripts before running it.
We have found that there are some common warnings and errors which we have listed below with suggestions of what might be wrong. If you find something that is not included let us know and we can add it to this page to help others.
Unable to import 'file_in_inst' (import-error)
This happens when the script contains something like
from file_in_inst import my_function
where file_in_inst is a file in your NDX/Python/inst directory, such as inst_routines This may work at the command line but does not work with the checker/linter. A better way of writing this is
from inst.file_in_inst import my_function
The checker does not know of this special case of allowing inst modules on the command line unprefixed.
Undefined variable `g` or `inst`
You need to add the following lines to the top of your user script:
from genie_python import genie as g import inst
This is so that the linter knows what is being referenced when you call functions on the g or inst modules.
Undefined variable <variable_name>...
If <variable_name> is a genie_python or instrument script function that you are trying to use then you should add the following lines to the top of your script:
from genie_python.genie import * from inst import * # pylint: disable=wildcard-import, unused-wildcard-import
The first two lines are so the linter knows where <variable_name> comes from and the third line removes unwanted linter warnings about this style of import.
Found indentation with tabs instead of spaces (mixed-indentation)
Here the script file you are loading contains both spaces and tabs. This can upset the python interpreter although this is usually caught on import; we recommend using spaces for indent.
If you are using Notepad++ as your editor it can be made to use spaces instead of tabs when you press tab. This is done by
- Select from the menu bar Settings -> Preferences.
- Select Language in the left-hand box
- In "Tab Settings" box select "python"
- Untick "Use default value" and tick "Replace by space".
Unused import XXXX from wildcard import (unused-wildcard-import)
Although wildcard imports are not recommended because of the possibility of name collisions if you want to use it the linter will give you lots of warning about anything you didn't use from the package. To disable these warnings place the following comment on the import line which will suppress these warnings:
from XXXX import * # pylint: disable=unused-wildcard-import
You will still get a warning about wildcards which is good but not the warning about unused methods.