Q100398: How to overcome issues using PySide Python scripts in Nuke 11+

Follow

 

SYMPTOMS

When launching a Python script with Nuke 11.0v1, or later, if your script requires Python's PySide module, then Nuke will throw an exception which will prevent it from opening. 

Displayed below, the following exception occurs on Nuke launch if a PySide module is imported in Nuke's init.py file:
 
 
Within Nuke's launch terminal, an ImportError will be shown specifying that Nuke could not import a PySide module, in this case, PySide.QtCore:
 
 

 

CAUSE

With the release of Nuke 11.0v1, there were many library changes, making Nuke VFX Reference Platform compliant.
 
In the case of the PySide, this was updated from PySide 1.2.2 to PySide 2.0 within the Nuke core libraries, so it can now be imported as Pyside2 rather than PySide. For more information on the library versions currently shipped with Nuke, see the Third-Party Libraries and Fonts section of the documentation.
 
As a result of this update from PySide to PySide2, starting with Nuke 11.0v1, PySide modules are no longer callable.
 
For example:
 
import PySide.some_module
  
has been replaced with:

import PySide2.some_module
 
Additionally, the definition of some classes has been moved between modules. This particularly affects any GUI related classes, which have mostly been moved from PySide.QtGui to PySide2.QtWidgets, however, there are other modules that have been relocated as well.
 
 

RESOLUTION

To resolve the ImportError exception when launching Nuke, any scripts within your '.nuke' folder which use PySide, will need to be altered to catch the exception before execution.
 
Handling the exception can be done by modifying the import statements to call a try and except function, and catch the ImportError when importing the PySide modules.
 
If the exception is caught, calls can be added to import the PySide2 equivalent functions, which will prevent the ImportError error on startup. 
 
 

FURTHER INFORMATION 

As mentioned in the Cause section, some of the GUI related functions have been moved to other modules. The PySide module used for all GUI related tasks, PySide.QtGui, has mostly been moved to the PySide2.QtWidgets module. 
 
In most cases when using this module, to avoid the ImportError within Nuke, and instead of refactoring each Pyside.QtGui function in your script, you should now import PySide2.QtWidgets as QtGui instead.
 
This can be set up like the following code snippet:
try:
    from PySide import QtGui, QtCore
except ImportError:
from PySide2 import QtCore
from PySide2 import QtWidgets as QtGui
 
This allows older PySide code to run in PySide2, as most of the QtGui functions have moved to the QtWidgets module.  Please note: This will work for many PySide modules, but not all of them, so you may still have to update other parts of your code.
 
To make sure scripts using PySide modules work correctly in Nuke 11 and Nuke 12, you need to complete the transition and all PySide functions will need to be refactored as PySide2 versions. 
 
If you would like to have code compatibility when using PySide and PySide2 across both Nuke 10.5 and Nuke 11 or 12, the Qt.py project provides a process which switches the PySide and PySide2 modules, depending on your Nuke version. 
 
NOTE: The Qt.py project is created by a third party and as such, any problems or questions need to be reported to the third party project owners. 
 
 

ADDITIONAL READING

If you would like to know more about handling exceptions, you can access the Python documentation on exceptions at the following link: Python Documentation - Handling Exceptions

 
 

    We're sorry to hear that

    Please tell us why